Skip to content

Commit a8afb8b

Browse files
committed
Merge branch 'master' into potel-base
2 parents 6b03581 + 55d757a commit a8afb8b

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-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: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,3 +624,77 @@ 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=[
639+
DjangoIntegration(
640+
middleware_spans=False,
641+
signals_spans=False,
642+
),
643+
],
644+
traces_sample_rate=1.0,
645+
)
646+
events = capture_events()
647+
648+
comm = HttpCommunicator(application, "GET", "/simple_async_view")
649+
await comm.get_response()
650+
await comm.wait()
651+
652+
comm = HttpCommunicator(application, "OPTIONS", "/simple_async_view")
653+
await comm.get_response()
654+
await comm.wait()
655+
656+
comm = HttpCommunicator(application, "HEAD", "/simple_async_view")
657+
await comm.get_response()
658+
await comm.wait()
659+
660+
(event,) = events
661+
662+
assert len(events) == 1
663+
assert event["request"]["method"] == "GET"
664+
665+
666+
@pytest.mark.parametrize("application", APPS)
667+
@pytest.mark.asyncio
668+
async def test_transaction_http_method_custom(sentry_init, capture_events, application):
669+
sentry_init(
670+
integrations=[
671+
DjangoIntegration(
672+
middleware_spans=False,
673+
signals_spans=False,
674+
http_methods_to_capture=(
675+
"OPTIONS",
676+
"head",
677+
), # capitalization does not matter
678+
)
679+
],
680+
traces_sample_rate=1.0,
681+
)
682+
events = capture_events()
683+
684+
comm = HttpCommunicator(application, "GET", "/simple_async_view")
685+
await comm.get_response()
686+
await comm.wait()
687+
688+
comm = HttpCommunicator(application, "OPTIONS", "/simple_async_view")
689+
await comm.get_response()
690+
await comm.wait()
691+
692+
comm = HttpCommunicator(application, "HEAD", "/simple_async_view")
693+
await comm.get_response()
694+
await comm.wait()
695+
696+
assert len(events) == 2
697+
698+
(event1, event2) = events
699+
assert event1["request"]["method"] == "OPTIONS"
700+
assert event2["request"]["method"] == "HEAD"

0 commit comments

Comments
 (0)