Skip to content

Commit e4f30c3

Browse files
committed
Remove _should_repr_strings and document params
1 parent ae7621c commit e4f30c3

File tree

1 file changed

+23
-28
lines changed

1 file changed

+23
-28
lines changed

sentry_sdk/serializer.py

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,24 @@ def __exit__(
9696

9797
def serialize(event, **kwargs):
9898
# type: (Dict[str, Any], **Any) -> Dict[str, Any]
99+
"""
100+
A very smart serializer that takes a dict and emits a json-friendly dict.
101+
Currently used for serializing the final Event and also prematurely while fetching the stack
102+
local variables for each frame in a stacktrace.
103+
104+
It works internally with 'databags' which are arbitrary data structures like Mapping, Sequence and Set.
105+
The algorithm itself is a recursive graph walk down the data structures it encounters.
106+
107+
It has the following responsibilities:
108+
* Trimming databags and keeping them withing MAX_DATABAG_BREADTH and MAX_DATABAG_DEPTH.
109+
* Calling safe_repr() on objects appropriately to keep them informative and readable in the final payload.
110+
* Annotating the payload with the _meta field whenever trimming happens.
111+
112+
:param max_request_body_size: If set to "always", will never trim request bodies.
113+
:param max_value_length: The max length to strip strings to, defaults to sentry_sdk.consts.DEFAULT_MAX_VALUE_LENGTH
114+
:param is_vars: If we're serializing vars early, we want to repr() things that are JSON-serializable to make their type more apparent. For example, it's useful to see the difference between a unicode-string and a bytestring when viewing a stacktrace.
115+
116+
"""
99117
memo = Memo()
100118
path = [] # type: List[Segment]
101119
meta_stack = [] # type: List[Dict[str, Any]]
@@ -119,40 +137,17 @@ def _annotate(**meta):
119137

120138
meta_stack[-1].setdefault("", {}).update(meta)
121139

122-
def _should_repr_strings():
123-
# type: () -> Optional[bool]
124-
"""
125-
By default non-serializable objects are going through
126-
safe_repr(). For certain places in the event (local vars) we
127-
want to repr() even things that are JSON-serializable to
128-
make their type more apparent. For example, it's useful to
129-
see the difference between a unicode-string and a bytestring
130-
when viewing a stacktrace.
131-
132-
For container-types we still don't do anything different.
133-
Generally we just try to make the Sentry UI present exactly
134-
what a pretty-printed repr would look like.
135-
136-
:returns: `True` if we are somewhere in frame variables, and `False` if
137-
we are in a position where we will never encounter frame variables
138-
when recursing (for example, we're in `event.extra`). `None` if we
139-
are not (yet) in frame variables, but might encounter them when
140-
recursing (e.g. we're in `event.exception`)
141-
"""
142-
return is_vars
143-
144140
def _is_databag():
145141
# type: () -> Optional[bool]
146142
"""
147143
A databag is any value that we need to trim.
144+
True for stuff like vars, request bodies, breadcrumbs and extra.
148145
149-
:returns: Works like `_should_repr_strings()`. `True` for "yes",
150-
`False` for :"no", `None` for "maybe soon".
146+
:returns: `True` for "yes", `False` for :"no", `None` for "maybe soon".
151147
"""
152148
try:
153-
rv = _should_repr_strings()
154-
if rv in (True, None):
155-
return rv
149+
if is_vars:
150+
return True
156151

157152
is_request_body = _is_request_body()
158153
if is_request_body in (True, None):
@@ -238,7 +233,7 @@ def _serialize_node_impl(
238233
if isinstance(obj, AnnotatedValue):
239234
should_repr_strings = False
240235
if should_repr_strings is None:
241-
should_repr_strings = _should_repr_strings()
236+
should_repr_strings = is_vars
242237

243238
if is_databag is None:
244239
is_databag = _is_databag()

0 commit comments

Comments
 (0)