Skip to content

fix(integrations): ASGI integration not capture transactions in Websocket #4293

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 3 commits into from
Apr 22, 2025
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
25 changes: 13 additions & 12 deletions sentry_sdk/integrations/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ async def _run_app(self, scope, receive, send, asgi_version):

method = scope.get("method", "").upper()
transaction = None
if method in self.http_methods_to_capture:
if ty in ("http", "websocket"):
if ty in ("http", "websocket"):
if ty == "websocket" or method in self.http_methods_to_capture:
transaction = continue_trace(
_get_headers(scope),
op="{}.server".format(ty),
Expand All @@ -205,17 +205,18 @@ async def _run_app(self, scope, receive, send, asgi_version):
"[ASGI] Created transaction (continuing trace): %s",
transaction,
)
else:
transaction = Transaction(
op=OP.HTTP_SERVER,
name=transaction_name,
source=transaction_source,
origin=self.span_origin,
)
logger.debug(
"[ASGI] Created transaction (new): %s", transaction
)
else:
transaction = Transaction(
op=OP.HTTP_SERVER,
name=transaction_name,
source=transaction_source,
origin=self.span_origin,
)
logger.debug(
"[ASGI] Created transaction (new): %s", transaction
)

if transaction:
transaction.set_tag("asgi.type", ty)
logger.debug(
"[ASGI] Set transaction name and source on transaction: '%s' / '%s'",
Expand Down
25 changes: 11 additions & 14 deletions tests/integrations/asgi/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,35 +349,32 @@ async def test_trace_from_headers_if_performance_disabled(

@pytest.mark.asyncio
async def test_websocket(sentry_init, asgi3_ws_app, capture_events, request):
sentry_init(send_default_pii=True)
sentry_init(send_default_pii=True, traces_sample_rate=1.0)

events = capture_events()

asgi3_ws_app = SentryAsgiMiddleware(asgi3_ws_app)

scope = {
"type": "websocket",
"endpoint": asgi3_app,
"client": ("127.0.0.1", 60457),
"route": "some_url",
"headers": [
("accept", "*/*"),
],
}
request_url = "/ws"

with pytest.raises(ValueError):
async with TestClient(asgi3_ws_app, scope=scope) as client:
async with client.websocket_connect("/ws") as ws:
await ws.receive_text()
client = TestClient(asgi3_ws_app)
async with client.websocket_connect(request_url) as ws:
await ws.receive_text()

msg_event, error_event = events
msg_event, error_event, transaction_event = events

assert msg_event["transaction"] == request_url
assert msg_event["transaction_info"] == {"source": "url"}
assert msg_event["message"] == "Some message to the world!"

(exc,) = error_event["exception"]["values"]
assert exc["type"] == "ValueError"
assert exc["value"] == "Oh no"

assert transaction_event["transaction"] == request_url
assert transaction_event["transaction_info"] == {"source": "url"}


@pytest.mark.asyncio
async def test_auto_session_tracking_with_aggregates(
Expand Down
Loading