@@ -176,26 +176,31 @@ async def _run_app(self, scope, receive, send, asgi_version):
176176 _asgi_middleware_applied .set (True )
177177 try :
178178 with sentry_sdk .isolation_scope () as sentry_scope :
179+ (
180+ transaction_name ,
181+ transaction_source ,
182+ ) = self ._get_transaction_name_and_source (
183+ self .transaction_style ,
184+ scope ,
185+ )
186+ sentry_scope .set_transaction_name (
187+ transaction_name ,
188+ source = transaction_source ,
189+ )
190+
179191 with track_session (sentry_scope , session_mode = "request" ):
180192 sentry_scope .clear_breadcrumbs ()
181193 sentry_scope ._name = "asgi"
182194 processor = partial (self .event_processor , asgi_scope = scope )
183195 sentry_scope .add_event_processor (processor )
184196
185197 ty = scope ["type" ]
186- (
187- transaction_name ,
188- transaction_source ,
189- ) = self ._get_transaction_name_and_source (
190- self .transaction_style ,
191- scope ,
192- )
193198
194199 method = scope .get ("method" , "" ).upper ()
195200 should_trace = method in self .http_methods_to_capture
196201 with sentry_sdk .continue_trace (_get_headers (scope )):
197202 with (
198- sentry_sdk .start_transaction (
203+ sentry_sdk .start_span (
199204 op = (
200205 OP .WEBSOCKET_SERVER
201206 if ty == "websocket"
@@ -251,13 +256,18 @@ def event_processor(self, event, hint, asgi_scope):
251256 event ["request" ] = deepcopy (request_data )
252257
253258 # Only set transaction name if not already set by Starlette or FastAPI (or other frameworks)
254- already_set = event ["transaction" ] != _DEFAULT_TRANSACTION_NAME and event [
255- "transaction_info"
256- ].get ("source" ) in [
257- TRANSACTION_SOURCE_COMPONENT ,
258- TRANSACTION_SOURCE_ROUTE ,
259- TRANSACTION_SOURCE_CUSTOM ,
260- ]
259+ already_set = (
260+ "transaction" in event
261+ and event ["transaction" ] != _DEFAULT_TRANSACTION_NAME
262+ and "transaction_info" in event
263+ and "source" in event ["transaction_info" ]
264+ and event ["transaction_info" ]["source" ]
265+ in [
266+ TRANSACTION_SOURCE_COMPONENT ,
267+ TRANSACTION_SOURCE_ROUTE ,
268+ TRANSACTION_SOURCE_CUSTOM ,
269+ ]
270+ )
261271 if not already_set :
262272 name , source = self ._get_transaction_name_and_source (
263273 self .transaction_style , asgi_scope
0 commit comments