Skip to content

Commit a1e7473

Browse files
author
Peter Uittenbroek
committed
Read MAX_STRING_LENGTH from client options (#2121)
1 parent e833825 commit a1e7473

File tree

6 files changed

+54
-15
lines changed

6 files changed

+54
-15
lines changed

sentry_sdk/client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,10 @@ def _prepare_event(
286286
"values": [
287287
{
288288
"stacktrace": current_stacktrace(
289-
self.options["include_local_variables"]
289+
self.options["max_string_length"],
290+
include_local_variables=self.options[
291+
"include_local_variables"
292+
],
290293
),
291294
"crashed": False,
292295
"current": True,

sentry_sdk/consts.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ def __init__(
197197
], # type: Optional[Sequence[str]]
198198
functions_to_trace=[], # type: Sequence[Dict[str, str]] # noqa: B006
199199
event_scrubber=None, # type: Optional[sentry_sdk.scrubber.EventScrubber]
200+
max_string_length=1024, # type: int
200201
):
201202
# type: (...) -> None
202203
pass

sentry_sdk/integrations/logging.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,10 @@ def _emit(self, record):
205205
"values": [
206206
{
207207
"stacktrace": current_stacktrace(
208-
client_options["include_local_variables"]
208+
client_options["max_string_length"],
209+
include_local_variables=client_options[
210+
"include_local_variables"
211+
],
209212
),
210213
"crashed": False,
211214
"current": True,

sentry_sdk/serializer.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def serialize(event, **kwargs):
121121
meta_stack = [] # type: List[Dict[str, Any]]
122122

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

125126
def _annotate(**meta):
126127
# type: (**Any) -> None
@@ -293,7 +294,9 @@ def _serialize_node_impl(
293294
if remaining_depth is not None and remaining_depth <= 0:
294295
_annotate(rem=[["!limit", "x"]])
295296
if is_databag:
296-
return _flatten_annotated(strip_string(safe_repr(obj)))
297+
return _flatten_annotated(
298+
strip_string(safe_repr(obj), max_length=max_string_length)
299+
)
297300
return None
298301

299302
if is_databag and global_repr_processors:
@@ -394,7 +397,7 @@ def _serialize_node_impl(
394397
if is_span_description:
395398
return obj
396399

397-
return _flatten_annotated(strip_string(obj))
400+
return _flatten_annotated(strip_string(obj, max_string_length))
398401

399402
#
400403
# Start of serialize() function

sentry_sdk/utils.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ def iter_stacks(tb):
468468
def get_lines_from_file(
469469
filename, # type: str
470470
lineno, # type: int
471+
max_length, # type: int
471472
loader=None, # type: Optional[Any]
472473
module=None, # type: Optional[str]
473474
):
@@ -496,11 +497,12 @@ def get_lines_from_file(
496497

497498
try:
498499
pre_context = [
499-
strip_string(line.strip("\r\n")) for line in source[lower_bound:lineno]
500+
strip_string(line.strip("\r\n"), max_length=max_length)
501+
for line in source[lower_bound:lineno]
500502
]
501-
context_line = strip_string(source[lineno].strip("\r\n"))
503+
context_line = strip_string(source[lineno].strip("\r\n"), max_length=max_length)
502504
post_context = [
503-
strip_string(line.strip("\r\n"))
505+
strip_string(line.strip("\r\n"), max_length=max_length)
504506
for line in source[(lineno + 1) : upper_bound]
505507
]
506508
return pre_context, context_line, post_context
@@ -512,6 +514,7 @@ def get_lines_from_file(
512514
def get_source_context(
513515
frame, # type: FrameType
514516
tb_lineno, # type: int
517+
max_string_length, # type: int
515518
):
516519
# type: (...) -> Tuple[List[Annotated[str]], Optional[Annotated[str]], List[Annotated[str]]]
517520
try:
@@ -528,7 +531,9 @@ def get_source_context(
528531
loader = None
529532
lineno = tb_lineno - 1
530533
if lineno is not None and abs_path:
531-
return get_lines_from_file(abs_path, lineno, loader, module)
534+
return get_lines_from_file(
535+
abs_path, lineno, max_string_length, loader=loader, module=module
536+
)
532537
return [], None, []
533538

534539

@@ -602,7 +607,11 @@ def filename_for_module(module, abs_path):
602607

603608

604609
def serialize_frame(
605-
frame, tb_lineno=None, include_local_variables=True, include_source_context=True
610+
frame,
611+
tb_lineno=None,
612+
include_local_variables=True,
613+
include_source_context=True,
614+
max_string_length=None,
606615
):
607616
# type: (FrameType, Optional[int], bool, bool) -> Dict[str, Any]
608617
f_code = getattr(frame, "f_code", None)
@@ -630,7 +639,7 @@ def serialize_frame(
630639

631640
if include_source_context:
632641
rv["pre_context"], rv["context_line"], rv["post_context"] = get_source_context(
633-
frame, tb_lineno
642+
frame, tb_lineno, max_string_length
634643
)
635644

636645
if include_local_variables:
@@ -639,7 +648,9 @@ def serialize_frame(
639648
return rv
640649

641650

642-
def current_stacktrace(include_local_variables=True, include_source_context=True):
651+
def current_stacktrace(
652+
max_string_length, include_local_variables=True, include_source_context=True
653+
):
643654
# type: (bool, bool) -> Any
644655
__tracebackhide__ = True
645656
frames = []
@@ -652,6 +663,7 @@ def current_stacktrace(include_local_variables=True, include_source_context=True
652663
f,
653664
include_local_variables=include_local_variables,
654665
include_source_context=include_source_context,
666+
max_string_length=max_string_length,
655667
)
656668
)
657669
f = f.f_back
@@ -724,16 +736,19 @@ def single_exception_from_error_tuple(
724736
if client_options is None:
725737
include_local_variables = True
726738
include_source_context = True
739+
max_string_length = MAX_STRING_LENGTH # fallback
727740
else:
728741
include_local_variables = client_options["include_local_variables"]
729742
include_source_context = client_options["include_source_context"]
743+
max_string_length = client_options["max_string_length"]
730744

731745
frames = [
732746
serialize_frame(
733747
tb.tb_frame,
734748
tb_lineno=tb.tb_lineno,
735749
include_local_variables=include_local_variables,
736750
include_source_context=include_source_context,
751+
max_string_length=max_string_length,
737752
)
738753
for tb in iter_stacks(tb)
739754
]
@@ -819,9 +834,7 @@ def exceptions_from_error(
819834
parent_id = exception_id
820835
exception_id += 1
821836

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

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

tests/test_serializer.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,20 @@ def test_no_trimming_if_request_bodies_is_always(body_normalizer):
144144
result = body_normalizer(data, request_bodies="always")
145145

146146
assert result == data
147+
148+
149+
def test_max_string_length_default(body_normalizer):
150+
data = {"key": "a" * 2000}
151+
152+
result = body_normalizer(data)
153+
154+
assert len(result["key"]) == 1024 # fallback max length
155+
156+
157+
def test_max_string_length(body_normalizer):
158+
data = {"key": "a" * 2000}
159+
160+
max_string_length = 2000
161+
result = body_normalizer(data, max_string_length=max_string_length)
162+
163+
assert len(result["key"]) == max_string_length

0 commit comments

Comments
 (0)