11import uuid
2- from datetime import datetime
2+ import datetime
33
44from collections import Mapping , Sequence
55
66from .utils import exceptions_from_error_tuple
77from ._compat import text_type
88
99
10- def datetime_to_json (dt ):
10+ def _datetime_to_json (dt ):
1111 return dt .strftime ("%Y-%m-%dT%H:%M:%SZ" )
1212
1313
@@ -17,7 +17,7 @@ class Event(Mapping):
1717 def __init__ (self , data = {}):
1818 self ._data = {
1919 "event_id" : uuid .uuid4 ().hex ,
20- "timestamp" : datetime_to_json ( datetime .utcnow () ),
20+ "timestamp" : datetime .datetime . utcnow (),
2121 "level" : "error" ,
2222 }
2323
@@ -124,6 +124,16 @@ def strip_frame(frame):
124124 return frame , ({"vars" : meta } if meta is not None else None )
125125
126126
127+ def convert_types (obj ):
128+ if isinstance (obj , (datetime .datetime , datetime .date )):
129+ return _datetime_to_json (obj )
130+ if isinstance (obj , Mapping ):
131+ return {k : convert_types (v ) for k , v in obj .items ()}
132+ if isinstance (obj , Sequence ) and not isinstance (obj , (text_type , bytes )):
133+ return [convert_types (v ) for v in obj ]
134+ return obj
135+
136+
127137def strip_databag (obj , remaining_depth = 20 ):
128138 assert not isinstance (obj , bytes ), "bytes should have been normalized before"
129139 if remaining_depth <= 0 :
0 commit comments