diff --git a/sentry_sdk/integrations/asgi.py b/sentry_sdk/integrations/asgi.py index cd8f5d6aa9..dc0ac81888 100644 --- a/sentry_sdk/integrations/asgi.py +++ b/sentry_sdk/integrations/asgi.py @@ -29,7 +29,6 @@ from sentry_sdk.scope import Scope, should_send_default_pii from sentry_sdk.sessions import track_session from sentry_sdk.traces import ( - SOURCE_FOR_STYLE, SegmentNameSource, Span, ) @@ -40,9 +39,7 @@ event_from_exception, has_data_collection_enabled, logger, - qualname_from_function, reraise, - transaction_from_function, ) if TYPE_CHECKING: @@ -57,8 +54,6 @@ _DEFAULT_TRANSACTION_NAME = "generic ASGI request" -TRANSACTION_STYLE_VALUES = ("endpoint", "url") - # Vendored: https://github.com/Kludex/uvicorn/blob/b224045f5900b7f766743bcb16ba9fc3adea2606/uvicorn/_compat.py#L10-L13 if sys.version_info >= (3, 14): @@ -95,7 +90,6 @@ class SentryAsgiMiddleware: __slots__ = ( "app", "__call__", - "transaction_style", "mechanism_type", "span_origin", "http_methods_to_capture", @@ -105,7 +99,6 @@ class SentryAsgiMiddleware: def __init__( self, app: "Any", - transaction_style: str = "endpoint", mechanism_type: str = "asgi", span_origin: str = "manual", http_methods_to_capture: "Tuple[str, ...]" = DEFAULT_HTTP_METHODS_TO_CAPTURE, @@ -118,12 +111,6 @@ def __init__( through the middleware. """ - if transaction_style not in TRANSACTION_STYLE_VALUES: - raise ValueError( - "Invalid value for transaction_style: %s (must be in %s)" - % (transaction_style, TRANSACTION_STYLE_VALUES) - ) - asgi_middleware_while_using_starlette_or_fastapi = ( mechanism_type == "asgi" and "starlette" in _get_installed_modules() ) @@ -134,7 +121,6 @@ def __init__( "See https://docs.sentry.io/platforms/python/guides/asgi/ for more information." ) - self.transaction_style = transaction_style self.mechanism_type = mechanism_type self.span_origin = span_origin self.app = app @@ -224,10 +210,7 @@ async def _run_app( ( transaction_name, transaction_source, - ) = self._get_transaction_name_and_source( - self.transaction_style, - scope, - ) + ) = self._get_transaction_name_and_source(scope) method = scope.get("method", "").upper() @@ -336,9 +319,7 @@ async def _sentry_wrapped_send( with capture_internal_exceptions(): if not already_set: name, source = ( - self._get_segment_name_and_source( - self.transaction_style, scope - ) + self._get_segment_name_and_source(scope) ) span.name = name span.set_attribute( @@ -370,9 +351,7 @@ def event_processor( ] ) if not already_set: - name, source = self._get_transaction_name_and_source( - self.transaction_style, asgi_scope - ) + name, source = self._get_transaction_name_and_source(asgi_scope) event["transaction"] = name event["transaction_info"] = {"source": source} @@ -385,48 +364,28 @@ def event_processor( # for that. def _get_transaction_name_and_source( - self: "SentryAsgiMiddleware", transaction_style: str, asgi_scope: "Any" + self: "SentryAsgiMiddleware", asgi_scope: "Any" ) -> "Tuple[str, str]": name = None - source = SOURCE_FOR_STYLE[transaction_style] - ty = asgi_scope.get("type") - - if transaction_style == "endpoint": - endpoint = asgi_scope.get("endpoint") - # Webframeworks like Starlette mutate the ASGI env once routing is - # done, which is sometime after the request has started. If we have - # an endpoint, overwrite our generic transaction name. - if endpoint: - name = transaction_from_function(endpoint) or "" - else: - name = _get_url( - asgi_scope, - "http" if ty == "http" else "ws", - host=None, - path=_get_path( - asgi_scope=asgi_scope, root_path_in_path=self.root_path_in_path - ), - ) - source = SegmentNameSource.URL - - elif transaction_style == "url": - # FastAPI includes the route object in the scope to let Sentry extract the - # path from it for the transaction name - route = asgi_scope.get("route") - if route: - path = getattr(route, "path", None) - if path is not None: - name = path - else: - name = _get_url( - asgi_scope, - "http" if ty == "http" else "ws", - host=None, - path=_get_path( - asgi_scope=asgi_scope, root_path_in_path=self.root_path_in_path - ), - ) - source = SegmentNameSource.URL + source = SegmentNameSource.ROUTE + + # FastAPI includes the route object in the scope to let Sentry extract the + # path from it for the transaction name + route = asgi_scope.get("route") + if route: + path = getattr(route, "path", None) + if path is not None: + name = path + else: + name = _get_url( + asgi_scope, + "http" if asgi_scope.get("type") == "http" else "ws", + host=None, + path=_get_path( + asgi_scope=asgi_scope, root_path_in_path=self.root_path_in_path + ), + ) + source = SegmentNameSource.URL if name is None: name = _DEFAULT_TRANSACTION_NAME @@ -436,48 +395,29 @@ def _get_transaction_name_and_source( return name, source def _get_segment_name_and_source( - self: "SentryAsgiMiddleware", segment_style: str, asgi_scope: "Any" + self: "SentryAsgiMiddleware", asgi_scope: "Any" ) -> "Tuple[str, str]": name = None - source = SOURCE_FOR_STYLE[segment_style].value + source = SegmentNameSource.ROUTE.value ty = asgi_scope.get("type") - if segment_style == "endpoint": - endpoint = asgi_scope.get("endpoint") - # Webframeworks like Starlette mutate the ASGI env once routing is - # done, which is sometime after the request has started. If we have - # an endpoint, overwrite our generic transaction name. - if endpoint: - name = qualname_from_function(endpoint) or "" - else: - name = _get_url( - asgi_scope, - "http" if ty == "http" else "ws", - host=None, - path=_get_path( - asgi_scope=asgi_scope, root_path_in_path=self.root_path_in_path - ), - ) - source = SegmentNameSource.URL.value - - elif segment_style == "url": - # FastAPI includes the route object in the scope to let Sentry extract the - # path from it for the transaction name - route = asgi_scope.get("route") - if route: - path = getattr(route, "path", None) - if path is not None: - name = path - else: - name = _get_url( - asgi_scope, - "http" if ty == "http" else "ws", - host=None, - path=_get_path( - asgi_scope=asgi_scope, root_path_in_path=self.root_path_in_path - ), - ) - source = SegmentNameSource.URL.value + # FastAPI includes the route object in the scope to let Sentry extract the + # path from it for the transaction name + route = asgi_scope.get("route") + if route: + path = getattr(route, "path", None) + if path is not None: + name = path + else: + name = _get_url( + asgi_scope, + "http" if ty == "http" else "ws", + host=None, + path=_get_path( + asgi_scope=asgi_scope, root_path_in_path=self.root_path_in_path + ), + ) + source = SegmentNameSource.URL.value if name is None: name = _DEFAULT_TRANSACTION_NAME diff --git a/sentry_sdk/integrations/fastapi.py b/sentry_sdk/integrations/fastapi.py index 064776d268..95bd6dd0de 100644 --- a/sentry_sdk/integrations/fastapi.py +++ b/sentry_sdk/integrations/fastapi.py @@ -7,7 +7,6 @@ from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations import DidNotEnable, _check_minimum_version from sentry_sdk.traces import ( - SOURCE_FOR_STYLE, SegmentNameSource, Span, get_current_span, @@ -15,11 +14,10 @@ from sentry_sdk.utils import ( has_data_collection_enabled, parse_version, - transaction_from_function, ) if TYPE_CHECKING: - from typing import Any, Awaitable, Callable, Dict, Optional + from typing import Any, Awaitable, Callable, Dict from sentry_sdk._types import Event @@ -60,29 +58,6 @@ def setup_once() -> None: patch_get_request_handler() -def _set_transaction_name_and_source( - scope: "sentry_sdk.Scope", - transaction_style: str, - endpoint: "Optional[Callable[..., Any]]", - route_path: "Optional[str]", -) -> None: - name = "" - - if transaction_style == "endpoint" and endpoint: - name = transaction_from_function(endpoint) or "" - - elif transaction_style == "url" and route_path is not None: - name = route_path - - if not name: - name = _DEFAULT_TRANSACTION_NAME - source = SegmentNameSource.ROUTE - else: - source = SOURCE_FOR_STYLE[transaction_style] - - scope.set_transaction_name(name, source=source) - - async def _wrap_async_handler( handler: "Callable[..., Awaitable[Any]]", *args: "Any", **kwargs: "Any" ) -> "Any": @@ -122,11 +97,9 @@ async def _wrap_async_handler( if server_span is not None and route_path is not None: server_span.set_attribute(SPANDATA.HTTP_ROUTE, route_path) - _set_transaction_name_and_source( - sentry_sdk.get_current_scope(), - integration.transaction_style, - endpoint=request.scope.get("endpoint"), - route_path=route_path, + sentry_sdk.get_current_scope().set_transaction_name( + route_path if route_path is not None else _DEFAULT_TRANSACTION_NAME, + source=SegmentNameSource.ROUTE, ) sentry_scope = sentry_sdk.get_isolation_scope() extractor = StarletteRequestExtractor(request) diff --git a/sentry_sdk/integrations/litestar.py b/sentry_sdk/integrations/litestar.py index 5e36b72058..3a49983bc3 100644 --- a/sentry_sdk/integrations/litestar.py +++ b/sentry_sdk/integrations/litestar.py @@ -91,7 +91,6 @@ def __init__( ) -> None: super().__init__( app=app, - transaction_style="endpoint", mechanism_type="asgi", span_origin=span_origin, asgi_version=3, diff --git a/sentry_sdk/integrations/starlette.py b/sentry_sdk/integrations/starlette.py index 8ce0ccfe78..8f4ecdf22a 100644 --- a/sentry_sdk/integrations/starlette.py +++ b/sentry_sdk/integrations/starlette.py @@ -24,14 +24,13 @@ request_body_within_bounds, ) from sentry_sdk.integrations.asgi import SentryAsgiMiddleware -from sentry_sdk.traces import SOURCE_FOR_STYLE, SegmentNameSource, Span +from sentry_sdk.traces import SegmentNameSource, Span from sentry_sdk.utils import ( AnnotatedValue, capture_internal_exceptions, ensure_integration_enabled, event_from_exception, parse_version, - transaction_from_function, ) if TYPE_CHECKING: @@ -86,28 +85,17 @@ _DEFAULT_TRANSACTION_NAME = "generic Starlette request" -TRANSACTION_STYLE_VALUES = ("endpoint", "url") - class StarletteIntegration(Integration): identifier = "starlette" origin = f"auto.http.{identifier}" - transaction_style = "" - def __init__( self, - transaction_style: str = "url", failed_request_status_codes: "Set[int]" = _DEFAULT_FAILED_REQUEST_STATUS_CODES, middleware_spans: bool = False, http_methods_to_capture: "tuple[str, ...]" = DEFAULT_HTTP_METHODS_TO_CAPTURE, ): - if transaction_style not in TRANSACTION_STYLE_VALUES: - raise ValueError( - "Invalid value for transaction_style: %s (must be in %s)" - % (transaction_style, TRANSACTION_STYLE_VALUES) - ) - self.transaction_style = transaction_style self.middleware_spans = middleware_spans self.http_methods_to_capture = tuple(map(str.upper, http_methods_to_capture)) @@ -161,14 +149,10 @@ async def _create_span_call( server_span.set_attribute(SPANDATA.HTTP_ROUTE, route_path) # Update transaction name with middleware name - name, source = _get_transaction_from_middleware( - app, integration, route_path=route_path, name_source=name_source - ) - - if name is not None: + if route_path is not None: sentry_sdk.get_current_scope().set_transaction_name( - name, - source=source, + route_path, + source=name_source, ) if not integration.middleware_spans: @@ -427,7 +411,6 @@ async def _sentry_patched_asgi_app( middleware = SentryAsgiMiddleware( lambda *a, **kw: old_app(self, *a, **kw), mechanism_type=StarletteIntegration.identifier, - transaction_style=integration.transaction_style, span_origin=StarletteIntegration.origin, http_methods_to_capture=( integration.http_methods_to_capture @@ -511,12 +494,9 @@ async def _wrap_async_handler( ): server_span.set_attribute(SPANDATA.HTTP_ROUTE, route_path) - _set_transaction_name_and_source( - sentry_sdk.get_current_scope(), - integration.transaction_style, - endpoint=request.scope.get("endpoint"), - route_path=route_path, - name_source=name_source, + sentry_sdk.get_current_scope().set_transaction_name( + route_path if route_path is not None else _DEFAULT_TRANSACTION_NAME, + source=name_source, ) sentry_scope = sentry_sdk.get_isolation_scope() @@ -611,12 +591,9 @@ def _sentry_sync_func(*args: "Any", **kwargs: "Any") -> "Any": ): server_span.set_attribute(SPANDATA.HTTP_ROUTE, route_path) - _set_transaction_name_and_source( - sentry_sdk.get_current_scope(), - integration.transaction_style, - endpoint=request.scope.get("endpoint"), - route_path=route_path, - name_source=name_source, + sentry_sdk.get_current_scope().set_transaction_name( + route_path if route_path is not None else _DEFAULT_TRANSACTION_NAME, + source=name_source, ) extractor = StarletteRequestExtractor(request) @@ -817,44 +794,3 @@ def _http_route_and_source_from_router( return scope.get("path"), SegmentNameSource.URL return None, SegmentNameSource.ROUTE - - -def _set_transaction_name_and_source( - scope: "sentry_sdk.Scope", - transaction_style: str, - endpoint: "Optional[Callable[..., Any]]", - route_path: "Optional[str]", - name_source: "SegmentNameSource", -) -> None: - name = None - source = SOURCE_FOR_STYLE[transaction_style] - - if transaction_style == "endpoint" and endpoint: - name = transaction_from_function(endpoint) or None - - elif transaction_style == "url": - name, source = route_path, name_source - - if name is None: - name = _DEFAULT_TRANSACTION_NAME - source = SegmentNameSource.ROUTE - - scope.set_transaction_name(name, source=source) - - -def _get_transaction_from_middleware( - app: "Any", - integration: "StarletteIntegration", - route_path: "Optional[str]", - name_source: "SegmentNameSource", -) -> "Tuple[Optional[str], Optional[str]]": - name = None - source = None - - if integration.transaction_style == "endpoint": - name = transaction_from_function(app.__class__) - source = SegmentNameSource.COMPONENT - elif integration.transaction_style == "url": - name, source = route_path, name_source - - return name, source diff --git a/sentry_sdk/integrations/starlite.py b/sentry_sdk/integrations/starlite.py index 18e69d0aaf..b6cf73b6be 100644 --- a/sentry_sdk/integrations/starlite.py +++ b/sentry_sdk/integrations/starlite.py @@ -76,7 +76,6 @@ def __init__( ) -> None: super().__init__( app=app, - transaction_style="endpoint", mechanism_type="asgi", span_origin=span_origin, asgi_version=3, diff --git a/tests/integrations/asgi/test_asgi.py b/tests/integrations/asgi/test_asgi.py index bb2c364807..799bafe2a8 100644 --- a/tests/integrations/asgi/test_asgi.py +++ b/tests/integrations/asgi/test_asgi.py @@ -183,16 +183,6 @@ async def app(scope, receive, send): return app -def test_invalid_transaction_style(asgi3_app): - with pytest.raises(ValueError) as exp: - SentryAsgiMiddleware(asgi3_app, transaction_style="URL") - - assert ( - str(exp.value) - == "Invalid value for transaction_style: URL (must be in ('endpoint', 'url'))" - ) - - @pytest.mark.asyncio @pytest.mark.parametrize( "should_send_pii", @@ -504,56 +494,35 @@ async def test_auto_session_tracking_with_aggregates( assert len(session_aggregates) == 1 -@pytest.mark.parametrize( - "url,transaction_style,expected_transaction,expected_source", - [ - ( - "/message", - "url", - "generic ASGI request", - "route", - ), - ( - "/message", - "endpoint", - "tests.integrations.asgi.test_asgi.asgi3_app..app", - "component", - ), - ], -) @pytest.mark.asyncio -async def test_transaction_style( +async def test_fallback_segment_name_and_source( sentry_init, asgi3_app, capture_items, - url, - transaction_style, - expected_transaction, - expected_source, ): sentry_init( send_default_pii=True, traces_sample_rate=1.0, ) - app = SentryAsgiMiddleware(asgi3_app, transaction_style=transaction_style) + app = SentryAsgiMiddleware(asgi3_app) scope = { "endpoint": asgi3_app, - "route": url, + "route": "/message", "client": ("127.0.0.1", 60457), } async with TestClient(app, scope=scope) as client: items = capture_items("span") - await client.get(url) + await client.get("/message") sentry_sdk.flush() assert len(items) == 1 span = items[0].payload - assert span["name"] == expected_transaction - assert span["attributes"]["sentry.segment.name.source"] == expected_source + assert span["name"] == "generic ASGI request" + assert span["attributes"]["sentry.segment.name.source"] == "route" def mock_asgi2_app(): @@ -1045,29 +1014,8 @@ async def test_get_request_attributes_client_address_user_info( @pytest.mark.asyncio -@pytest.mark.parametrize( - "request_url,transaction_style,expected_transaction_name,expected_transaction_source", - [ - ( - "/message/123456", - "endpoint", - "/message/123456", - "url", - ), - ( - "/message/123456", - "url", - "/message/123456", - "url", - ), - ], -) -async def test_transaction_name( +async def test_segment_name_and_source( sentry_init, - request_url, - transaction_style, - expected_transaction_name, - expected_transaction_source, asgi3_app, capture_items, ): @@ -1080,46 +1028,23 @@ async def test_transaction_name( items = capture_items("span") - app = SentryAsgiMiddleware(asgi3_app, transaction_style=transaction_style) + app = SentryAsgiMiddleware(asgi3_app) async with TestClient(app) as client: - await client.get(request_url) + await client.get("/message/123456") sentry_sdk.flush() assert len(items) == 1 span = items[0].payload - assert span["name"] == expected_transaction_name - assert ( - span["attributes"]["sentry.segment.name.source"] == expected_transaction_source - ) + assert span["name"] == "/message/123456" + assert span["attributes"]["sentry.segment.name.source"] == "url" @pytest.mark.asyncio -@pytest.mark.parametrize( - "request_url, transaction_style,expected_transaction_name,expected_transaction_source", - [ - ( - "/message/123456", - "endpoint", - "/message/123456", - "url", - ), - ( - "/message/123456", - "url", - "/message/123456", - "url", - ), - ], -) async def test_transaction_name_in_traces_sampler( sentry_init, - request_url, - transaction_style, - expected_transaction_name, - expected_transaction_source, asgi3_app, ): """ @@ -1128,23 +1053,18 @@ async def test_transaction_name_in_traces_sampler( """ def dummy_traces_sampler(sampling_context): - assert ( - sampling_context["transaction_context"]["name"] == expected_transaction_name - ) - assert ( - sampling_context["transaction_context"]["source"] - == expected_transaction_source - ) + assert sampling_context["transaction_context"]["name"] == "/message/123456" + assert sampling_context["transaction_context"]["source"] == "url" sentry_init( traces_sampler=dummy_traces_sampler, traces_sample_rate=1.0, ) - app = SentryAsgiMiddleware(asgi3_app, transaction_style=transaction_style) + app = SentryAsgiMiddleware(asgi3_app) async with TestClient(app) as client: - await client.get(request_url) + await client.get("/message/123456") @pytest.mark.asyncio diff --git a/tests/integrations/fastapi/test_fastapi.py b/tests/integrations/fastapi/test_fastapi.py index 0439326881..164f1941e8 100644 --- a/tests/integrations/fastapi/test_fastapi.py +++ b/tests/integrations/fastapi/test_fastapi.py @@ -363,46 +363,31 @@ async def test_response(sentry_init, capture_events): @pytest.mark.parametrize( - "url,transaction_style,expected_transaction,expected_source", + "url,expected_transaction,expected_source", [ ( "/message", - "url", "/message", "route", ), - ( - "/message", - "endpoint", - "tests.integrations.fastapi.test_fastapi.fastapi_app_factory.._message", - "component", - ), ( "/message/123456", - "url", "/message/{message_id}", "route", ), - ( - "/message/123456", - "endpoint", - "tests.integrations.fastapi.test_fastapi.fastapi_app_factory.._message_with_id", - "component", - ), ], ) -def test_transaction_style( +def test_segment_name_and_source( sentry_init, capture_events, url, - transaction_style, expected_transaction, expected_source, ): sentry_init( integrations=[ - StarletteIntegration(transaction_style=transaction_style), - FastApiIntegration(transaction_style=transaction_style), + StarletteIntegration(), + FastApiIntegration(), ], ) app = fastapi_app_factory() @@ -511,29 +496,8 @@ async def _error(request: Request): assert event["request"]["headers"]["proxy-authorization"] == "[Filtered]" -@pytest.mark.parametrize( - "request_url,transaction_style,expected_transaction_name,expected_transaction_source", - [ - ( - "/message/123456", - "endpoint", - "tests.integrations.fastapi.test_fastapi.fastapi_app_factory.._message_with_id", - "component", - ), - ( - "/message/123456", - "url", - "/message/{message_id}", - "route", - ), - ], -) def test_transaction_name( sentry_init, - request_url, - transaction_style, - expected_transaction_name, - expected_transaction_source, capture_items, ): """ @@ -542,8 +506,8 @@ def test_transaction_name( sentry_init( auto_enabling_integrations=False, # Make sure that httpx integration is not added, because it adds tracing information to the starlette test clients request. integrations=[ - StarletteIntegration(transaction_style=transaction_style), - FastApiIntegration(transaction_style=transaction_style), + StarletteIntegration(), + FastApiIntegration(), ], traces_sample_rate=1.0, ) @@ -553,17 +517,14 @@ def test_transaction_name( app = fastapi_app_factory() client = TestClient(app) - client.get(request_url) + client.get("/message/123456") sentry_sdk.flush() segments = [item.payload for item in items if item.payload.get("is_segment")] assert len(segments) == 1 segment = segments[0] - assert segment["name"] == expected_transaction_name - assert ( - segment["attributes"]["sentry.segment.name.source"] - == expected_transaction_source - ) + assert segment["name"] == "/message/{message_id}" + assert segment["attributes"]["sentry.segment.name.source"] == "route" def test_http_route_with_prefix( @@ -573,8 +534,8 @@ def test_http_route_with_prefix( sentry_init( auto_enabling_integrations=False, integrations=[ - StarletteIntegration(transaction_style="url"), - FastApiIntegration(transaction_style="url"), + StarletteIntegration(), + FastApiIntegration(), ], traces_sample_rate=1.0, ) @@ -623,29 +584,8 @@ def test_route_endpoint_equal_dependant_call(sentry_init): assert route.endpoint.__qualname__ == route.dependant.call.__qualname__ -@pytest.mark.parametrize( - "request_url,transaction_style,expected_transaction_name,expected_transaction_source", - [ - ( - "/message/123456", - "endpoint", - "http://testserver/message/123456", - "url", - ), - ( - "/message/123456", - "url", - "http://testserver/message/123456", - "url", - ), - ], -) def test_transaction_name_in_traces_sampler( sentry_init, - request_url, - transaction_style, - expected_transaction_name, - expected_transaction_source, ): """ Tests that a custom traces_sampler retrieves a meaningful transaction name. @@ -654,16 +594,14 @@ def test_transaction_name_in_traces_sampler( def dummy_traces_sampler(sampling_context): assert ( - sampling_context["transaction_context"]["name"] == expected_transaction_name - ) - assert ( - sampling_context["transaction_context"]["source"] - == expected_transaction_source + sampling_context["transaction_context"]["name"] + == "http://testserver/message/123456" ) + assert sampling_context["transaction_context"]["source"] == "url" sentry_init( auto_enabling_integrations=False, # Make sure that httpx integration is not added, because it adds tracing information to the starlette test clients request. - integrations=[StarletteIntegration(transaction_style=transaction_style)], + integrations=[StarletteIntegration()], traces_sampler=dummy_traces_sampler, traces_sample_rate=1.0, ) @@ -671,34 +609,13 @@ def dummy_traces_sampler(sampling_context): app = fastapi_app_factory() client = TestClient(app) - client.get(request_url) + client.get("/message/123456") @pytest.mark.parametrize("middleware_spans", [False, True]) -@pytest.mark.parametrize( - "request_url,transaction_style,expected_transaction_name,expected_transaction_source", - [ - ( - "/message/123456", - "endpoint", - "starlette.middleware.trustedhost.TrustedHostMiddleware", - "component", - ), - ( - "/message/123456", - "url", - "http://testserver/message/123456", - "url", - ), - ], -) def test_transaction_name_in_middleware( sentry_init, middleware_spans, - request_url, - transaction_style, - expected_transaction_name, - expected_transaction_source, capture_items, ): """ @@ -707,12 +624,8 @@ def test_transaction_name_in_middleware( sentry_init( auto_enabling_integrations=False, # Make sure that httpx integration is not added, because it adds tracing information to the starlette test clients request. integrations=[ - StarletteIntegration( - transaction_style=transaction_style, middleware_spans=middleware_spans - ), - FastApiIntegration( - transaction_style=transaction_style, middleware_spans=middleware_spans - ), + StarletteIntegration(middleware_spans=middleware_spans), + FastApiIntegration(middleware_spans=middleware_spans), ], traces_sample_rate=1.0, ) @@ -729,17 +642,14 @@ def test_transaction_name_in_middleware( ) client = TestClient(app) - client.get(request_url) + client.get("/message/123456") sentry_sdk.flush() segments = [item.payload for item in items if item.payload.get("is_segment")] assert len(segments) == 1 segment = segments[0] - assert segment["name"] == expected_transaction_name - assert ( - segment["attributes"]["sentry.segment.name.source"] - == expected_transaction_source - ) + assert segment["name"] == "http://testserver/message/123456" + assert segment["attributes"]["sentry.segment.name.source"] == "url" @pytest.mark.skipif( @@ -876,13 +786,12 @@ async def _error(): assert len(events) == int(expected_error) -@pytest.mark.parametrize("transaction_style", ["endpoint", "url"]) -def test_app_host(sentry_init, capture_items, transaction_style): +def test_app_host(sentry_init, capture_items): sentry_init( traces_sample_rate=1.0, integrations=[ - StarletteIntegration(transaction_style=transaction_style), - FastApiIntegration(transaction_style=transaction_style), + StarletteIntegration(), + FastApiIntegration(), ], ) @@ -905,10 +814,7 @@ async def subapp_route(): assert len(segments) == 1 segment = segments[0] - if transaction_style == "url": - assert segment["name"] == "/subapp" - else: - assert segment["name"].endswith("subapp_route") + assert segment["name"] == "/subapp" @pytest.mark.asyncio diff --git a/tests/integrations/starlette/test_starlette.py b/tests/integrations/starlette/test_starlette.py index a84ae6c986..20cbe44d81 100644 --- a/tests/integrations/starlette/test_starlette.py +++ b/tests/integrations/starlette/test_starlette.py @@ -791,44 +791,29 @@ def test_user_info_data_collection( @pytest.mark.parametrize( - "url,transaction_style,expected_transaction,expected_source", + "url,expected_transaction,expected_source", [ ( "/message", - "url", "/message", "route", ), - ( - "/message", - "endpoint", - "tests.integrations.starlette.test_starlette.starlette_app_factory.._message", - "component", - ), ( "/message/123456", - "url", "/message/{message_id}", "route", ), - ( - "/message/123456", - "endpoint", - "tests.integrations.starlette.test_starlette.starlette_app_factory.._message_with_id", - "component", - ), ], ) -def test_transaction_style( +def test_segment_name_and_source( sentry_init, capture_events, url, - transaction_style, expected_transaction, expected_source, ): sentry_init( - integrations=[StarletteIntegration(transaction_style=transaction_style)], + integrations=[StarletteIntegration()], ) starlette_app = starlette_app_factory() @@ -844,7 +829,7 @@ def test_transaction_style( def test_hosted_route_transaction_info(sentry_init, capture_items): sentry_init( - integrations=[StarletteIntegration(transaction_style="url")], + integrations=[StarletteIntegration()], traces_sample_rate=1.0, ) @@ -1341,7 +1326,7 @@ def test_active_thread_id(sentry_init, capture_items, endpoint): def test_http_route(sentry_init, capture_items, endpoint): sentry_init( auto_enabling_integrations=False, - integrations=[StarletteIntegration(transaction_style="url")], + integrations=[StarletteIntegration()], traces_sample_rate=1.0, ) app = starlette_app_factory() @@ -1367,7 +1352,7 @@ def test_segment_name_is_route_resolved_name_static( ): sentry_init( auto_enabling_integrations=False, - integrations=[StarletteIntegration(transaction_style="url")], + integrations=[StarletteIntegration()], traces_sample_rate=1.0, ) items = capture_items("span") @@ -1454,29 +1439,8 @@ def test_template_tracing_meta(sentry_init, capture_events): assert rendered_baggage == baggage -@pytest.mark.parametrize( - "request_url,transaction_style,expected_transaction_name,expected_transaction_source", - [ - ( - "/message/123456", - "endpoint", - "tests.integrations.starlette.test_starlette.starlette_app_factory.._message_with_id", - "component", - ), - ( - "/message/123456", - "url", - "/message/{message_id}", - "route", - ), - ], -) def test_transaction_name( sentry_init, - request_url, - transaction_style, - expected_transaction_name, - expected_transaction_source, capture_items, ): """ @@ -1484,7 +1448,7 @@ def test_transaction_name( """ sentry_init( auto_enabling_integrations=False, # Make sure that httpx integration is not added, because it adds tracing information to the starlette test clients request. - integrations=[StarletteIntegration(transaction_style=transaction_style)], + integrations=[StarletteIntegration()], traces_sample_rate=1.0, ) @@ -1492,42 +1456,18 @@ def test_transaction_name( app = starlette_app_factory() client = TestClient(app) - client.get(request_url) + client.get("/message/123456") sentry_sdk.flush() segments = [item.payload for item in items if item.payload.get("is_segment")] assert len(segments) == 1 - assert segments[0]["name"] == expected_transaction_name - assert ( - segments[0]["attributes"]["sentry.segment.name.source"] - == expected_transaction_source - ) + assert segments[0]["name"] == "/message/{message_id}" + assert segments[0]["attributes"]["sentry.segment.name.source"] == "route" -@pytest.mark.parametrize( - "request_url,transaction_style,expected_transaction_name,expected_transaction_source", - [ - ( - "/message/123456", - "endpoint", - "http://testserver/message/123456", - "url", - ), - ( - "/message/123456", - "url", - "http://testserver/message/123456", - "url", - ), - ], -) def test_transaction_name_in_traces_sampler( sentry_init, - request_url, - transaction_style, - expected_transaction_name, - expected_transaction_source, ): """ Tests that a custom traces_sampler has a meaningful transaction name. @@ -1536,50 +1476,27 @@ def test_transaction_name_in_traces_sampler( def dummy_traces_sampler(sampling_context): assert ( - sampling_context["transaction_context"]["name"] == expected_transaction_name - ) - assert ( - sampling_context["transaction_context"]["source"] - == expected_transaction_source + sampling_context["transaction_context"]["name"] + == "http://testserver/message/123456" ) + assert sampling_context["transaction_context"]["source"] == "url" sentry_init( auto_enabling_integrations=False, # Make sure that httpx integration is not added, because it adds tracing information to the starlette test clients request. - integrations=[StarletteIntegration(transaction_style=transaction_style)], + integrations=[StarletteIntegration()], traces_sampler=dummy_traces_sampler, traces_sample_rate=1.0, ) app = starlette_app_factory() client = TestClient(app) - client.get(request_url) + client.get("/message/123456") @pytest.mark.parametrize("middleware_spans", [False, True]) -@pytest.mark.parametrize( - "request_url,transaction_style,expected_transaction_name,expected_transaction_source", - [ - ( - "/message/123456", - "endpoint", - "starlette.middleware.trustedhost.TrustedHostMiddleware", - "component", - ), - ( - "/message/123456", - "url", - "http://testserver/message/123456", - "url", - ), - ], -) def test_transaction_name_in_middleware( sentry_init, middleware_spans, - request_url, - transaction_style, - expected_transaction_name, - expected_transaction_source, capture_items, ): """ @@ -1588,9 +1505,7 @@ def test_transaction_name_in_middleware( sentry_init( auto_enabling_integrations=False, # Make sure that httpx integration is not added, because it adds tracing information to the starlette test clients request. integrations=[ - StarletteIntegration( - transaction_style=transaction_style, middleware_spans=middleware_spans - ), + StarletteIntegration(middleware_spans=middleware_spans), ], traces_sample_rate=1.0, ) @@ -1606,17 +1521,14 @@ def test_transaction_name_in_middleware( app = starlette_app_factory(middleware=middleware) client = TestClient(app) - client.get(request_url) + client.get("/message/123456") sentry_sdk.flush() segments = [item.payload for item in items if item.payload.get("is_segment")] assert len(segments) == 1 - assert segments[0]["name"] == expected_transaction_name - assert ( - segments[0]["attributes"]["sentry.segment.name.source"] - == expected_transaction_source - ) + assert segments[0]["name"] == "http://testserver/message/123456" + assert segments[0]["attributes"]["sentry.segment.name.source"] == "url" def test_span_origin(sentry_init, capture_items):