Skip to content
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
17 changes: 2 additions & 15 deletions sentry_sdk/integrations/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,22 +298,9 @@ def _patch_channels():
"Python 3.7+ or the aiocontextvars package from PyPI."
)

from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
from sentry_sdk.integrations.django.asgi import patch_channels_asgi_handler_impl

old_app = AsgiHandler.__call__

def sentry_patched_asgi_handler(self, receive, send):
# type: (AsgiHandler, Any, Any) -> Any
if Hub.current.get_integration(DjangoIntegration) is None:
return old_app(receive, send)

middleware = SentryAsgiMiddleware(
lambda _scope: old_app.__get__(self, AsgiHandler)
)

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

AsgiHandler.__call__ = sentry_patched_asgi_handler
patch_channels_asgi_handler_impl(AsgiHandler)


def _patch_django_asgi_handler():
Expand Down
16 changes: 16 additions & 0 deletions sentry_sdk/integrations/django/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,19 @@ async def sentry_patched_asgi_handler(self, scope, receive, send):
return await middleware(scope, receive, send)

cls.__call__ = sentry_patched_asgi_handler


def patch_channels_asgi_handler_impl(cls):
# type: (Any) -> None
old_app = cls.__call__

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

middleware = SentryAsgiMiddleware(lambda _scope: old_app.__get__(self, cls))

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

cls.__call__ = sentry_patched_asgi_handler