def setup_once(): DjangoIntegration.setup_once() from django.core.handlers.base import BaseHandler # DjangoIntegration already patched get_response, we patch it again to add our custom # processor old_get_response = BaseHandler.get_response def sentry_patched_get_response(self, request): hub = Hub.current integration = hub.get_integration(DjangoIntegration) if integration is not None: with hub.configure_scope() as scope: scope.add_event_processor( _make_event_processor(weakref.ref(request), integration)) return old_get_response(self, request) BaseHandler.get_response = sentry_patched_get_response if hasattr(BaseHandler, "get_response_async"): from sentry_sdk.integrations.django.asgi import ( patch_get_response_async, ) patch_get_response_async(BaseHandler, _before_get_response)
def _patch_get_response(): # type: () -> None """ patch get_response, because at that point we have the Django request object """ from django.core.handlers.base import BaseHandler old_get_response = BaseHandler.get_response def sentry_patched_get_response(self, request): # type: (Any, WSGIRequest) -> Union[HttpResponse, BaseException] _before_get_response(request) return old_get_response(self, request) BaseHandler.get_response = sentry_patched_get_response if hasattr(BaseHandler, "get_response_async"): from sentry_sdk.integrations.django.asgi import patch_get_response_async patch_get_response_async(BaseHandler, _before_get_response)