8000 Fix WSGI tests (#3734) · getsentry/sentry-python@fb66e1a · GitHub
[go: up one dir, main page]

Skip to content

Commit fb66e1a

Browse files
authored
Fix WSGI tests (#3734)
* 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)
1 parent 64ae2ef commit fb66e1a

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

sentry_sdk/integrations/opentelemetry/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def extract_span_status(span):
234234
return (inferred_status, http_status)
235235

236236
if status and status.status_code == StatusCode.UNSET:
237-
return (SPANSTATUS.OK, None)
237+
return (None, None)
238238
else:
239239
return (SPANSTATUS.UNKNOWN_ERROR, None)
240240

@@ -308,9 +308,11 @@ def get_trace_context(span, span_data=None):
308308
"parent_span_id": parent_span_id,
309309
"op": op,
310310
"origin": origin or DEFAULT_SPAN_ORIGIN,
311-
"status": status,
312311
} # type: dict[str, Any]
313312

313+
if status:
314+
trace_context["status"] = status
315+
314316
if span.attributes:
315317
trace_context["data"] = dict(span.attributes)
316318

sentry_sdk/integrations/wsgi.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ def __call__(self, status, response_headers, exc_info=None): # type: ignore
4646

4747
_wsgi_middleware_applied = ContextVar("sentry_wsgi_middleware_applied")
4848

49+
DEFAULT_TRANSACTION_NAME = "generic WSGI request"
50+
4951

5052
def wsgi_decoding_dance(s, charset="utf-8", errors="replace"):
5153
# type: (str, str, str) -> str
@@ -96,6 +98,8 @@ def __call__(self, environ, start_response):
9698
_wsgi_middleware_applied.set(True)
9799
try:
98100
with sentry_sdk.isolation_scope() as scope:
101+
scope.set_transaction_name(DEFAULT_TRANSACTION_NAME, source=TRANSACTION_SOURCE_ROUTE)
102+
99103
with track_session(scope, session_mode="request"):
100104
with capture_internal_exceptions():
101105
scope.clear_breadcrumbs()
@@ -105,15 +109,13 @@ def __call__(self, environ, start_response):
105109
environ, self.use_x_forwarded_for
106110
)
107111
)
108-
109112
method = environ.get("REQUEST_METHOD", "").upper()
110113
should_trace = method in self.http_methods_to_capture
111114
with sentry_sdk.continue_trace(environ):
112115
with (
113-
sentry_sdk.start_transaction(
114-
environ,
116+
sentry_sdk.start_span(
115117
op=OP.HTTP_SERVER,
116-
name="generic WSGI request",
118+
name=DEFAULT_TRANSACTION_NAME,
117119
source=TRANSACTION_SOURCE_ROUTE,
118120
origin=self.span_origin,
119121
custom_sampling_context={"wsgi_environ": environ},

tests/integrations/opentelemetry/test_utils.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,54 +16,54 @@
1616
[
1717
(
1818
"OTel Span Blank",
19-
Status(StatusCode.UNSET), # Unset defaults to OK
19+
Status(StatusCode.UNSET),
2020
{},
2121
{
2222
"op": "OTel Span Blank",
2323
"description": "OTel Span Blank",
24-
"status": "ok",
24+
"status": None,
2525
"http_status_code": None,
2626
"origin": None,
2727
},
2828
),
2929
(
3030
"OTel Span RPC",
31-
Status(StatusCode.UNSET), # Unset defaults to OK
31+
Status(StatusCode.UNSET),
3232
{
3333
"rpc.service": "myservice.EchoService",
3434
},
3535
{
3636
"op": "rpc",
3737
"description": "OTel Span RPC",
38-
"status": "ok",
38+
"status": None,
3939
"http_status_code": None,
4040
"origin": None,
4141
},
4242
),
4343
(
4444
"OTel Span Messaging",
45-
Status(StatusCode.UNSET), # Unset defaults to OK
45+
Status(StatusCode.UNSET),
4646
{
4747
"messaging.system": "rabbitmq",
4848
},
4949
{
5050
"op": "message",
5151
"description": "OTel Span Messaging",
52-
"status": "ok",
52+
"status": None,
5353
"http_status_code": None,
5454
"origin": None,
5555
},
5656
),
5757
(
5858
"OTel Span FaaS",
59-
Status(StatusCode.UNSET), # Unset defaults to OK
59+
Status(StatusCode.UNSET),
6060
{
6161
"faas.trigger": "pubsub",
6262
},
6363
{
6464
"op": "pubsub",
6565
"description": "OTel Span FaaS",
66-
"status": "ok",
66+
"status": None,
6767
"http_status_code": None,
6868
"origin": None,
6969
},
@@ -241,13 +241,13 @@ def test_span_data_for_db_query():
241241
),
242242
(
243243
SpanKind.SERVER,
244-
Status(StatusCode.UNSET), # Unset defaults to OK
244+
Status(StatusCode.UNSET),
245245
{
246246
"http.method": "POST",
247247
"http.route": "/some/route",
248248
},
249249
{
250-
"status": "ok",
250+
"status": None,
251251
"http_status_code": None,
252252
},
253253
),
@@ -336,15 +336,15 @@ def test_span_data_for_db_query():
336336
SpanKind.SERVER,
337337
Status(
338338
StatusCode.ERROR, "I'm a teapot"
339-
), # Error status with unknown description is an unknown error
339+
),
340340
{
341341
"http.method": "POST",
342342
"http.route": "/some/route",
343343
"http.response.status_code": 418,
344344
},
345345
{
346-
"status": "unknown_error",
347-
"http_status_code": None,
346+
"status": "invalid_argument",
347+
"http_status_code": 418,
348348
},
349349
),
350350
(

0 commit comments

Comments
 (0)
0