from typing import Any from typing import Callable from typing import ContextManager from typing import Dict from typing import List from typing import Optional from typing import Type from typing import Union
# Bytes are technically not strings in Python 3, but we can serialize them
serializable_str_types = (str, bytes, bytearray, memoryview)
# Maximum length of JSON-serialized event payloads that can be safely sent # before the server may reject the event due to its size. This is not intended # to reflect actual values defined server-side, but rather only be an upper # bound for events sent by the SDK. # # Can be overwritten if wanting to send more bytes, e.g. with a custom server. # When changing this, keep in mind that events may be a little bit larger than # this value due to attached metadata, so keep the number conservative.
MAX_EVENT_BYTES = 10**6
# Maximum depth and breadth of databags. Excess data will be trimmed. If # max_request_body_size is "always", request bodies won't be trimmed.
MAX_DATABAG_DEPTH = 5
MAX_DATABAG_BREADTH = 10
CYCLE_MARKER = "<cyclic>"
def serialize(event, **kwargs): # type: (Dict[str, Any], **Any) -> Dict[str, Any] """
A very smart serializer that takes a dict and emits a json-friendly dict.
Currently used for serializing the final Event and also prematurely while fetching the stack
local variables for each frame in a stacktrace.
It works internally with'databags' which are arbitrary data structures like Mapping, Sequence and Set.
The algorithm itself is a recursive graph walk down the data structures it encounters.
It has the following responsibilities:
* Trimming databags and keeping them within MAX_DATABAG_BREADTH and MAX_DATABAG_DEPTH.
* Calling safe_repr() on objects appropriately to keep them informative and readable in the final payload.
* Annotating the payload with the _meta field whenever trimming happens.
:param max_request_body_size: If set to "always", will never trim request bodies.
:param max_value_length: The max length to strip strings to, defaults to sentry_sdk.consts.DEFAULT_MAX_VALUE_LENGTH
: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.
:param custom_repr: A custom repr function that runs before safe_repr on the object to be serialized. If it returns Noneor throws internally, we will fallback to safe_repr.
def _is_databag(): # type: () -> Optional[bool] """
A databag is any value that we need to trim. Truefor stuff like vars, request bodies, breadcrumbs and extra.
:returns: `True` for"yes", `False` for :"no", `None` for"maybe soon". """ try: if is_vars: returnTrue
is_request_body = _is_request_body() if is_request_body in (True, None): return is_request_body
if is_request_body isNone:
is_request_body = _is_request_body()
if is_databag: if is_request_body and keep_request_bodies:
remaining_depth = float("inf")
remaining_breadth = float("inf") else: if remaining_depth isNone:
remaining_depth = MAX_DATABAG_DEPTH if remaining_breadth isNone:
remaining_breadth = MAX_DATABAG_BREADTH
obj = _flatten_annotated(obj)
if remaining_depth isnotNoneand remaining_depth <= 0:
_annotate(rem=[["!limit", "x"]]) if is_databag: return _flatten_annotated(
strip_string(_safe_repr_wrapper(obj), max_length=max_value_length)
) returnNone
if is_databag and global_repr_processors:
hints = {"memo": memo, "remaining_depth": remaining_depth} for processor in global_repr_processors:
result = processor(obj, hints) if result isnot NotImplemented: return _flatten_annotated(result)
if obj isNoneor isinstance(obj, (bool, int, float)): if should_repr_strings or (
isinstance(obj, float) and (math.isinf(obj) or math.isnan(obj))
): return _safe_repr_wrapper(obj) else: return obj
elif isinstance(obj, Mapping): # Create temporary copy here to avoid calling too much code that # might mutate our dictionary while we're still iterating over it.
obj = dict(obj.items())
rv_dict = {} # type: Dict[str, Any]
i = 0
for k, v in obj.items(): if remaining_breadth isnotNoneand i >= remaining_breadth:
_annotate(len=len(obj)) break
str_k = str(k)
v = _serialize_node(
v,
segment=str_k,
should_repr_strings=should_repr_strings,
is_databag=is_databag,
is_request_body=is_request_body,
remaining_depth=(
remaining_depth - 1if remaining_depth isnotNoneelseNone
),
remaining_breadth=remaining_breadth,
)
rv_dict[str_k] = v
i += 1
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.