6767# this value due to attached metadata, so keep the number conservative.
6868MAX_EVENT_BYTES = 10 ** 6
6969
70+ # Maximum depth and breadth of databags. Excess data will be trimmed. If
71+ # request_bodies is "always", request bodies won't be trimmed.
7072MAX_DATABAG_DEPTH = 5
7173MAX_DATABAG_BREADTH = 10
7274CYCLE_MARKER = "<cyclic>"
@@ -118,6 +120,8 @@ def serialize(event, **kwargs):
118120 path = [] # type: List[Segment]
119121 meta_stack = [] # type: List[Dict[str, Any]]
120122
123+ keep_request_bodies = kwargs .pop ("request_bodies" , None ) == "always" # type: bool
124+
121125 def _annotate (** meta ):
122126 # type: (**Any) -> None
123127 while len (meta_stack ) <= len (path ):
@@ -182,10 +186,11 @@ def _is_databag():
182186 if rv in (True , None ):
183187 return rv
184188
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
188192
193+ p0 = path [0 ]
189194 if p0 == "breadcrumbs" and path [1 ] == "values" :
190195 path [2 ]
191196 return True
@@ -198,13 +203,24 @@ def _is_databag():
198203
199204 return False
200205
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+
201216 def _serialize_node (
202217 obj , # type: Any
203218 is_databag = None , # type: Optional[bool]
219+ is_request_body = None , # type: Optional[bool]
204220 should_repr_strings = None , # type: Optional[bool]
205221 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] ]
208224 ):
209225 # type: (...) -> Any
210226 if segment is not None :
@@ -218,6 +234,7 @@ def _serialize_node(
218234 return _serialize_node_impl (
219235 obj ,
220236 is_databag = is_databag ,
237+ is_request_body = is_request_body ,
221238 should_repr_strings = should_repr_strings ,
222239 remaining_depth = remaining_depth ,
223240 remaining_breadth = remaining_breadth ,
@@ -242,9 +259,14 @@ def _flatten_annotated(obj):
242259 return obj
243260
244261 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 ,
246268 ):
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
248270 if isinstance (obj , AnnotatedValue ):
249271 should_repr_strings = False
250272 if should_repr_strings is None :
@@ -253,10 +275,18 @@ def _serialize_node_impl(
253275 if is_databag is None :
254276 is_databag = _is_databag ()
255277
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
260290
261291 obj = _flatten_annotated (obj )
262292
@@ -312,6 +342,7 @@ def _serialize_node_impl(
312342 segment = str_k ,
313343 should_repr_strings = should_repr_strings ,
314344 is_databag = is_databag ,
345+ is_request_body = is_request_body ,
315346 remaining_depth = remaining_depth - 1
316347 if remaining_depth is not None
317348 else None ,
@@ -338,6 +369,7 @@ def _serialize_node_impl(
338369 segment = i ,
339370 should_repr_strings = should_repr_strings ,
340371 is_databag = is_databag ,
372+ is_request_body = is_request_body ,
341373 remaining_depth = remaining_depth - 1
342374 if remaining_depth is not None
343375 else None ,
0 commit comments