Skip to content

Add http_methods_to_capture to ASGI Django #3607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions sentry_sdk/integrations/django/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,15 @@ def patch_django_asgi_handler_impl(cls):

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

middleware = SentryAsgiMiddleware(
old_app.__get__(self, cls),
unsafe_context_data=True,
span_origin=DjangoIntegration.origin,
http_methods_to_capture=integration.http_methods_to_capture,
)._run_asgi3

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

async def sentry_patched_asgi_handler(self, receive, send):
# type: (Any, Any, Any) -> Any
if sentry_sdk.get_client().get_integration(DjangoIntegration) is None:
integration = sentry_sdk.get_client().get_integration(DjangoIntegration)
if integration is None:
return await old_app(self, receive, send)

middleware = SentryAsgiMiddleware(
lambda _scope: old_app.__get__(self, cls),
unsafe_context_data=True,
span_origin=DjangoIntegration.origin,
http_methods_to_capture=integration.http_methods_to_capture,
)

return await middleware(self.scope)(receive, send)
Expand Down
67 changes: 67 additions & 0 deletions tests/integrations/django/asgi/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,70 @@ async def test_async_view(sentry_init, capture_events, application):
(event,) = events
assert event["type"] == "transaction"
assert event["transaction"] == "/simple_async_view"


@pytest.mark.parametrize("application", APPS)
@pytest.mark.asyncio
async def test_transaction_http_method_default(
sentry_init, capture_events, application
):
"""
By default OPTIONS and HEAD requests do not create a transaction.
"""
sentry_init(
integrations=[DjangoIntegration()],
traces_sample_rate=1.0,
)
events = capture_events()

comm = HttpCommunicator(application, "GET", "/simple_async_view")
await comm.get_response()
await comm.wait()

comm = HttpCommunicator(application, "OPTIONS", "/simple_async_view")
await comm.get_response()
await comm.wait()

comm = HttpCommunicator(application, "HEAD", "/simple_async_view")
await comm.get_response()
await comm.wait()

(event,) = events

assert len(events) == 1
assert event["request"]["method"] == "GET"


@pytest.mark.parametrize("application", APPS)
@pytest.mark.asyncio
async def test_transaction_http_method_custom(sentry_init, capture_events, application):
sentry_init(
integrations=[
DjangoIntegration(
http_methods_to_capture=(
"OPTIONS",
"head",
), # capitalization does not matter
)
],
traces_sample_rate=1.0,
)
events = capture_events()

comm = HttpCommunicator(application, "GET", "/simple_async_view")
await comm.get_response()
await comm.wait()

comm = HttpCommunicator(application, "OPTIONS", "/simple_async_view")
await comm.get_response()
await comm.wait()

comm = HttpCommunicator(application, "HEAD", "/simple_async_view")
await comm.get_response()
await comm.wait()

assert len(events) == 2

(event1, event2) = events
assert event1["request"]["method"] == "OPTIONS"
assert event2["request"]["method"] == "HEAD"
Loading