2424if MYPY :
2525 from typing import Any , Awaitable , Callable , Dict , Optional
2626
27- from sentry_sdk ._types import Event
27+ from sentry_sdk .scope import Scope as SentryScope
2828
2929try :
3030 import starlette # type: ignore
3636 )
3737 from starlette .requests import Request # type: ignore
3838 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
4040except ImportError :
4141 raise DidNotEnable ("Starlette is not installed" )
4242
@@ -312,7 +312,7 @@ def patch_asgi_app():
312312 old_app = Starlette .__call__
313313
314314 async def _sentry_patched_asgi_app (self , scope , receive , send ):
315- # type: (Starlette, Scope , Receive, Send) -> None
315+ # type: (Starlette, StarletteScope , Receive, Send) -> None
316316 if Hub .current .get_integration (StarletteIntegration ) is None :
317317 return await old_app (self , scope , receive , send )
318318
@@ -359,6 +359,11 @@ async def _sentry_async_func(*args, **kwargs):
359359
360360 with hub .configure_scope () as sentry_scope :
361361 request = args [0 ]
362+
363+ _set_transaction_name_and_source (
364+ sentry_scope , integration .transaction_style , request
365+ )
366+
362367 extractor = StarletteRequestExtractor (request )
363368 info = await extractor .extract_request_info ()
364369
@@ -376,10 +381,6 @@ def event_processor(event, hint):
376381 request_info ["data" ] = info ["data" ]
377382 event ["request" ] = request_info
378383
379- _set_transaction_name_and_source (
380- event , integration .transaction_style , req
381- )
382-
383384 return event
384385
385386 return event_processor
@@ -403,6 +404,11 @@ def _sentry_sync_func(*args, **kwargs):
403404
404405 with hub .configure_scope () as sentry_scope :
405406 request = args [0 ]
407+
408+ _set_transaction_name_and_source (
409+ sentry_scope , integration .transaction_style , request
410+ )
411+
406412 extractor = StarletteRequestExtractor (request )
407413 cookies = extractor .extract_cookies_from_request ()
408414
@@ -418,10 +424,6 @@ def event_processor(event, hint):
418424
419425 event ["request" ] = request_info
420426
421- _set_transaction_name_and_source (
422- event , integration .transaction_style , req
423- )
424-
425427 return event
426428
427429 return event_processor
@@ -550,8 +552,8 @@ async def json(self):
550552 return await self .request .json ()
551553
552554
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
555557 name = ""
556558
557559 if transaction_style == "endpoint" :
@@ -573,9 +575,9 @@ def _set_transaction_name_and_source(event, transaction_style, request):
573575 break
574576
575577 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 ]
579582
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