|
| 1 | +from threading import Lock, local |
| 2 | + |
| 3 | +from django.apps import AppConfig |
| 4 | +from django.core import signals |
| 5 | +from django.conf import settings |
| 6 | +from django.urls import resolve |
| 7 | + |
| 8 | +from .. import get_current_hub, configure_scope, capture_exception |
| 9 | + |
| 10 | +try: |
| 11 | + # Django >= 1.10 |
| 12 | + from django.utils.deprecation import MiddlewareMixin |
| 13 | +except ImportError: |
| 14 | + # Not required for Django <= 1.9, see: |
| 15 | + # https://docs.djangoproject.com/en/1.10/topics/http/middleware/#upgrading-pre-django-1-10-style-middleware |
| 16 | + MiddlewareMixin = object |
| 17 | + |
| 18 | + |
| 19 | +def _get_transaction_from_request(request): |
| 20 | + return resolve(request.path).func |
| 21 | + |
| 22 | +_request_scope = local() |
| 23 | + |
| 24 | + |
| 25 | +class SentryMiddleware(MiddlewareMixin): |
| 26 | + def process_view(self, request, func, args, kwargs): |
| 27 | + try: |
| 28 | + with configure_scope() as scope: |
| 29 | + scope.transaction = _get_transaction_from_request(request) |
| 30 | + except Exception: |
| 31 | + capture_exception() |
| 32 | + |
| 33 | + |
| 34 | +def _request_started(*args, **kwargs): |
| 35 | + assert getattr(_request_scope, 'manager', None) is None, 'race condition' |
| 36 | + _request_scope.manager = get_current_hub().push_scope().__enter__() |
| 37 | + |
| 38 | + |
| 39 | +def _request_finished(*args, **kwargs): |
| 40 | + assert getattr(_request_scope, 'manager', None) is not None, 'race condition' |
| 41 | + _request_scope.manager.__exit__(None, None, None) |
| 42 | + _request_scope.manager = None |
| 43 | + |
| 44 | + |
| 45 | +def _got_request_exception(request=None, **kwargs): |
| 46 | + capture_exception() |
| 47 | + |
| 48 | + |
| 49 | +MIDDLEWARE_NAME = 'sentry_sdk.integrations.django.SentryMiddleware' |
| 50 | + |
| 51 | +CONFLICTING_MIDDLEWARE = ( |
| 52 | + 'raven.contrib.django.middleware.SentryMiddleware', |
| 53 | + 'raven.contrib.django.middleware.SentryLogMiddleware' |
| 54 | +) + (MIDDLEWARE_NAME,) |
| 55 | + |
| 56 | +_installer_lock = Lock() |
| 57 | +_installed = False |
| 58 | + |
| 59 | + |
| 60 | +def initialize(): |
| 61 | + global _installed |
| 62 | + with _installer_lock: |
| 63 | + if _installed: |
| 64 | + return |
| 65 | + _initialize_impl() |
| 66 | + _installed = True |
| 67 | + |
| 68 | + |
| 69 | +def _initialize_impl(): |
| 70 | + # default settings.MIDDLEWARE is None |
| 71 | + if getattr(settings, 'MIDDLEWARE', None): |
| 72 | + middleware_attr = 'MIDDLEWARE' |
| 73 | + else: |
| 74 | + middleware_attr = 'MIDDLEWARE_CLASSES' |
| 75 | + |
| 76 | + # make sure to get an empty tuple when attr is None |
| 77 | + middleware = getattr(settings, middleware_attr, ()) or () |
| 78 | + conflicts = set(CONFLICTING_MIDDLEWARE).intersection(set(middleware)) |
| 79 | + if conflicts: |
| 80 | + raise RuntimeError('Other sentry-middleware already registered: %s' % |
| 81 | + conflicts) |
| 82 | + |
| 83 | + setattr(settings, |
| 84 | + middleware_attr, |
| 85 | + [MIDDLEWARE_NAME] + list(middleware)) |
| 86 | + |
| 87 | + signals.request_started.connect(_request_started) |
| 88 | + signals.request_finished.connect(_request_finished) |
| 89 | + signals.got_request_exception.connect(_got_request_exception) |
| 90 | + |
| 91 | + |
| 92 | +default_app_config = 'sentry_sdk.integrations.django.SentryConfig' |
| 93 | + |
| 94 | + |
| 95 | +class SentryConfig(AppConfig): |
| 96 | + name = 'sentry_sdk.integrations.django' |
| 97 | + label = 'sentry_sdk_integrations_django' |
| 98 | + verbose_name = 'Sentry' |
| 99 | + |
| 100 | + def ready(self): |
| 101 | + initialize() |
0 commit comments