From 05411ff4ffa5bf795c111baa49425c803762eeb9 Mon Sep 17 00:00:00 2001 From: PakawiNz Date: Tue, 1 Oct 2024 16:38:22 +0700 Subject: [PATCH] allowing ASGI to use drf_request in DjangoRequestExtractor (#3572) since we already have patched a request object (both ASGI/WSGI) before arriving, we should move patched-using logic closer to where it actually being used. for minimize impact and allow ASGI functionality. --------- Co-authored-by: Ivana Kellyer --- sentry_sdk/integrations/django/__init__.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/sentry_sdk/integrations/django/__init__.py b/sentry_sdk/integrations/django/__init__.py index 40d17b0507..7d33aad29c 100644 --- a/sentry_sdk/integrations/django/__init__.py +++ b/sentry_sdk/integrations/django/__init__.py @@ -491,13 +491,6 @@ def wsgi_request_event_processor(event, hint): # We have a `asgi_request_event_processor` for this. return event - try: - drf_request = request._sentry_drf_request_backref() - if drf_request is not None: - request = drf_request - except AttributeError: - pass - with capture_internal_exceptions(): DjangoRequestExtractor(request).extract_into_event(event) @@ -530,6 +523,16 @@ def _got_request_exception(request=None, **kwargs): class DjangoRequestExtractor(RequestExtractor): + def __init__(self, request): + # type: (Union[WSGIRequest, ASGIRequest]) -> None + try: + drf_request = request._sentry_drf_request_backref() + if drf_request is not None: + request = drf_request + except AttributeError: + pass + self.request = request + def env(self): # type: () -> Dict[str, str] return self.request.META