3
3
from inspect import isawaitable
4
4
from urllib .parse import urlsplit
5
5
6
+ import sentry_sdk
6
7
from sentry_sdk import continue_trace
7
8
from sentry_sdk .consts import OP
8
- from sentry_sdk .hub import Hub
9
+ from sentry_sdk .integrations import Integration , DidNotEnable
10
+ from sentry_sdk .integrations ._wsgi_common import RequestExtractor , _filter_headers
11
+ from sentry_sdk .integrations .logging import ignore_logger
9
12
from sentry_sdk .tracing import TRANSACTION_SOURCE_COMPONENT , TRANSACTION_SOURCE_URL
10
13
from sentry_sdk .scope import Scope
11
14
from sentry_sdk .utils import (
16
19
parse_version ,
17
20
reraise ,
18
21
)
19
- from sentry_sdk .integrations import Integration , DidNotEnable
20
- from sentry_sdk .integrations ._wsgi_common import RequestExtractor , _filter_headers
21
- from sentry_sdk .integrations .logging import ignore_logger
22
-
23
22
from sentry_sdk ._types import TYPE_CHECKING
24
23
25
24
if TYPE_CHECKING :
@@ -162,13 +161,13 @@ async def _startup(self):
162
161
# type: (Sanic) -> None
163
162
# This happens about as early in the lifecycle as possible, just after the
164
163
# Request object is created. The body has not yet been consumed.
165
- self .signal ("http.lifecycle.request" )(_hub_enter )
164
+ self .signal ("http.lifecycle.request" )(_context_enter )
166
165
167
166
# This happens after the handler is complete. In v21.9 this signal is not
168
167
# dispatched when there is an exception. Therefore we need to close out
169
- # and call _hub_exit from the custom exception handler as well.
168
+ # and call _context_exit from the custom exception handler as well.
170
169
# See https://github.com/sanic-org/sanic/issues/2297
171
- self .signal ("http.lifecycle.response" )(_hub_exit )
170
+ self .signal ("http.lifecycle.response" )(_context_exit )
172
171
173
172
# This happens inside of request handling immediately after the route
174
173
# has been identified by the router.
@@ -178,23 +177,20 @@ async def _startup(self):
178
177
await old_startup (self )
179
178
180
179
181
- async def _hub_enter (request ):
180
+ async def _context_enter (request ):
182
181
# type: (Request) -> None
183
- hub = Hub .current
184
182
request .ctx ._sentry_do_integration = (
185
- hub .get_integration (SanicIntegration ) is not None
183
+ sentry_sdk . get_client () .get_integration (SanicIntegration ) is not None
186
184
)
187
185
188
186
if not request .ctx ._sentry_do_integration :
189
187
return
190
188
191
189
weak_request = weakref .ref (request )
192
- request .ctx ._sentry_hub = Hub (hub )
193
- request .ctx ._sentry_hub .__enter__ ()
194
-
195
- with request .ctx ._sentry_hub .configure_scope () as scope :
196
- scope .clear_breadcrumbs ()
197
- scope .add_event_processor (_make_request_processor (weak_request ))
190
+ request .ctx ._sentry_scope = sentry_sdk .isolation_scope ()
191
+ scope = request .ctx ._sentry_scope .__enter__ ()
192
+ scope .clear_breadcrumbs ()
193
+ scope .add_event_processor (_make_request_processor (weak_request ))
198
194
199
195
transaction = continue_trace (
200
196
dict (request .headers ),
@@ -203,18 +199,20 @@ async def _hub_enter(request):
203
199
name = request .path ,
204
200
source = TRANSACTION_SOURCE_URL ,
205
201
)
206
- request .ctx ._sentry_transaction = request . ctx . _sentry_hub .start_transaction (
202
+ request .ctx ._sentry_transaction = sentry_sdk .start_transaction (
207
203
transaction
208
204
).__enter__ ()
209
205
210
206
211
- async def _hub_exit (request , response = None ):
207
+ async def _context_exit (request , response = None ):
212
208
# type: (Request, Optional[BaseHTTPResponse]) -> None
213
209
with capture_internal_exceptions ():
214
210
if not request .ctx ._sentry_do_integration :
215
211
return
216
212
217
- integration = Hub .current .get_integration (SanicIntegration ) # type: Integration
213
+ integration = sentry_sdk .get_client ().get_integration (
214
+ SanicIntegration
215
+ ) # type: Integration
218
216
219
217
response_status = None if response is None else response .status
220
218
@@ -228,7 +226,7 @@ async def _hub_exit(request, response=None):
228
226
)
229
227
request .ctx ._sentry_transaction .__exit__ (None , None , None )
230
228
231
- request .ctx ._sentry_hub .__exit__ (None , None , None )
229
+ request .ctx ._sentry_scope .__exit__ (None , None , None )
232
230
233
231
234
232
async def _set_transaction (request , route , ** _ ):
@@ -248,7 +246,7 @@ def _sentry_error_handler_lookup(self, exception, *args, **kwargs):
248
246
if old_error_handler is None :
249
247
return None
250
248
251
- if Hub . current .get_integration (SanicIntegration ) is None :
249
+ if sentry_sdk . get_client () .get_integration (SanicIntegration ) is None :
252
250
return old_error_handler
253
251
254
252
async def sentry_wrapped_error_handler (request , exception ):
@@ -269,23 +267,21 @@ async def sentry_wrapped_error_handler(request, exception):
269
267
# As mentioned in previous comment in _startup, this can be removed
270
268
# after https://github.com/sanic-org/sanic/issues/2297 is resolved
271
269
if SanicIntegration .version and SanicIntegration .version == (21 , 9 ):
272
- await _hub_exit (request )
270
+ await _context_exit (request )
273
271
274
272
return sentry_wrapped_error_handler
275
273
276
274
277
275
async def _legacy_handle_request (self , request , * args , ** kwargs ):
278
276
# type: (Any, Request, *Any, **Any) -> Any
279
- hub = Hub .current
280
- if hub .get_integration (SanicIntegration ) is None :
277
+ if sentry_sdk .get_client ().get_integration (SanicIntegration ) is None :
281
278
return old_handle_request (self , request , * args , ** kwargs )
282
279
283
280
weak_request = weakref .ref (request )
284
281
285
- with Hub (hub ) as hub :
286
- with hub .configure_scope () as scope :
287
- scope .clear_breadcrumbs ()
288
- scope .add_event_processor (_make_request_processor (weak_request ))
282
+ with sentry_sdk .isolation_scope () as scope :
283
+ scope .clear_breadcrumbs ()
284
+ scope .add_event_processor (_make_request_processor (weak_request ))
289
285
290
286
response = old_handle_request (self , request , * args , ** kwargs )
291
287
if isawaitable (response ):
@@ -297,51 +293,47 @@ async def _legacy_handle_request(self, request, *args, **kwargs):
297
293
def _legacy_router_get (self , * args ):
298
294
# type: (Any, Union[Any, Request]) -> Any
299
295
rv = old_router_get (self , * args )
300
- hub = Hub .current
301
- if hub .get_integration (SanicIntegration ) is not None :
296
+ if sentry_sdk .get_client ().get_integration (SanicIntegration ) is not None :
302
297
with capture_internal_exceptions ():
303
- with hub . configure_scope () as scope :
304
- if SanicIntegration .version and SanicIntegration .version >= (21 , 3 ):
305
- # Sanic versions above and including 21.3 append the app name to the
306
- # route name, and so we need to remove it from Route name so the
307
- # transaction name is consistent across all versions
308
- sanic_app_name = self .ctx .app .name
309
- sanic_route = rv [0 ].name
310
-
311
- if sanic_route .startswith ("%s." % sanic_app_name ):
312
- # We add a 1 to the len of the sanic_app_name because there is a dot
313
- # that joins app name and the route name
314
- # Format: app_name.route_name
315
- sanic_route = sanic_route [len (sanic_app_name ) + 1 :]
316
-
317
- scope .set_transaction_name (
318
- sanic_route , source = TRANSACTION_SOURCE_COMPONENT
319
- )
320
- else :
321
- scope .set_transaction_name (
322
- rv [0 ].__name__ , source = TRANSACTION_SOURCE_COMPONENT
323
- )
298
+ scope = Scope . get_isolation_scope ()
299
+ if SanicIntegration .version and SanicIntegration .version >= (21 , 3 ):
300
+ # Sanic versions above and including 21.3 append the app name to the
301
+ # route name, and so we need to remove it from Route name so the
302
+ # transaction name is consistent across all versions
303
+ sanic_app_name = self .ctx .app .name
304
+ sanic_route = rv [0 ].name
305
+
306
+ if sanic_route .startswith ("%s." % sanic_app_name ):
307
+ # We add a 1 to the len of the sanic_app_name because there is a dot
308
+ # that joins app name and the route name
309
+ # Format: app_name.route_name
310
+ sanic_route = sanic_route [len (sanic_app_name ) + 1 :]
311
+
312
+ scope .set_transaction_name (
313
+ sanic_route , source = TRANSACTION_SOURCE_COMPONENT
314
+ )
315
+ else :
316
+ scope .set_transaction_name (
317
+ rv [0 ].__name__ , source = TRANSACTION_SOURCE_COMPONENT
318
+ )
324
319
325
320
return rv
326
321
327
322
328
323
def _capture_exception (exception ):
329
324
# type: (Union[Tuple[Optional[type], Optional[BaseException], Any], BaseException]) -> None
330
- hub = Hub . current
331
- integration = hub .get_integration (SanicIntegration )
325
+ client = sentry_sdk . get_client ()
326
+ integration = client .get_integration (SanicIntegration )
332
327
if integration is None :
333
328
return
334
329
335
- # If an integration is there, a client has to be there.
336
- client = hub .client # type: Any
337
-
338
330
with capture_internal_exceptions ():
339
331
event , hint = event_from_exception (
340
332
exception ,
341
333
client_options = client .options ,
342
334
mechanism = {"type" : "sanic" , "handled" : False },
343
335
)
344
- hub .capture_event (event , hint = hint )
336
+ sentry_sdk .capture_event (event , hint = hint )
345
337
346
338
347
339
def _make_request_processor (weak_request ):
0 commit comments