Skip to content

Commit 29d9d8f

Browse files
ref(asgi): Remove transaction_style parameter (#7796)
Remove the `transaction_style` parameter from all integrations that rely on `SentryAsgiMiddleware` by inlining the branches that set the route template as the segment name (i.e., `transaction_style="url"`)." Closes #7765 Closes #7760 Closes #7762 Closes #7759 Closes #7762
1 parent 61a562c commit 29d9d8f

8 files changed

Lines changed: 115 additions & 530 deletions

File tree

‎sentry_sdk/integrations/asgi.py‎

Lines changed: 42 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from sentry_sdk.scope import Scope, should_send_default_pii
3030
from sentry_sdk.sessions import track_session
3131
from sentry_sdk.traces import (
32-
SOURCE_FOR_STYLE,
3332
SegmentNameSource,
3433
Span,
3534
)
@@ -40,9 +39,7 @@
4039
event_from_exception,
4140
has_data_collection_enabled,
4241
logger,
43-
qualname_from_function,
4442
reraise,
45-
transaction_from_function,
4643
)
4744

4845
if TYPE_CHECKING:
@@ -57,8 +54,6 @@
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
6459
if 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

‎sentry_sdk/integrations/fastapi.py‎

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,17 @@
77
from sentry_sdk.consts import SPANDATA
88
from sentry_sdk.integrations import DidNotEnable, _check_minimum_version
99
from sentry_sdk.traces import (
10-
SOURCE_FOR_STYLE,
1110
SegmentNameSource,
1211
Span,
1312
get_current_span,
1413
)
1514
from sentry_sdk.utils import (
1615
has_data_collection_enabled,
1716
parse_version,
18-
transaction_from_function,
1917
)
2018

2119
if TYPE_CHECKING:
22-
from typing import Any, Awaitable, Callable, Dict, Optional
20+
from typing import Any, Awaitable, Callable, Dict
2321

2422
from sentry_sdk._types import Event
2523

@@ -60,29 +58,6 @@ def setup_once() -> None:
6058
patch_get_request_handler()
6159

6260

63-
def _set_transaction_name_and_source(
64-
scope: "sentry_sdk.Scope",
65-
transaction_style: str,
66-
endpoint: "Optional[Callable[..., Any]]",
67-
route_path: "Optional[str]",
68-
) -> None:
69-
name = ""
70-
71-
if transaction_style == "endpoint" and endpoint:
72-
name = transaction_from_function(endpoint) or ""
73-
74-
elif transaction_style == "url" and route_path is not None:
75-
name = route_path
76-
77-
if not name:
78-
name = _DEFAULT_TRANSACTION_NAME
79-
source = SegmentNameSource.ROUTE
80-
else:
81-
source = SOURCE_FOR_STYLE[transaction_style]
82-
83-
scope.set_transaction_name(name, source=source)
84-
85-
8661
async def _wrap_async_handler(
8762
handler: "Callable[..., Awaitable[Any]]", *args: "Any", **kwargs: "Any"
8863
) -> "Any":
@@ -122,11 +97,9 @@ async def _wrap_async_handler(
12297
if server_span is not None and route_path is not None:
12398
server_span.set_attribute(SPANDATA.HTTP_ROUTE, route_path)
12499

125-
_set_transaction_name_and_source(
126-
sentry_sdk.get_current_scope(),
127-
integration.transaction_style,
128-
endpoint=request.scope.get("endpoint"),
129-
route_path=route_path,
100+
sentry_sdk.get_current_scope().set_transaction_name(
101+
route_path if route_path is not None else _DEFAULT_TRANSACTION_NAME,
102+
source=SegmentNameSource.ROUTE,
130103
)
131104
sentry_scope = sentry_sdk.get_isolation_scope()
132105
extractor = StarletteRequestExtractor(request)

‎sentry_sdk/integrations/litestar.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ def __init__(
9191
) -> None:
9292
super().__init__(
9393
app=app,
94-
transaction_style="endpoint",
9594
mechanism_type="asgi",
9695
span_origin=span_origin,
9796
asgi_version=3,

0 commit comments

Comments
 (0)