Skip to content

Commit

Permalink
Read MAX_STRING_LENGTH from client options (getsentry#2121)
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Uittenbroek committed Jun 14, 2023
1 parent e833825 commit fba3fc2
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 15 deletions.
5 changes: 4 additions & 1 deletion sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,10 @@ def _prepare_event(
"values": [
{
"stacktrace": current_stacktrace(
self.options["include_local_variables"]
self.options["max_string_length"],
include_local_variables=self.options[
"include_local_variables"
],
),
"crashed": False,
"current": True,
Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ def __init__(
], # type: Optional[Sequence[str]]
functions_to_trace=[], # type: Sequence[Dict[str, str]] # noqa: B006
event_scrubber=None, # type: Optional[sentry_sdk.scrubber.EventScrubber]
max_string_length=1024, # type: int
):
# type: (...) -> None
pass
Expand Down
5 changes: 4 additions & 1 deletion sentry_sdk/integrations/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ def _emit(self, record):
"values": [
{
"stacktrace": current_stacktrace(
client_options["include_local_variables"]
client_options["max_string_length"],
include_local_variables=client_options[
"include_local_variables"
],
),
"crashed": False,
"current": True,
Expand Down
7 changes: 5 additions & 2 deletions sentry_sdk/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def serialize(event, **kwargs):
meta_stack = [] # type: List[Dict[str, Any]]

keep_request_bodies = kwargs.pop("request_bodies", None) == "always" # type: bool
max_string_length = kwargs.pop("max_string_length", None) # type: Optional[int]

def _annotate(**meta):
# type: (**Any) -> None
Expand Down Expand Up @@ -293,7 +294,9 @@ def _serialize_node_impl(
if remaining_depth is not None and remaining_depth <= 0:
_annotate(rem=[["!limit", "x"]])
if is_databag:
return _flatten_annotated(strip_string(safe_repr(obj)))
return _flatten_annotated(
strip_string(safe_repr(obj), max_length=max_string_length)
)
return None

if is_databag and global_repr_processors:
Expand Down Expand Up @@ -394,7 +397,7 @@ def _serialize_node_impl(
if is_span_description:
return obj

return _flatten_annotated(strip_string(obj))
return _flatten_annotated(strip_string(obj, max_string_length))

#
# Start of serialize() function
Expand Down
34 changes: 23 additions & 11 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ def iter_stacks(tb):
def get_lines_from_file(
filename, # type: str
lineno, # type: int
max_length, # type: int
loader=None, # type: Optional[Any]
module=None, # type: Optional[str]
):
Expand Down Expand Up @@ -496,11 +497,12 @@ def get_lines_from_file(

try:
pre_context = [
strip_string(line.strip("\r\n")) for line in source[lower_bound:lineno]
strip_string(line.strip("\r\n"), max_length=max_length)
for line in source[lower_bound:lineno]
]
context_line = strip_string(source[lineno].strip("\r\n"))
context_line = strip_string(source[lineno].strip("\r\n"), max_length=max_length)
post_context = [
strip_string(line.strip("\r\n"))
strip_string(line.strip("\r\n"), max_length=max_length)
for line in source[(lineno + 1) : upper_bound]
]
return pre_context, context_line, post_context
Expand All @@ -512,6 +514,7 @@ def get_lines_from_file(
def get_source_context(
frame, # type: FrameType
tb_lineno, # type: int
max_string_length, # type: int
):
# type: (...) -> Tuple[List[Annotated[str]], Optional[Annotated[str]], List[Annotated[str]]]
try:
Expand All @@ -528,7 +531,9 @@ def get_source_context(
loader = None
lineno = tb_lineno - 1
if lineno is not None and abs_path:
return get_lines_from_file(abs_path, lineno, loader, module)
return get_lines_from_file(
abs_path, lineno, max_string_length, loader=loader, module=module
)
return [], None, []


Expand Down Expand Up @@ -602,7 +607,11 @@ def filename_for_module(module, abs_path):


def serialize_frame(
frame, tb_lineno=None, include_local_variables=True, include_source_context=True
frame,
tb_lineno=None,
include_local_variables=True,
include_source_context=True,
max_string_length=None,
):
# type: (FrameType, Optional[int], bool, bool) -> Dict[str, Any]
f_code = getattr(frame, "f_code", None)
Expand Down Expand Up @@ -630,7 +639,7 @@ def serialize_frame(

if include_source_context:
rv["pre_context"], rv["context_line"], rv["post_context"] = get_source_context(
frame, tb_lineno
frame, tb_lineno, max_string_length
)

if include_local_variables:
Expand All @@ -639,7 +648,9 @@ def serialize_frame(
return rv


def current_stacktrace(include_local_variables=True, include_source_context=True):
def current_stacktrace(
max_string_length, include_local_variables=True, include_source_context=True
):
# type: (bool, bool) -> Any
__tracebackhide__ = True
frames = []
Expand All @@ -652,6 +663,7 @@ def current_stacktrace(include_local_variables=True, include_source_context=True
f,
include_local_variables=include_local_variables,
include_source_context=include_source_context,
max_string_length=max_string_length,
)
)
f = f.f_back
Expand Down Expand Up @@ -724,16 +736,19 @@ def single_exception_from_error_tuple(
if client_options is None:
include_local_variables = True
include_source_context = True
max_string_length = 1024
else:
include_local_variables = client_options["include_local_variables"]
include_source_context = client_options["include_source_context"]
max_string_length = client_options["max_string_length"]

frames = [
serialize_frame(
tb.tb_frame,
tb_lineno=tb.tb_lineno,
include_local_variables=include_local_variables,
include_source_context=include_source_context,
max_string_length=max_string_length,
)
for tb in iter_stacks(tb)
]
Expand Down Expand Up @@ -819,9 +834,7 @@ def exceptions_from_error(
parent_id = exception_id
exception_id += 1

should_supress_context = (
hasattr(exc_value, "__suppress_context__") and exc_value.__suppress_context__ # type: ignore
)
should_supress_context = hasattr(exc_value, "__suppress_context__") and exc_value.__suppress_context__ # type: ignore
if should_supress_context:
# Add direct cause.
# The field `__cause__` is set when raised with the exception (using the `from` keyword).
Expand Down Expand Up @@ -1082,7 +1095,6 @@ def _is_in_project_root(abs_path, project_root):

def strip_string(value, max_length=None):
# type: (str, Optional[int]) -> Union[AnnotatedValue, str]
# TODO: read max_length from config
if not value:
return value

Expand Down
17 changes: 17 additions & 0 deletions tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,20 @@ def test_no_trimming_if_request_bodies_is_always(body_normalizer):
result = body_normalizer(data, request_bodies="always")

assert result == data


def test_max_string_length_default(body_normalizer):
data = {"key": "a" * 2000}

result = body_normalizer(data)

assert len(result["key"]) == 1024 # fallback max length


def test_max_string_length(body_normalizer):
data = {"key": "a" * 2000}

max_string_length = 2000
result = body_normalizer(data, max_string_length=max_string_length)

assert len(result["key"]) == max_string_length

0 comments on commit fba3fc2

Please sign in to comment.