1+ from __future__ import absolute_import
2+
13from threading import Lock , local
24
3- from django .apps import AppConfig
45from django .conf import settings
56from django .core import signals
6- from django .urls import resolve
7+ try :
8+ from django .urls import resolve
9+ except ImportError :
10+ from django .core .urlresolvers import resolve
711
812from sentry_sdk import get_current_hub , configure_scope , capture_exception
913
@@ -25,19 +29,17 @@ def _get_transaction_from_request(request):
2529# request_started (or any other signal) cannot be used because the request is
2630# not yet available
2731class SentryMiddleware (MiddlewareMixin ):
28- def process_view (self , request , func , args , kwargs ):
32+ def process_request (self , request ):
33+ assert getattr (_request_scope , 'manager' , None ) is None , 'race condition'
34+ _request_scope .manager = get_current_hub ().push_scope ().__enter__ ()
35+
2936 try :
3037 with configure_scope () as scope :
3138 scope .transaction = _get_transaction_from_request (request )
3239 except Exception :
3340 capture_exception ()
3441
3542
36- def _request_started (* args , ** kwargs ):
37- assert getattr (_request_scope , 'manager' , None ) is None , 'race condition'
38- _request_scope .manager = get_current_hub ().push_scope ().__enter__ ()
39-
40-
4143def _request_finished (* args , ** kwargs ):
4244 assert getattr (_request_scope , 'manager' , None ) is not None , 'race condition'
4345 _request_scope .manager .__exit__ (None , None , None )
@@ -86,18 +88,24 @@ def _initialize_impl():
8688 middleware_attr ,
8789 [MIDDLEWARE_NAME ] + list (middleware ))
8890
89- signals .request_started .connect (_request_started )
9091 signals .request_finished .connect (_request_finished )
9192 signals .got_request_exception .connect (_got_request_exception )
9293
9394
94- default_app_config = 'sentry_sdk.integrations.django.SentryConfig'
9595
9696
97- class SentryConfig (AppConfig ):
98- name = 'sentry_sdk.integrations.django'
99- label = 'sentry_sdk_integrations_django'
100- verbose_name = 'Sentry'
97+ try :
98+ # Django >= 1.7
99+ from django .apps import AppConfig
100+ except ImportError :
101+ initialize ()
102+ else :
103+ class SentryConfig (AppConfig ):
104+ name = 'sentry_sdk.integrations.django'
105+ label = 'sentry_sdk_integrations_django'
106+ verbose_name = 'Sentry'
107+
108+ def ready (self ):
109+ initialize ()
101110
102- def ready (self ):
103- initialize ()
111+ default_app_config = 'sentry_sdk.integrations.django.SentryConfig'
0 commit comments