From 675690a6cff078294abbfb664c56ae25a66ece9e Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Tue, 5 Nov 2024 14:41:55 +0100 Subject: [PATCH] Fix WSGI tests * Don't default to OK span status for UNSET, this breaks older behavior * Set transaction name manually on scope after isolation (we should generally do this everywhere) --- .../integrations/opentelemetry/utils.py | 6 +++-- sentry_sdk/integrations/wsgi.py | 10 ++++--- .../integrations/opentelemetry/test_utils.py | 26 +++++++++---------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/sentry_sdk/integrations/opentelemetry/utils.py b/sentry_sdk/integrations/opentelemetry/utils.py index 544362a87c..07e60ddd0f 100644 --- a/sentry_sdk/integrations/opentelemetry/utils.py +++ b/sentry_sdk/integrations/opentelemetry/utils.py @@ -234,7 +234,7 @@ def extract_span_status(span): return (inferred_status, http_status) if status and status.status_code == StatusCode.UNSET: - return (SPANSTATUS.OK, None) + return (None, None) else: return (SPANSTATUS.UNKNOWN_ERROR, None) @@ -308,9 +308,11 @@ def get_trace_context(span, span_data=None): "parent_span_id": parent_span_id, "op": op, "origin": origin or DEFAULT_SPAN_ORIGIN, - "status": status, } # type: dict[str, Any] + if status: + trace_context["status"] = status + if span.attributes: trace_context["data"] = dict(span.attributes) diff --git a/sentry_sdk/integrations/wsgi.py b/sentry_sdk/integrations/wsgi.py index 843cf56564..72ecfdb758 100644 --- a/sentry_sdk/integrations/wsgi.py +++ b/sentry_sdk/integrations/wsgi.py @@ -46,6 +46,8 @@ def __call__(self, status, response_headers, exc_info=None): # type: ignore _wsgi_middleware_applied = ContextVar("sentry_wsgi_middleware_applied") +DEFAULT_TRANSACTION_NAME = "generic WSGI request" + def wsgi_decoding_dance(s, charset="utf-8", errors="replace"): # type: (str, str, str) -> str @@ -96,6 +98,8 @@ 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) + with track_session(scope, session_mode="request"): with capture_internal_exceptions(): scope.clear_breadcrumbs() @@ -105,15 +109,13 @@ def __call__(self, environ, start_response): environ, self.use_x_forwarded_for ) ) - method = environ.get("REQUEST_METHOD", "").upper() should_trace = method in self.http_methods_to_capture with sentry_sdk.continue_trace(environ): with ( - sentry_sdk.start_transaction( - environ, + sentry_sdk.start_span( op=OP.HTTP_SERVER, - name="generic WSGI request", + name=DEFAULT_TRANSACTION_NAME, source=TRANSACTION_SOURCE_ROUTE, origin=self.span_origin, custom_sampling_context={"wsgi_environ": environ}, diff --git a/tests/integrations/opentelemetry/test_utils.py b/tests/integrations/opentelemetry/test_utils.py index 66ffd7898a..b6f1072480 100644 --- a/tests/integrations/opentelemetry/test_utils.py +++ b/tests/integrations/opentelemetry/test_utils.py @@ -16,54 +16,54 @@ [ ( "OTel Span Blank", - Status(StatusCode.UNSET), # Unset defaults to OK + Status(StatusCode.UNSET), {}, { "op": "OTel Span Blank", "description": "OTel Span Blank", - "status": "ok", + "status": None, "http_status_code": None, "origin": None, }, ), ( "OTel Span RPC", - Status(StatusCode.UNSET), # Unset defaults to OK + Status(StatusCode.UNSET), { "rpc.service": "myservice.EchoService", }, { "op": "rpc", "description": "OTel Span RPC", - "status": "ok", + "status": None, "http_status_code": None, "origin": None, }, ), ( "OTel Span Messaging", - Status(StatusCode.UNSET), # Unset defaults to OK + Status(StatusCode.UNSET), { "messaging.system": "rabbitmq", }, { "op": "message", "description": "OTel Span Messaging", - "status": "ok", + "status": None, "http_status_code": None, "origin": None, }, ), ( "OTel Span FaaS", - Status(StatusCode.UNSET), # Unset defaults to OK + Status(StatusCode.UNSET), { "faas.trigger": "pubsub", }, { "op": "pubsub", "description": "OTel Span FaaS", - "status": "ok", + "status": None, "http_status_code": None, "origin": None, }, @@ -241,13 +241,13 @@ def test_span_data_for_db_query(): ), ( SpanKind.SERVER, - Status(StatusCode.UNSET), # Unset defaults to OK + Status(StatusCode.UNSET), { "http.method": "POST", "http.route": "/some/route", }, { - "status": "ok", + "status": None, "http_status_code": None, }, ), @@ -336,15 +336,15 @@ def test_span_data_for_db_query(): SpanKind.SERVER, Status( StatusCode.ERROR, "I'm a teapot" - ), # Error status with unknown description is an unknown error + ), { "http.method": "POST", "http.route": "/some/route", "http.response.status_code": 418, }, { - "status": "unknown_error", - "http_status_code": None, + "status": "invalid_argument", + "http_status_code": 418, }, ), (