Skip to content

Commit 086a231

Browse files
test(stdlib): Pin timestamps in add_http_request_source() patch (#6160)
Overwrite span timestamps in a patch of `add_http_request_source()` instead of a `HTTPConnection` subclass.
1 parent a4c16d7 commit 086a231

1 file changed

Lines changed: 28 additions & 23 deletions

File tree

tests/integrations/stdlib/test_httplib.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import pytest
1313

14+
import sentry_sdk
1415
from sentry_sdk import capture_message, start_transaction, continue_trace
1516
from sentry_sdk.consts import MATCH_ALL, SPANDATA
1617
from sentry_sdk.integrations.stdlib import StdlibIntegration
@@ -587,21 +588,23 @@ def test_no_request_source_if_duration_too_short(sentry_init, capture_events):
587588
http_request_source_threshold_ms=100,
588589
)
589590

590-
already_patched_putrequest = HTTPConnection.putrequest
591+
add_http_request_source = sentry_sdk.tracing_utils.add_http_request_source
591592

592-
class HttpConnectionWithPatchedSpan(HTTPConnection):
593-
def putrequest(self, *args, **kwargs) -> None:
594-
already_patched_putrequest(self, *args, **kwargs)
595-
span = self._sentrysdk_span # type: ignore
596-
span.start_timestamp = datetime.datetime(2024, 1, 1, microsecond=0)
597-
span.timestamp = datetime.datetime(2024, 1, 1, microsecond=99999)
593+
def add_http_request_source_with_pinned_timestamps(span):
594+
span.start_timestamp = datetime.datetime(2024, 1, 1, microsecond=0)
595+
span.timestamp = datetime.datetime(2024, 1, 1, microsecond=99999)
596+
return add_http_request_source(span)
598597

599598
events = capture_events()
600599

601-
with start_transaction(name="foo"):
602-
conn = HttpConnectionWithPatchedSpan("localhost", port=PORT)
603-
conn.request("GET", "/foo")
604-
conn.getresponse()
600+
with mock.patch(
601+
"sentry_sdk.integrations.stdlib.add_http_request_source",
602+
add_http_request_source_with_pinned_timestamps,
603+
):
604+
with start_transaction(name="foo"):
605+
conn = HTTPConnection("localhost", port=PORT)
606+
conn.request("GET", "/foo")
607+
conn.getresponse()
605608

606609
(event,) = events
607610

@@ -623,21 +626,23 @@ def test_request_source_if_duration_over_threshold(sentry_init, capture_events):
623626
http_request_source_threshold_ms=100,
624627
)
625628

626-
already_patched_putrequest = HTTPConnection.putrequest
629+
add_http_request_source = sentry_sdk.tracing_utils.add_http_request_source
627630

628-
class HttpConnectionWithPatchedSpan(HTTPConnection):
629-
def putrequest(self, *args, **kwargs) -> None:
630-
already_patched_putrequest(self, *args, **kwargs)
631-
span = self._sentrysdk_span # type: ignore
632-
span.start_timestamp = datetime.datetime(2024, 1, 1, microsecond=0)
633-
span.timestamp = datetime.datetime(2024, 1, 1, microsecond=100001)
631+
def add_http_request_source_with_pinned_timestamps(span):
632+
span.start_timestamp = datetime.datetime(2024, 1, 1, microsecond=0)
633+
span.timestamp = datetime.datetime(2024, 1, 1, microsecond=100001)
634+
return add_http_request_source(span)
634635

635636
events = capture_events()
636637

637-
with start_transaction(name="foo"):
638-
conn = HttpConnectionWithPatchedSpan("localhost", port=PORT)
639-
conn.request("GET", "/foo")
640-
conn.getresponse()
638+
with mock.patch(
639+
"sentry_sdk.integrations.stdlib.add_http_request_source",
640+
add_http_request_source_with_pinned_timestamps,
641+
):
642+
with start_transaction(name="foo"):
643+
conn = HTTPConnection("localhost", port=PORT)
644+
conn.request("GET", "/foo")
645+
conn.getresponse()
641646

642647
(event,) = events
643648

@@ -663,7 +668,7 @@ def putrequest(self, *args, **kwargs) -> None:
663668

664669
assert (
665670
data.get(SPANDATA.CODE_FUNCTION)
666-
== "test_request_source_if_duration_over_threshold"
671+
== "add_http_request_source_with_pinned_timestamps"
667672
)
668673

669674

0 commit comments

Comments
 (0)