Skip to content

Commit 4f9d326

Browse files
authored
Considerably raise DEFAULT_MAX_VALUE_LENGTH (#4632)
AI prompts/messages are potentially huge. * raise `DEFAULT_MAX_VALUE_LENGTH` (responsible for string trimming) from 1024 to 100 000 * adapt tests (and make them more generic, without hardcoded parts, where possible)
1 parent fd7dca4 commit 4f9d326

File tree

8 files changed

+115
-41
lines changed

8 files changed

+115
-41
lines changed

sentry_sdk/consts.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
from typing import TYPE_CHECKING
44

55
# up top to prevent circular import due to integration import
6-
DEFAULT_MAX_VALUE_LENGTH = 1024
6+
# This is more or less an arbitrary large-ish value for now, so that we allow
7+
# pretty long strings (like LLM prompts), but still have *some* upper limit
8+
# until we verify that removing the trimming completely is safe.
9+
DEFAULT_MAX_VALUE_LENGTH = 100_000
710

811
DEFAULT_MAX_STACK_FRAMES = 100
912
DEFAULT_ADD_FULL_STACK = False

tests/integrations/bottle/test_bottle.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from io import BytesIO
66
from bottle import Bottle, debug as set_debug, abort, redirect, HTTPResponse
77
from sentry_sdk import capture_message
8+
from sentry_sdk.consts import DEFAULT_MAX_VALUE_LENGTH
89
from sentry_sdk.integrations.bottle import BottleIntegration
910
from sentry_sdk.serializer import MAX_DATABAG_BREADTH
1011

@@ -121,9 +122,9 @@ def index():
121122

122123

123124
def test_large_json_request(sentry_init, capture_events, app, get_client):
124-
sentry_init(integrations=[BottleIntegration()])
125+
sentry_init(integrations=[BottleIntegration()], max_request_body_size="always")
125126

126-
data = {"foo": {"bar": "a" * 2000}}
127+
data = {"foo": {"bar": "a" * (DEFAULT_MAX_VALUE_LENGTH + 10)}}
127128

128129
@app.route("/", method="POST")
129130
def index():
@@ -144,9 +145,14 @@ def index():
144145

145146
(event,) = events
146147
assert event["_meta"]["request"]["data"]["foo"]["bar"] == {
147-
"": {"len": 2000, "rem": [["!limit", "x", 1021, 1024]]}
148+
"": {
149+
"len": DEFAULT_MAX_VALUE_LENGTH + 10,
150+
"rem": [
151+
["!limit", "x", DEFAULT_MAX_VALUE_LENGTH - 3, DEFAULT_MAX_VALUE_LENGTH]
152+
],
153+
}
148154
}
149-
assert len(event["request"]["data"]["foo"]["bar"]) == 1024
155+
assert len(event["request"]["data"]["foo"]["bar"]) == DEFAULT_MAX_VALUE_LENGTH
150156

151157

152158
@pytest.mark.parametrize("data", [{}, []], ids=["empty-dict", "empty-list"])
@@ -174,9 +180,9 @@ def index():
174180

175181

176182
def test_medium_formdata_request(sentry_init, capture_events, app, get_client):
177-
sentry_init(integrations=[BottleIntegration()])
183+
sentry_init(integrations=[BottleIntegration()], max_request_body_size="always")
178184

179-
data = {"foo": "a" * 2000}
185+
data = {"foo": "a" * (DEFAULT_MAX_VALUE_LENGTH + 10)}
180186

181187
@app.route("/", method="POST")
182188
def index():
@@ -194,9 +200,14 @@ def index():
194200

195201
(event,) = events
196202
assert event["_meta"]["request"]["data"]["foo"] == {
197-
"": {"len": 2000, "rem": [["!limit", "x", 1021, 1024]]}
203+
"": {
204+
"len": DEFAULT_MAX_VALUE_LENGTH + 10,
205+
"rem": [
206+
["!limit", "x", DEFAULT_MAX_VALUE_LENGTH - 3, DEFAULT_MAX_VALUE_LENGTH]
207+
],
208+
}
198209
}
199-
assert len(event["request"]["data"]["foo"]) == 1024
210+
assert len(event["request"]["data"]["foo"]) == DEFAULT_MAX_VALUE_LENGTH
200211

201212

202213
@pytest.mark.parametrize("input_char", ["a", b"a"])
@@ -233,7 +244,10 @@ def index():
233244
def test_files_and_form(sentry_init, capture_events, app, get_client):
234245
sentry_init(integrations=[BottleIntegration()], max_request_body_size="always")
235246

236-
data = {"foo": "a" * 2000, "file": (BytesIO(b"hello"), "hello.txt")}
247+
data = {
248+
"foo": "a" * (DEFAULT_MAX_VALUE_LENGTH + 10),
249+
"file": (BytesIO(b"hello"), "hello.txt"),
250+
}
237251

238252
@app.route("/", method="POST")
239253
def index():
@@ -253,9 +267,14 @@ def index():
253267

254268
(event,) = events
255269
assert event["_meta"]["request"]["data"]["foo"] == {
256-
"": {"len": 2000, "rem": [["!limit", "x", 1021, 1024]]}
270+
"": {
271+
"len": DEFAULT_MAX_VALUE_LENGTH + 10,
272+
"rem": [
273+
["!limit", "x", DEFAULT_MAX_VALUE_LENGTH - 3, DEFAULT_MAX_VALUE_LENGTH]
274+
],
275+
}
257276
}
258-
assert len(event["request"]["data"]["foo"]) == 1024
277+
assert len(event["request"]["data"]["foo"]) == DEFAULT_MAX_VALUE_LENGTH
259278

260279
assert event["_meta"]["request"]["data"]["file"] == {
261280
"": {

tests/integrations/falcon/test_falcon.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import falcon
66
import falcon.testing
77
import sentry_sdk
8+
from sentry_sdk.consts import DEFAULT_MAX_VALUE_LENGTH
89
from sentry_sdk.integrations.falcon import FalconIntegration
910
from sentry_sdk.integrations.logging import LoggingIntegration
1011
from sentry_sdk.utils import parse_version
@@ -207,9 +208,9 @@ def on_get(self, req, resp):
207208

208209

209210
def test_falcon_large_json_request(sentry_init, capture_events):
210-
sentry_init(integrations=[FalconIntegration()])
211+
sentry_init(integrations=[FalconIntegration()], max_request_body_size="always")
211212

212-
data = {"foo": {"bar": "a" * 2000}}
213+
data = {"foo": {"bar": "a" * (DEFAULT_MAX_VALUE_LENGTH + 10)}}
213214

214215
class Resource:
215216
def on_post(self, req, resp):
@@ -228,9 +229,14 @@ def on_post(self, req, resp):
228229

229230
(event,) = events
230231
assert event["_meta"]["request"]["data"]["foo"]["bar"] == {
231-
"": {"len": 2000, "rem": [["!limit", "x", 1021, 1024]]}
232+
"": {
233+
"len": DEFAULT_MAX_VALUE_LENGTH + 10,
234+
"rem": [
235+
["!limit", "x", DEFAULT_MAX_VALUE_LENGTH - 3, DEFAULT_MAX_VALUE_LENGTH]
236+
],
237+
}
232238
}
233-
assert len(event["request"]["data"]["foo"]["bar"]) == 1024
239+
assert len(event["request"]["data"]["foo"]["bar"]) == DEFAULT_MAX_VALUE_LENGTH
234240

235241

236242
@pytest.mark.parametrize("data", [{}, []], ids=["empty-dict", "empty-list"])

tests/integrations/flask/test_flask.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
capture_message,
2828
capture_exception,
2929
)
30+
from sentry_sdk.consts import DEFAULT_MAX_VALUE_LENGTH
3031
from sentry_sdk.integrations.logging import LoggingIntegration
3132
from sentry_sdk.serializer import MAX_DATABAG_BREADTH
3233

@@ -248,9 +249,11 @@ def login():
248249

249250

250251
def test_flask_large_json_request(sentry_init, capture_events, app):
251-
sentry_init(integrations=[flask_sentry.FlaskIntegration()])
252+
sentry_init(
253+
integrations=[flask_sentry.FlaskIntegration()], max_request_body_size="always"
254+
)
252255

253-
data = {"foo": {"bar": "a" * 2000}}
256+
data = {"foo": {"bar": "a" * (DEFAULT_MAX_VALUE_LENGTH + 10)}}
254257

255258
@app.route("/", methods=["POST"])
256259
def index():
@@ -268,9 +271,14 @@ def index():
268271

269272
(event,) = events
270273
assert event["_meta"]["request"]["data"]["foo"]["bar"] == {
271-
"": {"len": 2000, "rem": [["!limit", "x", 1021, 1024]]}
274+
"": {
275+
"len": DEFAULT_MAX_VALUE_LENGTH + 10,
276+
"rem": [
277+
["!limit", "x", DEFAULT_MAX_VALUE_LENGTH - 3, DEFAULT_MAX_VALUE_LENGTH]
278+
],
279+
}
272280
}
273-
assert len(event["request"]["data"]["foo"]["bar"]) == 1024
281+
assert len(event["request"]["data"]["foo"]["bar"]) == DEFAULT_MAX_VALUE_LENGTH
274282

275283

276284
def test_flask_session_tracking(sentry_init, capture_envelopes, app):
@@ -336,9 +344,11 @@ def index():
336344

337345

338346
def test_flask_medium_formdata_request(sentry_init, capture_events, app):
339-
sentry_init(integrations=[flask_sentry.FlaskIntegration()])
347+
sentry_init(
348+
integrations=[flask_sentry.FlaskIntegration()], max_request_body_size="always"
349+
)
340350

341-
data = {"foo": "a" * 2000}
351+
data = {"foo": "a" * (DEFAULT_MAX_VALUE_LENGTH + 10)}
342352

343353
@app.route("/", methods=["POST"])
344354
def index():
@@ -360,9 +370,14 @@ def index():
360370

361371
(event,) = events
362372
assert event["_meta"]["request"]["data"]["foo"] == {
363-
"": {"len": 2000, "rem": [["!limit", "x", 1021, 1024]]}
373+
"": {
374+
"len": DEFAULT_MAX_VALUE_LENGTH + 10,
375+
"rem": [
376+
["!limit", "x", DEFAULT_MAX_VALUE_LENGTH - 3, DEFAULT_MAX_VALUE_LENGTH]
377+
],
378+
}
364379
}
365-
assert len(event["request"]["data"]["foo"]) == 1024
380+
assert len(event["request"]["data"]["foo"]) == DEFAULT_MAX_VALUE_LENGTH
366381

367382

368383
def test_flask_formdata_request_appear_transaction_body(
@@ -441,7 +456,10 @@ def test_flask_files_and_form(sentry_init, capture_events, app):
441456
integrations=[flask_sentry.FlaskIntegration()], max_request_body_size="always"
442457
)
443458

444-
data = {"foo": "a" * 2000, "file": (BytesIO(b"hello"), "hello.txt")}
459+
data = {
460+
"foo": "a" * (DEFAULT_MAX_VALUE_LENGTH + 10),
461+
"file": (BytesIO(b"hello"), "hello.txt"),
462+
}
445463

446464
@app.route("/", methods=["POST"])
447465
def index():
@@ -463,9 +481,14 @@ def index():
463481

464482
(event,) = events
465483
assert event["_meta"]["request"]["data"]["foo"] == {
466-
"": {"len": 2000, "rem": [["!limit", "x", 1021, 1024]]}
484+
"": {
485+
"len": DEFAULT_MAX_VALUE_LENGTH + 10,
486+
"rem": [
487+
["!limit", "x", DEFAULT_MAX_VALUE_LENGTH - 3, DEFAULT_MAX_VALUE_LENGTH]
488+
],
489+
}
467490
}
468-
assert len(event["request"]["data"]["foo"]) == 1024
491+
assert len(event["request"]["data"]["foo"]) == DEFAULT_MAX_VALUE_LENGTH
469492

470493
assert event["_meta"]["request"]["data"]["file"] == {"": {"rem": [["!raw", "x"]]}}
471494
assert not event["request"]["data"]["file"]

tests/integrations/pyramid/test_pyramid.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from werkzeug.test import Client
1010

1111
from sentry_sdk import capture_message, add_breadcrumb
12+
from sentry_sdk.consts import DEFAULT_MAX_VALUE_LENGTH
1213
from sentry_sdk.integrations.pyramid import PyramidIntegration
1314
from sentry_sdk.serializer import MAX_DATABAG_BREADTH
1415
from tests.conftest import unpack_werkzeug_response
@@ -156,9 +157,9 @@ def test_transaction_style(
156157

157158

158159
def test_large_json_request(sentry_init, capture_events, route, get_client):
159-
sentry_init(integrations=[PyramidIntegration()])
160+
sentry_init(integrations=[PyramidIntegration()], max_request_body_size="always")
160161

161-
data = {"foo": {"bar": "a" * 2000}}
162+
data = {"foo": {"bar": "a" * (DEFAULT_MAX_VALUE_LENGTH + 10)}}
162163

163164
@route("/")
164165
def index(request):
@@ -175,9 +176,14 @@ def index(request):
175176

176177
(event,) = events
177178
assert event["_meta"]["request"]["data"]["foo"]["bar"] == {
178-
"": {"len": 2000, "rem": [["!limit", "x", 1021, 1024]]}
179+
"": {
180+
"len": DEFAULT_MAX_VALUE_LENGTH + 10,
181+
"rem": [
182+
["!limit", "x", DEFAULT_MAX_VALUE_LENGTH - 3, DEFAULT_MAX_VALUE_LENGTH]
183+
],
184+
}
179185
}
180-
assert len(event["request"]["data"]["foo"]["bar"]) == 1024
186+
assert len(event["request"]["data"]["foo"]["bar"]) == DEFAULT_MAX_VALUE_LENGTH
181187

182188

183189
@pytest.mark.parametrize("data", [{}, []], ids=["empty-dict", "empty-list"])
@@ -230,7 +236,10 @@ def index(request):
230236
def test_files_and_form(sentry_init, capture_events, route, get_client):
231237
sentry_init(integrations=[PyramidIntegration()], max_request_body_size="always")
232238

233-
data = {"foo": "a" * 2000, "file": (BytesIO(b"hello"), "hello.txt")}
239+
data = {
240+
"foo": "a" * (DEFAULT_MAX_VALUE_LENGTH + 10),
241+
"file": (BytesIO(b"hello"), "hello.txt"),
242+
}
234243

235244
@route("/")
236245
def index(request):
@@ -244,9 +253,14 @@ def index(request):
244253

245254
(event,) = events
246255
assert event["_meta"]["request"]["data"]["foo"] == {
247-
"": {"len": 2000, "rem": [["!limit", "x", 1021, 1024]]}
256+
"": {
257+
"len": DEFAULT_MAX_VALUE_LENGTH + 10,
258+
"rem": [
259+
["!limit", "x", DEFAULT_MAX_VALUE_LENGTH - 3, DEFAULT_MAX_VALUE_LENGTH]
260+
],
261+
}
248262
}
249-
assert len(event["request"]["data"]["foo"]) == 1024
263+
assert len(event["request"]["data"]["foo"]) == DEFAULT_MAX_VALUE_LENGTH
250264

251265
assert event["_meta"]["request"]["data"]["file"] == {"": {"rem": [["!raw", "x"]]}}
252266
assert not event["request"]["data"]["file"]

tests/integrations/sqlalchemy/test_sqlalchemy.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,12 @@ def processor(event, hint):
275275

276276
# The _meta for other truncated fields should be there as well.
277277
assert event["_meta"]["message"] == {
278-
"": {"len": 1034, "rem": [["!limit", "x", 1021, 1024]]}
278+
"": {
279+
"len": DEFAULT_MAX_VALUE_LENGTH + 10,
280+
"rem": [
281+
["!limit", "x", DEFAULT_MAX_VALUE_LENGTH - 3, DEFAULT_MAX_VALUE_LENGTH]
282+
],
283+
}
279284
}
280285

281286

tests/test_client.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -773,14 +773,14 @@ def test_databag_string_stripping(sentry_init, capture_events, benchmark):
773773
def inner():
774774
del events[:]
775775
try:
776-
a = "A" * 1000000 # noqa
776+
a = "A" * DEFAULT_MAX_VALUE_LENGTH * 10 # noqa
777777
1 / 0
778778
except Exception:
779779
capture_exception()
780780

781781
(event,) = events
782782

783-
assert len(json.dumps(event)) < 10000
783+
assert len(json.dumps(event)) < DEFAULT_MAX_VALUE_LENGTH * 10
784784

785785

786786
def test_databag_breadth_stripping(sentry_init, capture_events, benchmark):
@@ -1073,7 +1073,10 @@ def test_multiple_positional_args(sentry_init):
10731073
"sdk_options, expected_data_length",
10741074
[
10751075
({}, DEFAULT_MAX_VALUE_LENGTH),
1076-
({"max_value_length": 1800}, 1800),
1076+
(
1077+
{"max_value_length": DEFAULT_MAX_VALUE_LENGTH + 1000},
1078+
DEFAULT_MAX_VALUE_LENGTH + 1000,
1079+
),
10771080
],
10781081
)
10791082
def test_max_value_length_option(
@@ -1082,7 +1085,7 @@ def test_max_value_length_option(
10821085
sentry_init(sdk_options)
10831086
events = capture_events()
10841087

1085-
capture_message("a" * 2000)
1088+
capture_message("a" * (DEFAULT_MAX_VALUE_LENGTH + 2000))
10861089

10871090
assert len(events[0]["message"]) == expected_data_length
10881091

tests/test_serializer.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pytest
44

5+
from sentry_sdk.consts import DEFAULT_MAX_VALUE_LENGTH
56
from sentry_sdk.serializer import MAX_DATABAG_BREADTH, MAX_DATABAG_DEPTH, serialize
67

78
try:
@@ -166,11 +167,11 @@ def test_no_trimming_if_max_request_body_size_is_always(body_normalizer):
166167

167168

168169
def test_max_value_length_default(body_normalizer):
169-
data = {"key": "a" * 2000}
170+
data = {"key": "a" * (DEFAULT_MAX_VALUE_LENGTH * 10)}
170171

171172
result = body_normalizer(data)
172173

173-
assert len(result["key"]) == 1024 # fallback max length
174+
assert len(result["key"]) == DEFAULT_MAX_VALUE_LENGTH # fallback max length
174175

175176

176177
def test_max_value_length(body_normalizer):

0 commit comments

Comments
 (0)