Skip to content

Commit b775430

Browse files
Peter Uittenbroekpuittenbroek
authored andcommitted
Some more testing and passing max_string_length
1 parent 2580126 commit b775430

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

sentry_sdk/client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,11 @@ def _prepare_event(
323323
# Postprocess the event here so that annotated types do
324324
# generally not surface in before_send
325325
if event is not None:
326-
event = serialize(event, request_bodies=self.options.get("request_bodies"))
326+
event = serialize(
327+
event,
328+
request_bodies=self.options.get("request_bodies"),
329+
max_string_length=self.options.get("max_string_length"),
330+
)
327331

328332
before_send = self.options["before_send"]
329333
if (

sentry_sdk/consts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
DEFAULT_QUEUE_SIZE = 100
4444
DEFAULT_MAX_BREADCRUMBS = 100
45-
45+
DEFAULT_MAX_STRING_LENGTH = 1024
4646
MATCH_ALL = r".*"
4747

4848

@@ -197,7 +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
200+
max_string_length=DEFAULT_MAX_STRING_LENGTH, # type: int
201201
):
202202
# type: (...) -> None
203203
pass

sentry_sdk/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import sentry_sdk
5151
from sentry_sdk._compat import PY2, PY33, PY37, implements_str, text_type, urlparse
5252
from sentry_sdk._types import TYPE_CHECKING
53+
from sentry_sdk.consts import DEFAULT_MAX_STRING_LENGTH
5354

5455
if TYPE_CHECKING:
5556
from types import FrameType, TracebackType
@@ -75,7 +76,7 @@
7576
# The logger is created here but initialized in the debug support module
7677
logger = logging.getLogger("sentry_sdk.errors")
7778

78-
MAX_STRING_LENGTH = 1024
79+
7980
BASE64_ALPHABET = re.compile(r"^[a-zA-Z0-9/+=]*$")
8081

8182
SENSITIVE_DATA_SUBSTITUTE = "[Filtered]"
@@ -736,7 +737,7 @@ def single_exception_from_error_tuple(
736737
if client_options is None:
737738
include_local_variables = True
738739
include_source_context = True
739-
max_string_length = MAX_STRING_LENGTH # fallback
740+
max_string_length = DEFAULT_MAX_STRING_LENGTH # fallback
740741
else:
741742
include_local_variables = client_options["include_local_variables"]
742743
include_source_context = client_options["include_source_context"]
@@ -1099,8 +1100,7 @@ def strip_string(value, max_length=None):
10991100
return value
11001101

11011102
if max_length is None:
1102-
# This is intentionally not just the default such that one can patch `MAX_STRING_LENGTH` and affect `strip_string`.
1103-
max_length = MAX_STRING_LENGTH
1103+
max_length = DEFAULT_MAX_STRING_LENGTH
11041104

11051105
length = len(value.encode("utf-8"))
11061106

tests/test_client.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from sentry_sdk.utils import HAS_CHAINED_EXCEPTIONS
2525
from sentry_sdk.utils import logger
2626
from sentry_sdk.serializer import MAX_DATABAG_BREADTH
27-
from sentry_sdk.consts import DEFAULT_MAX_BREADCRUMBS
27+
from sentry_sdk.consts import DEFAULT_MAX_BREADCRUMBS, DEFAULT_MAX_STRING_LENGTH
2828

2929
try:
3030
from unittest import mock # python 3.3 and above
@@ -1029,3 +1029,18 @@ def test_multiple_positional_args(sentry_init):
10291029
with pytest.raises(TypeError) as exinfo:
10301030
sentry_init(1, None)
10311031
assert "Only single positional argument is expected" in str(exinfo.value)
1032+
1033+
1034+
@pytest.mark.parametrize(
1035+
"sdk_options, expected_data_length",
1036+
[({}, DEFAULT_MAX_STRING_LENGTH), ({"max_string_length": 2000}, 2000)],
1037+
)
1038+
def test_max_string_length_option(
1039+
sentry_init, capture_events, sdk_options, expected_data_length
1040+
):
1041+
sentry_init(sdk_options)
1042+
events = capture_events()
1043+
1044+
capture_message("a" * 2000)
1045+
1046+
assert len(events[0]["message"]) == expected_data_length

0 commit comments

Comments
 (0)