Skip to content

Commit 55d757a

Browse files
authored
Add http_methods_to_capture to ASGI Django (#3607)
1 parent 033e3ad commit 55d757a

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

sentry_sdk/integrations/django/asgi.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,15 @@ def patch_django_asgi_handler_impl(cls):
9090

9191
async def sentry_patched_asgi_handler(self, scope, receive, send):
9292
# type: (Any, Any, Any, Any) -> Any
93-
if sentry_sdk.get_client().get_integration(DjangoIntegration) is None:
93+
integration = sentry_sdk.get_client().get_integration(DjangoIntegration)
94+
if integration is None:
9495
return await old_app(self, scope, receive, send)
9596

9697
middleware = SentryAsgiMiddleware(
9798
old_app.__get__(self, cls),
9899
unsafe_context_data=True,
99100
span_origin=DjangoIntegration.origin,
101+
http_methods_to_capture=integration.http_methods_to_capture,
100102
)._run_asgi3
101103

102104
return await middleware(scope, receive, send)
@@ -142,13 +144,15 @@ def patch_channels_asgi_handler_impl(cls):
142144

143145
async def sentry_patched_asgi_handler(self, receive, send):
144146
# type: (Any, Any, Any) -> Any
145-
if sentry_sdk.get_client().get_integration(DjangoIntegration) is None:
147+
integration = sentry_sdk.get_client().get_integration(DjangoIntegration)
148+
if integration is None:
146149
return await old_app(self, receive, send)
147150

148151
middleware = SentryAsgiMiddleware(
149152
lambda _scope: old_app.__get__(self, cls),
150153
unsafe_context_data=True,
151154
span_origin=DjangoIntegration.origin,
155+
http_methods_to_capture=integration.http_methods_to_capture,
152156
)
153157

154158
return await middleware(self.scope)(receive, send)

tests/integrations/django/asgi/test_asgi.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,3 +624,70 @@ async def test_async_view(sentry_init, capture_events, application):
624624
(event,) = events
625625
assert event["type"] == "transaction"
626626
assert event["transaction"] == "/simple_async_view"
627+
628+
629+
@pytest.mark.parametrize("application", APPS)
630+
@pytest.mark.asyncio
631+
async def test_transaction_http_method_default(
632+
sentry_init, capture_events, application
633+
):
634+
"""
635+
By default OPTIONS and HEAD requests do not create a transaction.
636+
"""
637+
sentry_init(
638+
integrations=[DjangoIntegration()],
639+
traces_sample_rate=1.0,
640+
)
641+
events = capture_events()
642+
643+
comm = HttpCommunicator(application, "GET", "/simple_async_view")
644+
await comm.get_response()
645+
await comm.wait()
646+
647+
comm = HttpCommunicator(application, "OPTIONS", "/simple_async_view")
648+
await comm.get_response()
649+
await comm.wait()
650+
651+
comm = HttpCommunicator(application, "HEAD", "/simple_async_view")
652+
await comm.get_response()
653+
await comm.wait()
654+
655+
(event,) = events
656+
657+
assert len(events) == 1
658+
assert event["request"]["method"] == "GET"
659+
660+
661+
@pytest.mark.parametrize("application", APPS)
662+
@pytest.mark.asyncio
663+
async def test_transaction_http_method_custom(sentry_init, capture_events, application):
664+
sentry_init(
665+
integrations=[
666+
DjangoIntegration(
667+
http_methods_to_capture=(
668+
"OPTIONS",
669+
"head",
670+
), # capitalization does not matter
671+
)
672+
],
673+
traces_sample_rate=1.0,
674+
)
675+
events = capture_events()
676+
677+
comm = HttpCommunicator(application, "GET", "/simple_async_view")
678+
await comm.get_response()
679+
await comm.wait()
680+
681+
comm = HttpCommunicator(application, "OPTIONS", "/simple_async_view")
682+
await comm.get_response()
683+
await comm.wait()
684+
685+
comm = HttpCommunicator(application, "HEAD", "/simple_async_view")
686+
await comm.get_response()
687+
await comm.wait()
688+
689+
assert len(events) == 2
690+
691+
(event1, event2) = events
692+
assert event1["request"]["method"] == "OPTIONS"
693+
assert event2["request"]["method"] == "HEAD"

0 commit comments

Comments
 (0)