Skip to content

Fix WSGI tests #3734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions sentry_sdk/integrations/opentelemetry/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
10 changes: 6 additions & 4 deletions sentry_sdk/integrations/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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},
Expand Down
26 changes: 13 additions & 13 deletions tests/integrations/opentelemetry/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
},
),
Expand Down Expand Up @@ -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,
},
),
(
Expand Down
Loading