From dda47cdbe3bd08a4f63a4a5abbb624a833a5534e Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Tue, 15 Sep 2026 16:06:01 +0200 Subject: [PATCH] chore: Use SegmentSourceName --- sentry_sdk/integrations/aiohttp.py | 8 ++--- sentry_sdk/integrations/asgi.py | 22 +++++-------- sentry_sdk/integrations/boto3.py | 3 +- sentry_sdk/integrations/celery/__init__.py | 3 +- sentry_sdk/integrations/django/__init__.py | 4 +-- sentry_sdk/integrations/dramatiq.py | 4 +-- sentry_sdk/integrations/fastapi.py | 10 ++++-- sentry_sdk/integrations/flask.py | 2 +- sentry_sdk/integrations/huey.py | 6 ++-- sentry_sdk/integrations/litestar.py | 4 +-- .../openai_agents/patches/models.py | 3 +- sentry_sdk/integrations/otlp.py | 2 +- sentry_sdk/integrations/sanic.py | 3 +- sentry_sdk/integrations/starlette.py | 30 ++++++++--------- sentry_sdk/integrations/starlite.py | 4 +-- sentry_sdk/integrations/stdlib.py | 3 +- sentry_sdk/scope.py | 6 ++-- sentry_sdk/tracing.py | 33 ------------------- sentry_sdk/tracing_utils.py | 6 ++-- tests/integrations/asgi/test_asgi.py | 4 +-- tests/integrations/sanic/test_sanic.py | 10 +++--- 21 files changed, 61 insertions(+), 109 deletions(-) delete mode 100644 sentry_sdk/tracing.py diff --git a/sentry_sdk/integrations/aiohttp.py b/sentry_sdk/integrations/aiohttp.py index 84551db868..32441f4e04 100644 --- a/sentry_sdk/integrations/aiohttp.py +++ b/sentry_sdk/integrations/aiohttp.py @@ -21,14 +21,12 @@ from sentry_sdk.scope import Scope, should_send_default_pii from sentry_sdk.sessions import track_session from sentry_sdk.traces import ( - SegmentNameSource, - Span, - SpanStatus, -) -from sentry_sdk.tracing import ( BAGGAGE_HEADER_NAME, SENTRY_TRACE_HEADER_NAME, SOURCE_FOR_STYLE, + SegmentNameSource, + Span, + SpanStatus, ) from sentry_sdk.tracing_utils import ( add_http_breadcrumb, diff --git a/sentry_sdk/integrations/asgi.py b/sentry_sdk/integrations/asgi.py index c9c86a4c03..9bcecc975c 100644 --- a/sentry_sdk/integrations/asgi.py +++ b/sentry_sdk/integrations/asgi.py @@ -29,16 +29,10 @@ 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 as SEGMENT_SOURCE_FOR_STYLE, -) -from sentry_sdk.traces import ( + SOURCE_FOR_STYLE, SegmentNameSource, Span, ) -from sentry_sdk.tracing import ( - SOURCE_FOR_STYLE, - TransactionSource, -) from sentry_sdk.utils import ( _get_installed_modules, capture_internal_exceptions, @@ -354,9 +348,9 @@ def event_processor( and transaction != _DEFAULT_TRANSACTION_NAME and transaction_source in [ - TransactionSource.COMPONENT, - TransactionSource.ROUTE, - TransactionSource.CUSTOM, + SegmentNameSource.COMPONENT, + SegmentNameSource.ROUTE, + SegmentNameSource.CUSTOM, ] ) if not already_set: @@ -397,7 +391,7 @@ def _get_transaction_name_and_source( asgi_scope=asgi_scope, root_path_in_path=self.root_path_in_path ), ) - source = TransactionSource.URL + source = SegmentNameSource.URL elif transaction_style == "url": # FastAPI includes the route object in the scope to let Sentry extract the @@ -416,11 +410,11 @@ def _get_transaction_name_and_source( asgi_scope=asgi_scope, root_path_in_path=self.root_path_in_path ), ) - source = TransactionSource.URL + source = SegmentNameSource.URL if name is None: name = _DEFAULT_TRANSACTION_NAME - source = TransactionSource.ROUTE + source = SegmentNameSource.ROUTE return name, source return name, source @@ -429,7 +423,7 @@ def _get_segment_name_and_source( self: "SentryAsgiMiddleware", segment_style: str, asgi_scope: "Any" ) -> "Tuple[str, str]": name = None - source = SEGMENT_SOURCE_FOR_STYLE[segment_style].value + source = SOURCE_FOR_STYLE[segment_style].value ty = asgi_scope.get("type") if segment_style == "endpoint": diff --git a/sentry_sdk/integrations/boto3.py b/sentry_sdk/integrations/boto3.py index 4ffe245f09..b8f424c281 100644 --- a/sentry_sdk/integrations/boto3.py +++ b/sentry_sdk/integrations/boto3.py @@ -4,8 +4,7 @@ import sentry_sdk from sentry_sdk.consts import OP, SPANDATA from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version -from sentry_sdk.traces import Span -from sentry_sdk.tracing import BAGGAGE_HEADER_NAME +from sentry_sdk.traces import BAGGAGE_HEADER_NAME, Span from sentry_sdk.tracing_utils import ( add_http_breadcrumb, add_sentry_baggage_to_headers, diff --git a/sentry_sdk/integrations/celery/__init__.py b/sentry_sdk/integrations/celery/__init__.py index 4ae0772b16..3032ac04f0 100644 --- a/sentry_sdk/integrations/celery/__init__.py +++ b/sentry_sdk/integrations/celery/__init__.py @@ -16,8 +16,7 @@ from sentry_sdk.integrations.celery.utils import _now_seconds_since_epoch from sentry_sdk.integrations.logging import ignore_logger_for_events from sentry_sdk.scope import Scope, should_send_default_pii -from sentry_sdk.traces import SegmentNameSource, Span -from sentry_sdk.tracing import BAGGAGE_HEADER_NAME +from sentry_sdk.traces import BAGGAGE_HEADER_NAME, SegmentNameSource, Span from sentry_sdk.tracing_utils import Baggage from sentry_sdk.utils import ( SENSITIVE_DATA_SUBSTITUTE, diff --git a/sentry_sdk/integrations/django/__init__.py b/sentry_sdk/integrations/django/__init__.py index 5602f91b5d..8c3540e509 100644 --- a/sentry_sdk/integrations/django/__init__.py +++ b/sentry_sdk/integrations/django/__init__.py @@ -21,7 +21,7 @@ from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware from sentry_sdk.scope import add_global_event_processor, should_send_default_pii from sentry_sdk.serializer import add_global_repr_processor, add_repr_sequence_type -from sentry_sdk.tracing import SOURCE_FOR_STYLE, TransactionSource +from sentry_sdk.traces import SOURCE_FOR_STYLE, SegmentNameSource from sentry_sdk.tracing_utils import ( add_query_source, record_sql_queries, @@ -405,7 +405,7 @@ def _set_transaction_name_and_source( if transaction_name is None: transaction_name = request.path_info - source = TransactionSource.URL + source = SegmentNameSource.URL else: source = SOURCE_FOR_STYLE[transaction_style] diff --git a/sentry_sdk/integrations/dramatiq.py b/sentry_sdk/integrations/dramatiq.py index 06675d50dd..be20fd2871 100644 --- a/sentry_sdk/integrations/dramatiq.py +++ b/sentry_sdk/integrations/dramatiq.py @@ -6,10 +6,10 @@ from sentry_sdk.consts import OP, SPANDATA from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version from sentry_sdk.integrations._wsgi_common import request_body_within_bounds -from sentry_sdk.traces import SegmentNameSource -from sentry_sdk.tracing import ( +from sentry_sdk.traces import ( BAGGAGE_HEADER_NAME, SENTRY_TRACE_HEADER_NAME, + SegmentNameSource, ) from sentry_sdk.utils import ( AnnotatedValue, diff --git a/sentry_sdk/integrations/fastapi.py b/sentry_sdk/integrations/fastapi.py index c1a0aab3f4..cdae8b8ac3 100644 --- a/sentry_sdk/integrations/fastapi.py +++ b/sentry_sdk/integrations/fastapi.py @@ -6,8 +6,12 @@ import sentry_sdk from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations import DidNotEnable, _check_minimum_version -from sentry_sdk.traces import Span, get_current_span -from sentry_sdk.tracing import SOURCE_FOR_STYLE, TransactionSource +from sentry_sdk.traces import ( + SOURCE_FOR_STYLE, + SegmentNameSource, + Span, + get_current_span, +) from sentry_sdk.utils import ( has_data_collection_enabled, parse_version, @@ -72,7 +76,7 @@ def _set_transaction_name_and_source( if not name: name = _DEFAULT_TRANSACTION_NAME - source = TransactionSource.ROUTE + source = SegmentNameSource.ROUTE else: source = SOURCE_FOR_STYLE[transaction_style] diff --git a/sentry_sdk/integrations/flask.py b/sentry_sdk/integrations/flask.py index f17cd0f197..bff656bbb8 100644 --- a/sentry_sdk/integrations/flask.py +++ b/sentry_sdk/integrations/flask.py @@ -9,7 +9,7 @@ ) from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware from sentry_sdk.scope import should_send_default_pii -from sentry_sdk.tracing import SOURCE_FOR_STYLE +from sentry_sdk.traces import SOURCE_FOR_STYLE from sentry_sdk.utils import ( capture_internal_exceptions, ensure_integration_enabled, diff --git a/sentry_sdk/integrations/huey.py b/sentry_sdk/integrations/huey.py index f158bfceec..ae3b3adce1 100644 --- a/sentry_sdk/integrations/huey.py +++ b/sentry_sdk/integrations/huey.py @@ -7,10 +7,12 @@ from sentry_sdk.consts import OP, SPANDATA from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version from sentry_sdk.scope import should_send_default_pii -from sentry_sdk.traces import SegmentNameSource, Span, SpanStatus -from sentry_sdk.tracing import ( +from sentry_sdk.traces import ( BAGGAGE_HEADER_NAME, SENTRY_TRACE_HEADER_NAME, + SegmentNameSource, + Span, + SpanStatus, ) from sentry_sdk.utils import ( SENSITIVE_DATA_SUBSTITUTE, diff --git a/sentry_sdk/integrations/litestar.py b/sentry_sdk/integrations/litestar.py index 80ee01b9ae..95df2c7e1d 100644 --- a/sentry_sdk/integrations/litestar.py +++ b/sentry_sdk/integrations/litestar.py @@ -13,7 +13,7 @@ from sentry_sdk.integrations.asgi import SentryAsgiMiddleware from sentry_sdk.integrations.logging import ignore_logger_for_events from sentry_sdk.scope import should_send_default_pii -from sentry_sdk.tracing import SOURCE_FOR_STYLE, TransactionSource +from sentry_sdk.traces import SOURCE_FOR_STYLE, SegmentNameSource from sentry_sdk.utils import ( ensure_integration_enabled, event_from_exception, @@ -272,7 +272,7 @@ async def handle_wrapper( if not name: name = _DEFAULT_TRANSACTION_NAME - source = TransactionSource.ROUTE + source = SegmentNameSource.ROUTE sentry_sdk.set_transaction_name(name, source) sentry_scope.set_transaction_name(name, source) diff --git a/sentry_sdk/integrations/openai_agents/patches/models.py b/sentry_sdk/integrations/openai_agents/patches/models.py index 116306de60..9514854105 100644 --- a/sentry_sdk/integrations/openai_agents/patches/models.py +++ b/sentry_sdk/integrations/openai_agents/patches/models.py @@ -6,8 +6,7 @@ import sentry_sdk from sentry_sdk.consts import SPANDATA from sentry_sdk.integrations import DidNotEnable -from sentry_sdk.traces import Span -from sentry_sdk.tracing import BAGGAGE_HEADER_NAME +from sentry_sdk.traces import BAGGAGE_HEADER_NAME, Span from sentry_sdk.tracing_utils import ( add_sentry_baggage_to_headers, should_propagate_trace, diff --git a/sentry_sdk/integrations/otlp.py b/sentry_sdk/integrations/otlp.py index df1f2be257..ed1658d156 100644 --- a/sentry_sdk/integrations/otlp.py +++ b/sentry_sdk/integrations/otlp.py @@ -2,7 +2,7 @@ from sentry_sdk.consts import VERSION, EndpointType from sentry_sdk.integrations import DidNotEnable, Integration from sentry_sdk.scope import register_external_propagation_context -from sentry_sdk.tracing import ( +from sentry_sdk.traces import ( BAGGAGE_HEADER_NAME, SENTRY_TRACE_HEADER_NAME, ) diff --git a/sentry_sdk/integrations/sanic.py b/sentry_sdk/integrations/sanic.py index e6aafdaca5..e2ea3a230b 100644 --- a/sentry_sdk/integrations/sanic.py +++ b/sentry_sdk/integrations/sanic.py @@ -13,7 +13,6 @@ from sentry_sdk.integrations._wsgi_common import RequestExtractor, _filter_headers from sentry_sdk.scope import should_send_default_pii from sentry_sdk.traces import SegmentNameSource -from sentry_sdk.tracing import TransactionSource from sentry_sdk.utils import ( capture_internal_exceptions, ensure_integration_enabled, @@ -189,7 +188,7 @@ async def _set_transaction(request: "Request", route: "Route", **_: "Any") -> No with capture_internal_exceptions(): scope = sentry_sdk.get_current_scope() route_name = route.name.replace(request.app.name, "").strip(".") - scope.set_transaction_name(route_name, source=TransactionSource.COMPONENT) + scope.set_transaction_name(route_name, source=SegmentNameSource.COMPONENT) def _sentry_error_handler_lookup( diff --git a/sentry_sdk/integrations/starlette.py b/sentry_sdk/integrations/starlette.py index 9234a22d1f..dd2e63033e 100644 --- a/sentry_sdk/integrations/starlette.py +++ b/sentry_sdk/integrations/starlette.py @@ -25,11 +25,7 @@ ) from sentry_sdk.integrations.asgi import SentryAsgiMiddleware from sentry_sdk.scope import should_send_default_pii -from sentry_sdk.traces import Span -from sentry_sdk.tracing import ( - SOURCE_FOR_STYLE, - TransactionSource, -) +from sentry_sdk.traces import SOURCE_FOR_STYLE, SegmentNameSource, Span from sentry_sdk.utils import ( AnnotatedValue, capture_internal_exceptions, @@ -162,7 +158,7 @@ async def _create_span_call( if ( server_span is not None and route_path is not None - and name_source == TransactionSource.ROUTE + and name_source == SegmentNameSource.ROUTE ): server_span.set_attribute(SPANDATA.HTTP_ROUTE, route_path) @@ -516,7 +512,7 @@ async def _wrap_async_handler( if ( server_span is not None and route_path is not None - and name_source == TransactionSource.ROUTE + and name_source == SegmentNameSource.ROUTE ): server_span.set_attribute(SPANDATA.HTTP_ROUTE, route_path) @@ -628,7 +624,7 @@ def _sentry_sync_func(*args: "Any", **kwargs: "Any") -> "Any": if ( server_span is not None and route_path is not None - and name_source == TransactionSource.ROUTE + and name_source == SegmentNameSource.ROUTE ): server_span.set_attribute(SPANDATA.HTTP_ROUTE, route_path) @@ -832,22 +828,22 @@ async def json(self: "StarletteRequestExtractor") -> "Optional[Dict[str, Any]]": def _http_route_and_source_from_router( scope: "StarletteScope", -) -> "Tuple[Optional[str], TransactionSource]": +) -> "Tuple[Optional[str], SegmentNameSource]": router = scope.get("router") if not router: - return None, TransactionSource.ROUTE + return None, SegmentNameSource.ROUTE for route in router.routes: match = route.matches(scope) if match[0] == Match.FULL: try: - return route.path, TransactionSource.ROUTE + return route.path, SegmentNameSource.ROUTE except AttributeError: # Host routes have no path template, so fall back to the # concrete request path and classify it as a URL. - return scope.get("path"), TransactionSource.URL + return scope.get("path"), SegmentNameSource.URL - return None, TransactionSource.ROUTE + return None, SegmentNameSource.ROUTE def _set_transaction_name_and_source( @@ -855,7 +851,7 @@ def _set_transaction_name_and_source( transaction_style: str, endpoint: "Optional[Callable[..., Any]]", route_path: "Optional[str]", - name_source: "TransactionSource", + name_source: "SegmentNameSource", ) -> None: name = None source = SOURCE_FOR_STYLE[transaction_style] @@ -868,7 +864,7 @@ def _set_transaction_name_and_source( if name is None: name = _DEFAULT_TRANSACTION_NAME - source = TransactionSource.ROUTE + source = SegmentNameSource.ROUTE scope.set_transaction_name(name, source=source) @@ -877,14 +873,14 @@ def _get_transaction_from_middleware( app: "Any", integration: "StarletteIntegration", route_path: "Optional[str]", - name_source: "TransactionSource", + name_source: "SegmentNameSource", ) -> "Tuple[Optional[str], Optional[str]]": name = None source = None if integration.transaction_style == "endpoint": name = transaction_from_function(app.__class__) - source = TransactionSource.COMPONENT + source = SegmentNameSource.COMPONENT elif integration.transaction_style == "url": name, source = route_path, name_source diff --git a/sentry_sdk/integrations/starlite.py b/sentry_sdk/integrations/starlite.py index 0f71f4ef15..58a4f520ee 100644 --- a/sentry_sdk/integrations/starlite.py +++ b/sentry_sdk/integrations/starlite.py @@ -7,7 +7,7 @@ from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version from sentry_sdk.integrations.asgi import SentryAsgiMiddleware from sentry_sdk.scope import should_send_default_pii -from sentry_sdk.tracing import SOURCE_FOR_STYLE, TransactionSource +from sentry_sdk.traces import SOURCE_FOR_STYLE, SegmentNameSource from sentry_sdk.utils import ( ensure_integration_enabled, event_from_exception, @@ -247,7 +247,7 @@ async def handle_wrapper( if not name: name = _DEFAULT_TRANSACTION_NAME - source = TransactionSource.ROUTE + source = SegmentNameSource.ROUTE sentry_sdk.set_transaction_name(name, source) sentry_scope.set_transaction_name(name, source) diff --git a/sentry_sdk/integrations/stdlib.py b/sentry_sdk/integrations/stdlib.py index 2dc174b617..b3efef18c7 100644 --- a/sentry_sdk/integrations/stdlib.py +++ b/sentry_sdk/integrations/stdlib.py @@ -9,8 +9,7 @@ from sentry_sdk.consts import _SENTRY_HEADER_NAMES, OP, SPANDATA from sentry_sdk.integrations import Integration from sentry_sdk.scope import add_global_event_processor -from sentry_sdk.traces import Span -from sentry_sdk.tracing import SENTRY_TRACE_HEADER_NAME +from sentry_sdk.traces import SENTRY_TRACE_HEADER_NAME, Span from sentry_sdk.tracing_utils import ( EnvironHeaders, add_http_breadcrumb, diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index 843a2ec07f..621f21cd7b 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -23,12 +23,10 @@ from sentry_sdk.session import Session from sentry_sdk.traces import ( _DEFAULT_PARENT_SPAN, - NoOpSpan, - Span, -) -from sentry_sdk.tracing import ( BAGGAGE_HEADER_NAME, SENTRY_TRACE_HEADER_NAME, + NoOpSpan, + Span, ) from sentry_sdk.tracing_utils import ( Baggage, diff --git a/sentry_sdk/tracing.py b/sentry_sdk/tracing.py deleted file mode 100644 index 619bb6d000..0000000000 --- a/sentry_sdk/tracing.py +++ /dev/null @@ -1,33 +0,0 @@ -from enum import Enum - -BAGGAGE_HEADER_NAME = "baggage" -SENTRY_TRACE_HEADER_NAME = "sentry-trace" - - -# Transaction source -# see https://develop.sentry.dev/sdk/event-payloads/transaction/#transaction-annotations -class TransactionSource(str, Enum): - COMPONENT = "component" - CUSTOM = "custom" - ROUTE = "route" - TASK = "task" - URL = "url" - VIEW = "view" - - def __str__(self) -> str: - return self.value - - -SOURCE_FOR_STYLE = { - "endpoint": TransactionSource.COMPONENT, - "function_name": TransactionSource.COMPONENT, - "handler_name": TransactionSource.COMPONENT, - "method_and_path_pattern": TransactionSource.ROUTE, - "path": TransactionSource.URL, - "route_name": TransactionSource.COMPONENT, - "route_pattern": TransactionSource.ROUTE, - "uri_template": TransactionSource.ROUTE, - "url": TransactionSource.ROUTE, -} - -# Circular imports diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py index 3c16c3cb14..7a81240dcc 100644 --- a/sentry_sdk/tracing_utils.py +++ b/sentry_sdk/tracing_utils.py @@ -1296,10 +1296,8 @@ def _matches(rule: "Any", value: "Any") -> bool: # Circular imports from sentry_sdk.traces import ( - LOW_QUALITY_SEGMENT_SOURCES, - Span, -) -from sentry_sdk.tracing import ( BAGGAGE_HEADER_NAME, + LOW_QUALITY_SEGMENT_SOURCES, SENTRY_TRACE_HEADER_NAME, + Span, ) diff --git a/tests/integrations/asgi/test_asgi.py b/tests/integrations/asgi/test_asgi.py index bef2cf8823..3f08f5c289 100644 --- a/tests/integrations/asgi/test_asgi.py +++ b/tests/integrations/asgi/test_asgi.py @@ -10,7 +10,7 @@ _get_ip, ) from sentry_sdk.integrations.asgi import SentryAsgiMiddleware, _looks_like_asgi3 -from sentry_sdk.tracing import TransactionSource +from sentry_sdk.traces import SegmentNameSource @pytest.fixture @@ -134,7 +134,7 @@ async def app(scope, receive, send): def asgi3_custom_transaction_app(): async def app(scope, receive, send): sentry_sdk.get_current_scope().set_transaction_name( - "foobar", source=TransactionSource.CUSTOM + "foobar", source=SegmentNameSource.CUSTOM ) await send( { diff --git a/tests/integrations/sanic/test_sanic.py b/tests/integrations/sanic/test_sanic.py index 7a3ef06fa6..db6c46a648 100644 --- a/tests/integrations/sanic/test_sanic.py +++ b/tests/integrations/sanic/test_sanic.py @@ -14,7 +14,7 @@ import sentry_sdk from sentry_sdk import capture_message from sentry_sdk.integrations.sanic import SanicIntegration -from sentry_sdk.tracing import TransactionSource +from sentry_sdk.traces import SegmentNameSource from tests.conftest import get_free_port from tests.integrations.utils import ( DATA_COLLECTION_REMOTE_ADDR_CASES, @@ -377,28 +377,28 @@ def __init__( url="/message", expected_status=200, expected_transaction_name="hi", - expected_source=TransactionSource.COMPONENT, + expected_source=SegmentNameSource.COMPONENT, ), TransactionTestConfig( # Transaction for successful page load with query string url="/message?foo=bar", expected_status=200, expected_transaction_name="hi", - expected_source=TransactionSource.COMPONENT, + expected_source=SegmentNameSource.COMPONENT, ), TransactionTestConfig( # Transaction still recorded when we have an internal server error url="/500", expected_status=500, expected_transaction_name="fivehundred", - expected_source=TransactionSource.COMPONENT, + expected_source=SegmentNameSource.COMPONENT, ), TransactionTestConfig( # We should get transactions for 404 errors url="/404", expected_status=404, expected_transaction_name="/404", - expected_source=TransactionSource.URL, + expected_source=SegmentNameSource.URL, ), ], )