24
24
if MYPY :
25
25
from typing import Any , Awaitable , Callable , Dict , Optional
26
26
27
- from sentry_sdk ._types import Event
27
+ from sentry_sdk .scope import Scope as SentryScope
28
28
29
29
try :
30
30
import starlette # type: ignore
36
36
)
37
37
from starlette .requests import Request # type: ignore
38
38
from starlette .routing import Match # type: ignore
39
- from starlette .types import ASGIApp , Receive , Scope , Send # type: ignore
39
+ from starlette .types import ASGIApp , Receive , Scope as StarletteScope , Send # type: ignore
40
40
except ImportError :
41
41
raise DidNotEnable ("Starlette is not installed" )
42
42
@@ -312,7 +312,7 @@ def patch_asgi_app():
312
312
old_app = Starlette .__call__
313
313
314
314
async def _sentry_patched_asgi_app (self , scope , receive , send ):
315
- # type: (Starlette, Scope , Receive, Send) -> None
315
+ # type: (Starlette, StarletteScope , Receive, Send) -> None
316
316
if Hub .current .get_integration (StarletteIntegration ) is None :
317
317
return await old_app (self , scope , receive , send )
318
318
@@ -359,6 +359,11 @@ async def _sentry_async_func(*args, **kwargs):
359
359
360
360
with hub .configure_scope () as sentry_scope :
361
361
request = args [0 ]
362
+
363
+ _set_transaction_name_and_source (
364
+ sentry_scope , integration .transaction_style , request
365
+ )
366
+
362
367
extractor = StarletteRequestExtractor (request )
363
368
info = await extractor .extract_request_info ()
364
369
@@ -376,10 +381,6 @@ def event_processor(event, hint):
376
381
request_info ["data" ] = info ["data" ]
377
382
event ["request" ] = request_info
378
383
379
- _set_transaction_name_and_source (
380
- event , integration .transaction_style , req
381
- )
382
-
383
384
return event
384
385
385
386
return event_processor
@@ -403,6 +404,11 @@ def _sentry_sync_func(*args, **kwargs):
403
404
404
405
with hub .configure_scope () as sentry_scope :
405
406
request = args [0 ]
407
+
408
+ _set_transaction_name_and_source (
409
+ sentry_scope , integration .transaction_style , request
410
+ )
411
+
406
412
extractor = StarletteRequestExtractor (request )
407
413
cookies = extractor .extract_cookies_from_request ()
408
414
@@ -418,10 +424,6 @@ def event_processor(event, hint):
418
424
419
425
event ["request" ] = request_info
420
426
421
- _set_transaction_name_and_source (
422
- event , integration .transaction_style , req
423
- )
424
-
425
427
return event
426
428
427
429
return event_processor
@@ -550,8 +552,8 @@ async def json(self):
550
552
return await self .request .json ()
551
553
552
554
553
- def _set_transaction_name_and_source (event , transaction_style , request ):
554
- # type: (Event , str, Any) -> None
555
+ def _set_transaction_name_and_source (scope , transaction_style , request ):
556
+ # type: (SentryScope , str, Any) -> None
555
557
name = ""
556
558
557
559
if transaction_style == "endpoint" :
@@ -573,9 +575,9 @@ def _set_transaction_name_and_source(event, transaction_style, request):
573
575
break
574
576
575
577
if not name :
576
- event ["transaction" ] = _DEFAULT_TRANSACTION_NAME
577
- event ["transaction_info" ] = {"source" : TRANSACTION_SOURCE_ROUTE }
578
- return
578
+ name = _DEFAULT_TRANSACTION_NAME
579
+ source = TRANSACTION_SOURCE_ROUTE
580
+ else :
581
+ source = SOURCE_FOR_STYLE [transaction_style ]
579
582
580
- event ["transaction" ] = name
581
- event ["transaction_info" ] = {"source" : SOURCE_FOR_STYLE [transaction_style ]}
583
+ scope .set_transaction_name (name , source = source )
0 commit comments