2929from sentry_sdk .scope import Scope , should_send_default_pii
3030from sentry_sdk .sessions import track_session
3131from sentry_sdk .traces import (
32- SOURCE_FOR_STYLE ,
3332 SegmentNameSource ,
3433 Span ,
3534)
4039 event_from_exception ,
4140 has_data_collection_enabled ,
4241 logger ,
43- qualname_from_function ,
4442 reraise ,
45- transaction_from_function ,
4643)
4744
4845if TYPE_CHECKING :
5754
5855_DEFAULT_TRANSACTION_NAME = "generic ASGI request"
5956
60- TRANSACTION_STYLE_VALUES = ("endpoint" , "url" )
61-
6257
6358# Vendored: https://github.com/Kludex/uvicorn/blob/b224045f5900b7f766743bcb16ba9fc3adea2606/uvicorn/_compat.py#L10-L13
6459if sys .version_info >= (3 , 14 ):
@@ -95,7 +90,6 @@ class SentryAsgiMiddleware:
9590 __slots__ = (
9691 "app" ,
9792 "__call__" ,
98- "transaction_style" ,
9993 "mechanism_type" ,
10094 "span_origin" ,
10195 "http_methods_to_capture" ,
@@ -105,7 +99,6 @@ class SentryAsgiMiddleware:
10599 def __init__ (
106100 self ,
107101 app : "Any" ,
108- transaction_style : str = "endpoint" ,
109102 mechanism_type : str = "asgi" ,
110103 span_origin : str = "manual" ,
111104 http_methods_to_capture : "Tuple[str, ...]" = DEFAULT_HTTP_METHODS_TO_CAPTURE ,
@@ -118,12 +111,6 @@ def __init__(
118111 through the middleware.
119112
120113 """
121- if transaction_style not in TRANSACTION_STYLE_VALUES :
122- raise ValueError (
123- "Invalid value for transaction_style: %s (must be in %s)"
124- % (transaction_style , TRANSACTION_STYLE_VALUES )
125- )
126-
127114 asgi_middleware_while_using_starlette_or_fastapi = (
128115 mechanism_type == "asgi" and "starlette" in _get_installed_modules ()
129116 )
@@ -134,7 +121,6 @@ def __init__(
134121 "See https://docs.sentry.io/platforms/python/guides/asgi/ for more information."
135122 )
136123
137- self .transaction_style = transaction_style
138124 self .mechanism_type = mechanism_type
139125 self .span_origin = span_origin
140126 self .app = app
@@ -224,10 +210,7 @@ async def _run_app(
224210 (
225211 transaction_name ,
226212 transaction_source ,
227- ) = self ._get_transaction_name_and_source (
228- self .transaction_style ,
229- scope ,
230- )
213+ ) = self ._get_transaction_name_and_source (scope )
231214
232215 method = scope .get ("method" , "" ).upper ()
233216
@@ -336,9 +319,7 @@ async def _sentry_wrapped_send(
336319 with capture_internal_exceptions ():
337320 if not already_set :
338321 name , source = (
339- self ._get_segment_name_and_source (
340- self .transaction_style , scope
341- )
322+ self ._get_segment_name_and_source (scope )
342323 )
343324 span .name = name
344325 span .set_attribute (
@@ -370,9 +351,7 @@ def event_processor(
370351 ]
371352 )
372353 if not already_set :
373- name , source = self ._get_transaction_name_and_source (
374- self .transaction_style , asgi_scope
375- )
354+ name , source = self ._get_transaction_name_and_source (asgi_scope )
376355 event ["transaction" ] = name
377356 event ["transaction_info" ] = {"source" : source }
378357
@@ -385,48 +364,28 @@ def event_processor(
385364 # for that.
386365
387366 def _get_transaction_name_and_source (
388- self : "SentryAsgiMiddleware" , transaction_style : str , asgi_scope : "Any"
367+ self : "SentryAsgiMiddleware" , asgi_scope : "Any"
389368 ) -> "Tuple[str, str]" :
390369 name = None
391- source = SOURCE_FOR_STYLE [transaction_style ]
392- ty = asgi_scope .get ("type" )
393-
394- if transaction_style == "endpoint" :
395- endpoint = asgi_scope .get ("endpoint" )
396- # Webframeworks like Starlette mutate the ASGI env once routing is
397- # done, which is sometime after the request has started. If we have
398- # an endpoint, overwrite our generic transaction name.
399- if endpoint :
400- name = transaction_from_function (endpoint ) or ""
401- else :
402- name = _get_url (
403- asgi_scope ,
404- "http" if ty == "http" else "ws" ,
405- host = None ,
406- path = _get_path (
407- asgi_scope = asgi_scope , root_path_in_path = self .root_path_in_path
408- ),
409- )
410- source = SegmentNameSource .URL
411-
412- elif transaction_style == "url" :
413- # FastAPI includes the route object in the scope to let Sentry extract the
414- # path from it for the transaction name
415- route = asgi_scope .get ("route" )
416- if route :
417- path = getattr (route , "path" , None )
418- if path is not None :
419- name = path
420- else :
421- name = _get_url (
422- asgi_scope ,
423- "http" if ty == "http" else "ws" ,
424- host = None ,
425- path = _get_path (
426- asgi_scope = asgi_scope , root_path_in_path = self .root_path_in_path
427- ),
428- )
429- source = SegmentNameSource .URL
370+ source = SegmentNameSource .ROUTE
371+
372+ # FastAPI includes the route object in the scope to let Sentry extract the
373+ # path from it for the transaction name
374+ route = asgi_scope .get ("route" )
375+ if route :
376+ path = getattr (route , "path" , None )
377+ if path is not None :
378+ name = path
379+ else :
380+ name = _get_url (
381+ asgi_scope ,
382+ "http" if asgi_scope .get ("type" ) == "http" else "ws" ,
383+ host = None ,
384+ path = _get_path (
385+ asgi_scope = asgi_scope , root_path_in_path = self .root_path_in_path
386+ ),
387+ )
388+ source = SegmentNameSource .URL
430389
431390 if name is None :
432391 name = _DEFAULT_TRANSACTION_NAME
@@ -436,48 +395,29 @@ def _get_transaction_name_and_source(
436395 return name , source
437396
438397 def _get_segment_name_and_source (
439- self : "SentryAsgiMiddleware" , segment_style : str , asgi_scope : "Any"
398+ self : "SentryAsgiMiddleware" , asgi_scope : "Any"
440399 ) -> "Tuple[str, str]" :
441400 name = None
442- source = SOURCE_FOR_STYLE [ segment_style ] .value
401+ source = SegmentNameSource . ROUTE .value
443402 ty = asgi_scope .get ("type" )
444403
445- if segment_style == "endpoint" :
446- endpoint = asgi_scope .get ("endpoint" )
447- # Webframeworks like Starlette mutate the ASGI env once routing is
448- # done, which is sometime after the request has started. If we have
449- # an endpoint, overwrite our generic transaction name.
450- if endpoint :
451- name = qualname_from_function (endpoint ) or ""
452- else :
453- name = _get_url (
454- asgi_scope ,
455- "http" if ty == "http" else "ws" ,
456- host = None ,
457- path = _get_path (
458- asgi_scope = asgi_scope , root_path_in_path = self .root_path_in_path
459- ),
460- )
461- source = SegmentNameSource .URL .value
462-
463- elif segment_style == "url" :
464- # FastAPI includes the route object in the scope to let Sentry extract the
465- # path from it for the transaction name
466- route = asgi_scope .get ("route" )
467- if route :
468- path = getattr (route , "path" , None )
469- if path is not None :
470- name = path
471- else :
472- name = _get_url (
473- asgi_scope ,
474- "http" if ty == "http" else "ws" ,
475- host = None ,
476- path = _get_path (
477- asgi_scope = asgi_scope , root_path_in_path = self .root_path_in_path
478- ),
479- )
480- source = SegmentNameSource .URL .value
404+ # FastAPI includes the route object in the scope to let Sentry extract the
405+ # path from it for the transaction name
406+ route = asgi_scope .get ("route" )
407+ if route :
408+ path = getattr (route , "path" , None )
409+ if path is not None :
410+ name = path
411+ else :
412+ name = _get_url (
413+ asgi_scope ,
414+ "http" if ty == "http" else "ws" ,
415+ host = None ,
416+ path = _get_path (
417+ asgi_scope = asgi_scope , root_path_in_path = self .root_path_in_path
418+ ),
419+ )
420+ source = SegmentNameSource .URL .value
481421
482422 if name is None :
483423 name = _DEFAULT_TRANSACTION_NAME
0 commit comments