diff --git a/sentry_sdk/integrations/asgi.py b/sentry_sdk/integrations/asgi.py index 61bc20fd40..f67c47ef02 100644 --- a/sentry_sdk/integrations/asgi.py +++ b/sentry_sdk/integrations/asgi.py @@ -176,6 +176,18 @@ async def _run_app(self, scope, receive, send, asgi_version): _asgi_middleware_applied.set(True) try: with sentry_sdk.isolation_scope() as sentry_scope: + ( + transaction_name, + transaction_source, + ) = self._get_transaction_name_and_source( + self.transaction_style, + scope, + ) + sentry_scope.set_transaction_name( + transaction_name, + source=transaction_source, + ) + with track_session(sentry_scope, session_mode="request"): sentry_scope.clear_breadcrumbs() sentry_scope._name = "asgi" @@ -183,19 +195,12 @@ async def _run_app(self, scope, receive, send, asgi_version): sentry_scope.add_event_processor(processor) ty = scope["type"] - ( - transaction_name, - transaction_source, - ) = self._get_transaction_name_and_source( - self.transaction_style, - scope, - ) method = scope.get("method", "").upper() should_trace = method in self.http_methods_to_capture with sentry_sdk.continue_trace(_get_headers(scope)): with ( - sentry_sdk.start_transaction( + sentry_sdk.start_span( op=( OP.WEBSOCKET_SERVER if ty == "websocket" @@ -251,13 +256,18 @@ def event_processor(self, event, hint, asgi_scope): event["request"] = deepcopy(request_data) # Only set transaction name if not already set by Starlette or FastAPI (or other frameworks) - already_set = event["transaction"] != _DEFAULT_TRANSACTION_NAME and event[ - "transaction_info" - ].get("source") in [ - TRANSACTION_SOURCE_COMPONENT, - TRANSACTION_SOURCE_ROUTE, - TRANSACTION_SOURCE_CUSTOM, - ] + already_set = ( + "transaction" in event + and event["transaction"] != _DEFAULT_TRANSACTION_NAME + and "transaction_info" in event + and "source" in event["transaction_info"] + and event["transaction_info"]["source"] + in [ + TRANSACTION_SOURCE_COMPONENT, + TRANSACTION_SOURCE_ROUTE, + TRANSACTION_SOURCE_CUSTOM, + ] + ) if not already_set: name, source = self._get_transaction_name_and_source( self.transaction_style, asgi_scope diff --git a/sentry_sdk/integrations/wsgi.py b/sentry_sdk/integrations/wsgi.py index 72ecfdb758..3aebff17d5 100644 --- a/sentry_sdk/integrations/wsgi.py +++ b/sentry_sdk/integrations/wsgi.py @@ -98,7 +98,9 @@ def __call__(self, environ, start_response): _wsgi_middleware_applied.set(True) try: with sentry_sdk.isolation_scope() as scope: - scope.set_transaction_name(DEFAULT_TRANSACTION_NAME, source=TRANSACTION_SOURCE_ROUTE) + scope.set_transaction_name( + DEFAULT_TRANSACTION_NAME, source=TRANSACTION_SOURCE_ROUTE + ) with track_session(scope, session_mode="request"): with capture_internal_exceptions(): diff --git a/sentry_sdk/tracing.py b/sentry_sdk/tracing.py index 31312c9ad3..c2bd5734c5 100644 --- a/sentry_sdk/tracing.py +++ b/sentry_sdk/tracing.py @@ -683,9 +683,9 @@ def get_trace_context(self): rv["status"] = self.status if self.containing_transaction: - rv["dynamic_sampling_context"] = ( - self.containing_transaction.get_baggage().dynamic_sampling_context() - ) + rv[ + "dynamic_sampling_context" + ] = self.containing_transaction.get_baggage().dynamic_sampling_context() data = {} @@ -1203,6 +1203,7 @@ def __init__( start_timestamp=None, # type: Optional[Union[datetime, float]] origin=None, # type: Optional[str] name=None, # type: Optional[str] + source=TRANSACTION_SOURCE_CUSTOM, # type: str otel_span=None, # type: Optional[OtelSpan] **_, # type: dict[str, object] ): @@ -1232,6 +1233,7 @@ def __init__( self.op = op self.description = description self.name = span_name + self.source = source if status is not None: self.set_status(status)