Skip to content

Commit dbce219

Browse files
authored
fix: Make patched channels asgi app awaitable, fix integraton-disabled codepath (getsentry#598)
* fix: Make patched channels asgi app awaitable, fix integraton-disabled codepath * fix: Fix linters * fix: Reformatting
1 parent bace745 commit dbce219

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

sentry_sdk/integrations/django/__init__.py

+2-15
Original file line numberDiff line numberDiff line change
@@ -298,22 +298,9 @@ def _patch_channels():
298298
"Python 3.7+ or the aiocontextvars package from PyPI."
299299
)
300300

301-
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
301+
from sentry_sdk.integrations.django.asgi import patch_channels_asgi_handler_impl
302302

303-
old_app = AsgiHandler.__call__
304-
305-
def sentry_patched_asgi_handler(self, receive, send):
306-
# type: (AsgiHandler, Any, Any) -> Any
307-
if Hub.current.get_integration(DjangoIntegration) is None:
308-
return old_app(receive, send)
309-
310-
middleware = SentryAsgiMiddleware(
311-
lambda _scope: old_app.__get__(self, AsgiHandler)
312-
)
313-
314-
return middleware(self.scope)(receive, send)
315-
316-
AsgiHandler.__call__ = sentry_patched_asgi_handler
303+
patch_channels_asgi_handler_impl(AsgiHandler)
317304

318305

319306
def _patch_django_asgi_handler():

sentry_sdk/integrations/django/asgi.py

+16
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,19 @@ async def sentry_patched_asgi_handler(self, scope, receive, send):
2929
return await middleware(scope, receive, send)
3030

3131
cls.__call__ = sentry_patched_asgi_handler
32+
33+
34+
def patch_channels_asgi_handler_impl(cls):
35+
# type: (Any) -> None
36+
old_app = cls.__call__
37+
38+
async def sentry_patched_asgi_handler(self, receive, send):
39+
# type: (Any, Any, Any) -> Any
40+
if Hub.current.get_integration(DjangoIntegration) is None:
41+
return await old_app(self, receive, send)
42+
43+
middleware = SentryAsgiMiddleware(lambda _scope: old_app.__get__(self, cls))
44+
45+
return await middleware(self.scope)(receive, send)
46+
47+
cls.__call__ = sentry_patched_asgi_handler

0 commit comments

Comments
 (0)