9
9
from copy import deepcopy
10
10
from functools import partial
11
11
12
+ import sentry_sdk
12
13
from sentry_sdk ._types import TYPE_CHECKING
13
14
from sentry_sdk .api import continue_trace
14
15
from sentry_sdk .consts import OP
15
- from sentry_sdk .hub import Hub
16
16
17
17
from sentry_sdk .integrations ._asgi_common import (
18
18
_get_headers ,
19
19
_get_request_data ,
20
20
_get_url ,
21
21
)
22
- from sentry_sdk .sessions import auto_session_tracking
22
+ from sentry_sdk .sessions import auto_session_tracking_scope
23
23
from sentry_sdk .tracing import (
24
24
SOURCE_FOR_STYLE ,
25
25
TRANSACTION_SOURCE_ROUTE ,
54
54
TRANSACTION_STYLE_VALUES = ("endpoint" , "url" )
55
55
56
56
57
- def _capture_exception (hub , exc , mechanism_type = "asgi" ):
58
- # type: (Hub, Any, str) -> None
57
+ def _capture_exception (exc , mechanism_type = "asgi" ):
58
+ # type: (Any, str) -> None
59
59
60
- # Check client here as it might have been unset while streaming response
61
- if hub .client is not None :
62
- event , hint = event_from_exception (
63
- exc ,
64
- client_options = hub .client .options ,
65
- mechanism = {"type" : mechanism_type , "handled" : False },
66
- )
67
- hub .capture_event (event , hint = hint )
60
+ event , hint = event_from_exception (
61
+ exc ,
62
+ client_options = sentry_sdk .get_client ().options ,
63
+ mechanism = {"type" : mechanism_type , "handled" : False },
64
+ )
65
+ sentry_sdk .capture_event (event , hint = hint )
68
66
69
67
70
68
def _looks_like_asgi3 (app ):
@@ -157,19 +155,17 @@ async def _run_app(self, scope, receive, send, asgi_version):
157
155
return await self .app (scope , receive , send )
158
156
159
157
except Exception as exc :
160
- _capture_exception (Hub . current , exc , mechanism_type = self .mechanism_type )
158
+ _capture_exception (exc , mechanism_type = self .mechanism_type )
161
159
raise exc from None
162
160
163
161
_asgi_middleware_applied .set (True )
164
162
try :
165
- hub = Hub (Hub .current )
166
- with hub :
167
- with auto_session_tracking (hub , session_mode = "request" ):
168
- with hub .configure_scope () as sentry_scope :
169
- sentry_scope .clear_breadcrumbs ()
170
- sentry_scope ._name = "asgi"
171
- processor = partial (self .event_processor , asgi_scope = scope )
172
- sentry_scope .add_event_processor (processor )
163
+ with sentry_sdk .isolation_scope () as sentry_scope :
164
+ with auto_session_tracking_scope (sentry_scope , session_mode = "request" ):
165
+ sentry_scope .clear_breadcrumbs ()
166
+ sentry_scope ._name = "asgi"
167
+ processor = partial (self .event_processor , asgi_scope = scope )
168
+ sentry_scope .add_event_processor (processor )
173
169
174
170
ty = scope ["type" ]
175
171
(
@@ -208,7 +204,7 @@ async def _run_app(self, scope, receive, send, asgi_version):
208
204
transaction .source ,
209
205
)
210
206
211
- with hub .start_transaction (
207
+ with sentry_sdk .start_transaction (
212
208
transaction , custom_sampling_context = {"asgi_scope" : scope }
213
209
):
214
210
logger .debug ("[ASGI] Started transaction: %s" , transaction )
@@ -235,9 +231,7 @@ async def _sentry_wrapped_send(event):
235
231
scope , receive , _sentry_wrapped_send
236
232
)
237
233
except Exception as exc :
238
- _capture_exception (
239
- hub , exc , mechanism_type = self .mechanism_type
240
- )
234
+ _capture_exception (exc , mechanism_type = self .mechanism_type )
241
235
raise exc from None
242
236
finally :
243
237
_asgi_middleware_applied .set (False )
0 commit comments