Skip to content

Commit fb15e13

Browse files
authored
fix: Actually run tests for fast_serialize (getsentry#521)
* fix: Fix typo in conftest * fix: Fix tests * fix: Fix some more tests * fix: Fix celery tests * fix: Fix subprocess test * fix: Fix more tests
1 parent 3da615b commit fb15e13

File tree

17 files changed

+144
-95
lines changed

17 files changed

+144
-95
lines changed

sentry_sdk/hub.py

+2
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ def bind_client(
301301
"""Binds a new client to the hub."""
302302
top = self._stack[-1]
303303
self._stack[-1] = (new, top[1])
304+
if not new or new.options["_experiments"].get("fast_serialize", False):
305+
top[1].clear_breadcrumbs()
304306

305307
def capture_event(
306308
self,

sentry_sdk/integrations/_wsgi_common.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ def extract_into_event(self, event):
7373
if data is not None:
7474
request_info["data"] = data
7575

76-
event["request"] = partial_serialize(client, request_info)
76+
event["request"] = partial_serialize(
77+
client, request_info, should_repr_strings=False
78+
)
7779

7880
def content_length(self):
7981
# type: () -> int

sentry_sdk/integrations/celery.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,12 @@ def _make_event_processor(task, uuid, args, kwargs, request=None):
161161
# type: (Any, Any, Any, Any, Optional[Any]) -> EventProcessor
162162
def event_processor(event, hint):
163163
# type: (Event, Hint) -> Optional[Event]
164+
client = Hub.current.client
165+
164166
with capture_internal_exceptions():
165167
extra = event.setdefault("extra", {})
166168
extra["celery-job"] = partial_serialize(
167-
Hub.current.client,
169+
client,
168170
{"task_name": task.name, "args": args, "kwargs": kwargs},
169171
should_repr_strings=False,
170172
)
@@ -175,7 +177,11 @@ def event_processor(event, hint):
175177
event["fingerprint"] = [
176178
"celery",
177179
"SoftTimeLimitExceeded",
178-
getattr(task, "name", task),
180+
partial_serialize(
181+
client,
182+
getattr(task, "name", task),
183+
should_repr_strings=False,
184+
),
179185
]
180186

181187
return event

sentry_sdk/integrations/logging.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,16 @@ def _emit(self, record):
202202

203203
hint["log_record"] = record
204204

205+
client = Hub.current.client
206+
205207
event["level"] = _logging_to_event_level(record.levelname)
206208
event["logger"] = record.name
207-
event["logentry"] = {"message": to_string(record.msg), "params": record.args}
209+
event["logentry"] = {
210+
"message": to_string(record.msg),
211+
"params": partial_serialize(client, record.args, should_repr_strings=False),
212+
}
208213
event["extra"] = partial_serialize(
209-
Hub.current.client, _extra_from_record(record), should_repr_strings=False
214+
client, _extra_from_record(record), should_repr_strings=False
210215
)
211216

212217
hub.capture_event(event, hint=hint)

sentry_sdk/integrations/stdlib.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ def getresponse(self, *args, **kwargs):
108108
rv = real_getresponse(self, *args, **kwargs)
109109

110110
if data_dict is not None:
111-
data_dict["httplib_response"] = rv
112111
data_dict["status_code"] = rv.status
113112
data_dict["reason"] = rv.reason
114113
except TypeError:
@@ -200,7 +199,8 @@ def sentry_patched_popen_init(self, *a, **kw):
200199
env["SUBPROCESS_" + k.upper().replace("-", "_")] = v
201200

202201
with hub.start_span(op="subprocess", description=description) as span:
203-
span.set_data("subprocess.cwd", cwd)
202+
if cwd:
203+
span.set_data("subprocess.cwd", cwd)
204204

205205
rv = old_popen_init(self, *a, **kw) # type: ignore
206206

sentry_sdk/integrations/wsgi.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ def event_processor(event, hint):
278278

279279
if _should_send_default_pii():
280280
user_info = event.setdefault("user", {})
281-
user_info["ip_address"] = client_ip
281+
if client_ip:
282+
user_info["ip_address"] = client_ip
282283

283284
request_info["url"] = request_url
284285
request_info["query_string"] = query_string

sentry_sdk/tracing.py

+15-12
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,12 @@ def finish(self, hub=None):
338338
)
339339

340340
def to_json(self, client):
341-
# type: (Optional[sentry_sdk.Client]) -> Any
341+
# type: (Optional[sentry_sdk.Client]) -> Dict[str, Any]
342342
rv = {
343343
"trace_id": self.trace_id,
344344
"span_id": self.span_id,
345345
"parent_span_id": self.parent_span_id,
346346
"same_process_as_parent": self.same_process_as_parent,
347-
"transaction": self.transaction,
348347
"op": self.op,
349348
"description": self.description,
350349
"start_timestamp": partial_serialize(
@@ -356,9 +355,19 @@ def to_json(self, client):
356355
"timestamp": partial_serialize(
357356
client, self.timestamp, is_databag=False, should_repr_strings=False
358357
),
359-
"tags": self._tags,
360-
"data": self._data,
361-
}
358+
} # type: Dict[str, Any]
359+
360+
transaction = self.transaction
361+
if transaction:
362+
rv["transaction"] = transaction
363+
364+
tags = self._tags
365+
if tags:
366+
rv["tags"] = tags
367+
368+
data = self._data
369+
if data:
370+
rv["data"] = data
362371

363372
return rv
364373

@@ -460,17 +469,11 @@ def _maybe_create_breadcrumbs_from_span(hub, span):
460469
message=span.description, type="redis", category="redis", data=span._tags
461470
)
462471
elif span.op == "http" and span.is_success():
463-
hub.add_breadcrumb(
464-
type="http",
465-
category="httplib",
466-
data=span._data,
467-
hint={"httplib_response": span._data.pop("httplib_response", None)},
468-
)
472+
hub.add_breadcrumb(type="http", category="httplib", data=span._data)
469473
elif span.op == "subprocess":
470474
hub.add_breadcrumb(
471475
type="subprocess",
472476
category="subprocess",
473477
message=span.description,
474478
data=span._data,
475-
hint={"popen_instance": span._data.pop("popen_instance", None)},
476479
)

tests/conftest.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,17 @@ def inner(event):
178178
return inner
179179

180180

181-
@pytest.fixture(params=[True, False], ids=["fast_serializer", "default_serializer"])
182-
def sentry_init(monkeypatch_test_transport, request):
181+
@pytest.fixture(params=[True, False], ids=["fast_serialize", "default_serialize"])
182+
def fast_serialize(request):
183+
return request.param
184+
185+
186+
@pytest.fixture
187+
def sentry_init(monkeypatch_test_transport, fast_serialize):
183188
def inner(*a, **kw):
184189
hub = sentry_sdk.Hub.current
185190
client = sentry_sdk.Client(*a, **kw)
186-
client.options["_experiments"]["fast_serializer"] = request.param
191+
client.options["_experiments"]["fast_serialize"] = fast_serialize
187192
hub.bind_client(client)
188193
monkeypatch_test_transport(sentry_sdk.Hub.current.client)
189194

tests/integrations/bottle/test_bottle.py

+31-23
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ def index():
117117
assert event["exception"]["values"][0]["mechanism"]["handled"] is False
118118

119119

120-
def test_large_json_request(sentry_init, capture_events, app, get_client):
120+
def test_large_json_request(
121+
sentry_init, capture_events, app, get_client, fast_serialize
122+
):
121123
sentry_init(integrations=[bottle_sentry.BottleIntegration()])
122124

123125
data = {"foo": {"bar": "a" * 2000}}
@@ -140,10 +142,10 @@ def index():
140142
assert response[1] == "200 OK"
141143

142144
event, = events
143-
# __import__("pdb").set_trace()
144-
assert event["_meta"]["request"]["data"]["foo"]["bar"] == {
145-
"": {"len": 2000, "rem": [["!limit", "x", 509, 512]]}
146-
}
145+
if not fast_serialize:
146+
assert event["_meta"]["request"]["data"]["foo"]["bar"] == {
147+
"": {"len": 2000, "rem": [["!limit", "x", 509, 512]]}
148+
}
147149
assert len(event["request"]["data"]["foo"]["bar"]) == 512
148150

149151

@@ -171,7 +173,9 @@ def index():
171173
assert event["request"]["data"] == data
172174

173175

174-
def test_medium_formdata_request(sentry_init, capture_events, app, get_client):
176+
def test_medium_formdata_request(
177+
sentry_init, capture_events, app, get_client, fast_serialize
178+
):
175179
sentry_init(integrations=[bottle_sentry.BottleIntegration()])
176180

177181
data = {"foo": "a" * 2000}
@@ -191,15 +195,16 @@ def index():
191195
assert response[1] == "200 OK"
192196

193197
event, = events
194-
assert event["_meta"]["request"]["data"]["foo"] == {
195-
"": {"len": 2000, "rem": [["!limit", "x", 509, 512]]}
196-
}
198+
if not fast_serialize:
199+
assert event["_meta"]["request"]["data"]["foo"] == {
200+
"": {"len": 2000, "rem": [["!limit", "x", 509, 512]]}
201+
}
197202
assert len(event["request"]["data"]["foo"]) == 512
198203

199204

200205
@pytest.mark.parametrize("input_char", [u"a", b"a"])
201206
def test_too_large_raw_request(
202-
sentry_init, input_char, capture_events, app, get_client
207+
sentry_init, input_char, capture_events, app, get_client, fast_serialize
203208
):
204209
sentry_init(
205210
integrations=[bottle_sentry.BottleIntegration()], request_bodies="small"
@@ -226,13 +231,14 @@ def index():
226231
assert response[1] == "200 OK"
227232

228233
event, = events
229-
assert event["_meta"]["request"]["data"] == {
230-
"": {"len": 2000, "rem": [["!config", "x", 0, 2000]]}
231-
}
234+
if not fast_serialize:
235+
assert event["_meta"]["request"]["data"] == {
236+
"": {"len": 2000, "rem": [["!config", "x", 0, 2000]]}
237+
}
232238
assert not event["request"]["data"]
233239

234240

235-
def test_files_and_form(sentry_init, capture_events, app, get_client):
241+
def test_files_and_form(sentry_init, capture_events, app, get_client, fast_serialize):
236242
sentry_init(
237243
integrations=[bottle_sentry.BottleIntegration()], request_bodies="always"
238244
)
@@ -256,17 +262,19 @@ def index():
256262
assert response[1] == "200 OK"
257263

258264
event, = events
259-
assert event["_meta"]["request"]["data"]["foo"] == {
260-
"": {"len": 2000, "rem": [["!limit", "x", 509, 512]]}
261-
}
265+
if not fast_serialize:
266+
assert event["_meta"]["request"]["data"]["foo"] == {
267+
"": {"len": 2000, "rem": [["!limit", "x", 509, 512]]}
268+
}
262269
assert len(event["request"]["data"]["foo"]) == 512
263270

264-
assert event["_meta"]["request"]["data"]["file"] == {
265-
"": {
266-
"len": -1,
267-
"rem": [["!raw", "x", 0, -1]],
268-
} # bottle default content-length is -1
269-
}
271+
if not fast_serialize:
272+
assert event["_meta"]["request"]["data"]["file"] == {
273+
"": {
274+
"len": -1,
275+
"rem": [["!raw", "x", 0, -1]],
276+
} # bottle default content-length is -1
277+
}
270278
assert not event["request"]["data"]["file"]
271279

272280

tests/integrations/celery/test_celery.py

-2
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,12 @@ def dummy_task(x, y):
131131
assert execution_event["spans"] == []
132132
assert submission_event["spans"] == [
133133
{
134-
u"data": {},
135134
u"description": u"dummy_task",
136135
u"op": "celery.submit",
137136
u"parent_span_id": submission_event["contexts"]["trace"]["span_id"],
138137
u"same_process_as_parent": True,
139138
u"span_id": submission_event["spans"][0]["span_id"],
140139
u"start_timestamp": submission_event["spans"][0]["start_timestamp"],
141-
u"tags": {},
142140
u"timestamp": submission_event["spans"][0]["timestamp"],
143141
u"trace_id": text_type(span.trace_id),
144142
}

tests/integrations/django/test_basic.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ def test_transaction_style(
341341
assert event["transaction"] == expected_transaction
342342

343343

344-
def test_request_body(sentry_init, client, capture_events):
344+
def test_request_body(sentry_init, client, capture_events, fast_serialize):
345345
sentry_init(integrations=[DjangoIntegration()])
346346
events = capture_events()
347347
content, status, headers = client.post(
@@ -354,10 +354,11 @@ def test_request_body(sentry_init, client, capture_events):
354354

355355
assert event["message"] == "hi"
356356
assert event["request"]["data"] == ""
357-
assert event["_meta"]["request"]["data"][""] == {
358-
"len": 6,
359-
"rem": [["!raw", "x", 0, 6]],
360-
}
357+
if not fast_serialize:
358+
assert event["_meta"]["request"]["data"][""] == {
359+
"len": 6,
360+
"rem": [["!raw", "x", 0, 6]],
361+
}
361362

362363
del events[:]
363364

tests/integrations/falcon/test_falcon.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def on_get(self, req, resp):
9898
assert event["exception"]["values"][0]["mechanism"]["type"] == "falcon"
9999

100100

101-
def test_falcon_large_json_request(sentry_init, capture_events):
101+
def test_falcon_large_json_request(sentry_init, capture_events, fast_serialize):
102102
sentry_init(integrations=[FalconIntegration()])
103103

104104
data = {"foo": {"bar": "a" * 2000}}
@@ -119,9 +119,10 @@ def on_post(self, req, resp):
119119
assert response.status == falcon.HTTP_200
120120

121121
event, = events
122-
assert event["_meta"]["request"]["data"]["foo"]["bar"] == {
123-
"": {"len": 2000, "rem": [["!limit", "x", 509, 512]]}
124-
}
122+
if not fast_serialize:
123+
assert event["_meta"]["request"]["data"]["foo"]["bar"] == {
124+
"": {"len": 2000, "rem": [["!limit", "x", 509, 512]]}
125+
}
125126
assert len(event["request"]["data"]["foo"]["bar"]) == 512
126127

127128

0 commit comments

Comments
 (0)