Skip to content

Commit b732d86

Browse files
test iteration
2 parents e889d0e + 22b2886 commit b732d86

1 file changed

Lines changed: 36 additions & 36 deletions

File tree

tests/integrations/stdlib/test_httplib.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -337,13 +337,13 @@ def test_request_source_enabled(
337337
sentry_options = {
338338
"traces_sample_rate": 1.0,
339339
"http_request_source_threshold_ms": 0,
340+
"_experiments": {"trace_lifecycle": "stream" if span_streaming else "static"},
340341
}
341342
if enable_http_request_source is not None:
342343
sentry_options["enable_http_request_source"] = enable_http_request_source
343344

344345
sentry_init(
345346
**sentry_options,
346-
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
347347
)
348348

349349
items = capture_items("event", "transaction", "span")
@@ -463,22 +463,23 @@ def test_no_request_source_if_duration_too_short(
463463
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
464464
)
465465

466-
already_patched_getresponse = HTTPConnection.getresponse
466+
add_http_request_source = sentry_sdk.tracing_utils.add_http_request_source
467467

468-
class HttpConnectionWithPatchedSpan(HTTPConnection):
469-
def getresponse(self, *args, **kwargs):
470-
response = already_patched_getresponse(self, *args, **kwargs)
471-
span = self._sentrysdk_span # type: ignore
472-
span._start_timestamp = datetime.datetime(2024, 1, 1, microsecond=0)
473-
span._timestamp = datetime.datetime(2024, 1, 1, microsecond=99999)
474-
return response
468+
def add_http_request_source_with_pinned_timestamps(span):
469+
span._start_timestamp = datetime.datetime(2024, 1, 1, microsecond=0)
470+
span._timestamp = datetime.datetime(2024, 1, 1, microsecond=99999)
471+
return add_http_request_source(span)
475472

476473
items = capture_items("event", "transaction", "span")
477474

478-
with sentry_sdk.traces.start_span(name="custom parent"):
479-
conn = HttpConnectionWithPatchedSpan("localhost", port=PORT)
480-
conn.request("GET", "/foo")
481-
conn.getresponse()
475+
with mock.patch(
476+
"sentry_sdk.integrations.stdlib.add_http_request_source",
477+
add_http_request_source_with_pinned_timestamps,
478+
):
479+
with sentry_sdk.traces.start_span(name="foo"):
480+
conn = HTTPConnection("localhost", port=PORT)
481+
conn.request("GET", "/foo")
482+
conn.getresponse()
482483

483484
sentry_sdk.flush()
484485
span = next(item.payload for item in items if item.type == "span")
@@ -503,22 +504,23 @@ def test_request_source_if_duration_over_threshold(
503504
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
504505
)
505506

506-
already_patched_getresponse = HTTPConnection.getresponse
507+
add_http_request_source = sentry_sdk.tracing_utils.add_http_request_source
507508

508-
class HttpConnectionWithPatchedSpan(HTTPConnection):
509-
def getresponse(self, *args, **kwargs):
510-
response = already_patched_getresponse(self, *args, **kwargs)
511-
span = self._sentrysdk_span # type: ignore
512-
span._start_timestamp = datetime.datetime(2024, 1, 1, microsecond=0)
513-
span._timestamp = datetime.datetime(2024, 1, 1, microsecond=100001)
514-
return response
509+
def add_http_request_source_with_pinned_timestamps(span):
510+
span._start_timestamp = datetime.datetime(2024, 1, 1, microsecond=0)
511+
span._timestamp = datetime.datetime(2024, 1, 1, microsecond=100001)
512+
return add_http_request_source(span)
515513

516514
items = capture_items("event", "transaction", "span")
517515

518-
with sentry_sdk.traces.start_span(name="custom parent"):
519-
conn = HttpConnectionWithPatchedSpan("localhost", port=PORT)
520-
conn.request("GET", "/foo")
521-
conn.getresponse()
516+
with mock.patch(
517+
"sentry_sdk.integrations.stdlib.add_http_request_source",
518+
add_http_request_source_with_pinned_timestamps,
519+
):
520+
with sentry_sdk.traces.start_span(name="foo"):
521+
conn = HTTPConnection("localhost", port=PORT)
522+
conn.request("GET", "/foo")
523+
conn.getresponse()
522524

523525
sentry_sdk.flush()
524526
span = next(item.payload for item in items if item.type == "span")
@@ -546,7 +548,7 @@ def getresponse(self, *args, **kwargs):
546548

547549
assert (
548550
attributes.get(SPANDATA.CODE_FUNCTION)
549-
== "test_request_source_if_duration_over_threshold"
551+
== "add_http_request_source_with_pinned_timestamps"
550552
)
551553

552554

@@ -566,14 +568,14 @@ def test_span_origin(sentry_init, capture_items, span_streaming):
566568

567569
sentry_sdk.flush()
568570
spans = [item.payload for item in items if item.type == "span"]
569-
assert spans[0]["attributes"]["sentry.origin"] == "manual"
571+
assert spans[1]["attributes"]["sentry.origin"] == "manual"
570572

571573
assert spans[0]["attributes"]["sentry.op"] == "http.client"
572-
assert spans[0]["origin"] == "auto.http.stdlib.httplib"
574+
assert spans[0]["attributes"]["sentry.origin"] == "auto.http.stdlib.httplib"
573575

574576

575577
@pytest.mark.parametrize("span_streaming", [True, False])
576-
def test_http_timeout(monkeypatch, sentry_init, capture_envelopes, span_streaming):
578+
def test_http_timeout(monkeypatch, sentry_init, capture_items, span_streaming):
577579
mock_readinto = mock.Mock(side_effect=TimeoutError)
578580
monkeypatch.setattr(SocketIO, "readinto", mock_readinto)
579581

@@ -582,7 +584,7 @@ def test_http_timeout(monkeypatch, sentry_init, capture_envelopes, span_streamin
582584
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
583585
)
584586

585-
envelopes = capture_envelopes()
587+
items = capture_items("event", "transaction", "span")
586588

587589
with pytest.raises(TimeoutError):
588590
with sentry_sdk.traces.start_span(
@@ -595,11 +597,9 @@ def test_http_timeout(monkeypatch, sentry_init, capture_envelopes, span_streamin
595597
conn.request("GET", "/bla")
596598
conn.getresponse()
597599

598-
(transaction_envelope,) = envelopes
599-
transaction = transaction_envelope.get_transaction_event()
600-
assert len(transaction["spans"]) == 1
601-
602-
span = transaction["spans"][0]
600+
sentry_sdk.flush()
601+
spans = [item.payload for item in items if item.type == "span"]
602+
span = spans[0]
603603
assert span["attributes"]["sentry.op"] == "http.client"
604604
assert span["name"] == f"GET http://localhost:{PORT}/bla" # noqa: E231
605605

@@ -624,7 +624,7 @@ def test_proxy_http_tunnel(sentry_init, capture_items, tunnel_port, span_streami
624624
(span,) = (
625625
span
626626
for span in spans
627-
if span["attributes"].get("sentry.op") == "auto.http.stdlib.httplib"
627+
if span["attributes"].get("sentry.origin") == "auto.http.stdlib.httplib"
628628
)
629629

630630
port_modifier = f":{tunnel_port}" if tunnel_port else ""

0 commit comments

Comments
 (0)