67
67
# this value due to attached metadata, so keep the number conservative.
68
68
MAX_EVENT_BYTES = 10 ** 6
69
69
70
+ # Maximum depth and breadth of databags. Excess data will be trimmed. If
71
+ # request_bodies is "always", request bodies won't be trimmed.
70
72
MAX_DATABAG_DEPTH = 5
71
73
MAX_DATABAG_BREADTH = 10
72
74
CYCLE_MARKER = "<cyclic>"
@@ -118,6 +120,8 @@ def serialize(event, **kwargs):
118
120
path = [] # type: List[Segment]
119
121
meta_stack = [] # type: List[Dict[str, Any]]
120
122
123
+ keep_request_bodies = kwargs .pop ("request_bodies" , None ) == "always" # type: bool
124
+
121
125
def _annotate (** meta ):
122
126
# type: (**Any) -> None
123
127
while len (meta_stack ) <= len (path ):
@@ -182,10 +186,11 @@ def _is_databag():
182
186
if rv in (True , None ):
183
187
return rv
184
188
185
- p0 = path [ 0 ]
186
- if p0 == "request" and path [ 1 ] == "data" :
187
- return True
189
+ is_request_body = _is_request_body ()
190
+ if is_request_body in ( True , None ) :
191
+ return is_request_body
188
192
193
+ p0 = path [0 ]
189
194
if p0 == "breadcrumbs" and path [1 ] == "values" :
190
195
path [2 ]
191
196
return True
@@ -198,13 +203,24 @@ def _is_databag():
198
203
199
204
return False
200
205
206
+ def _is_request_body ():
207
+ # type: () -> Optional[bool]
208
+ try :
209
+ if path [0 ] == "request" and path [1 ] == "data" :
210
+ return True
211
+ except IndexError :
212
+ return None
213
+
214
+ return False
215
+
201
216
def _serialize_node (
202
217
obj , # type: Any
203
218
is_databag = None , # type: Optional[bool]
219
+ is_request_body = None , # type: Optional[bool]
204
220
should_repr_strings = None , # type: Optional[bool]
205
221
segment = None , # type: Optional[Segment]
206
- remaining_breadth = None , # type: Optional[int]
207
- remaining_depth = None , # type: Optional[int]
222
+ remaining_breadth = None , # type: Optional[Union[ int, float] ]
223
+ remaining_depth = None , # type: Optional[Union[ int, float] ]
208
224
):
209
225
# type: (...) -> Any
210
226
if segment is not None :
@@ -218,6 +234,7 @@ def _serialize_node(
218
234
return _serialize_node_impl (
219
235
obj ,
220
236
is_databag = is_databag ,
237
+ is_request_body = is_request_body ,
221
238
should_repr_strings = should_repr_strings ,
222
239
remaining_depth = remaining_depth ,
223
240
remaining_breadth = remaining_breadth ,
@@ -242,9 +259,14 @@ def _flatten_annotated(obj):
242
259
return obj
243
260
244
261
def _serialize_node_impl (
245
- obj , is_databag , should_repr_strings , remaining_depth , remaining_breadth
262
+ obj ,
263
+ is_databag ,
264
+ is_request_body ,
265
+ should_repr_strings ,
266
+ remaining_depth ,
267
+ remaining_breadth ,
246
268
):
247
- # type: (Any, Optional[bool], Optional[bool], Optional[int], Optional[int]) -> Any
269
+ # type: (Any, Optional[bool], Optional[bool], Optional[bool], Optional[Union[float, int]] , Optional[Union[float, int] ]) -> Any
248
270
if isinstance (obj , AnnotatedValue ):
249
271
should_repr_strings = False
250
272
if should_repr_strings is None :
@@ -253,10 +275,18 @@ def _serialize_node_impl(
253
275
if is_databag is None :
254
276
is_databag = _is_databag ()
255
277
256
- if is_databag and remaining_depth is None :
257
- remaining_depth = MAX_DATABAG_DEPTH
258
- if is_databag and remaining_breadth is None :
259
- remaining_breadth = MAX_DATABAG_BREADTH
278
+ if is_request_body is None :
279
+ is_request_body = _is_request_body ()
280
+
281
+ if is_databag :
282
+ if is_request_body and keep_request_bodies :
283
+ remaining_depth = float ("inf" )
284
+ remaining_breadth = float ("inf" )
285
+ else :
286
+ if remaining_depth is None :
287
+ remaining_depth = MAX_DATABAG_DEPTH
288
+ if remaining_breadth is None :
289
+ remaining_breadth = MAX_DATABAG_BREADTH
260
290
261
291
obj = _flatten_annotated (obj )
262
292
@@ -312,6 +342,7 @@ def _serialize_node_impl(
312
342
segment = str_k ,
313
343
should_repr_strings = should_repr_strings ,
314
344
is_databag = is_databag ,
345
+ is_request_body = is_request_body ,
315
346
remaining_depth = remaining_depth - 1
316
347
if remaining_depth is not None
317
348
else None ,
@@ -338,6 +369,7 @@ def _serialize_node_impl(
338
369
segment = i ,
339
370
should_repr_strings = should_repr_strings ,
340
371
is_databag = is_databag ,
372
+ is_request_body = is_request_body ,
341
373
remaining_depth = remaining_depth - 1
342
374
if remaining_depth is not None
343
375
else None ,
0 commit comments