Skip to content

Commit b96e2b6

Browse files
authored
fix(integrations): ASGI integration not capture transactions in Websocket (#4293)
In [ASGI Specs](https://github.com/django/asgiref/blob/main/specs/www.rst#websocket-connection-scope), `method` is not in Websocket Connection Scope.
1 parent d1819c7 commit b96e2b6

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

sentry_sdk/integrations/asgi.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ async def _run_app(self, scope, receive, send, asgi_version):
192192

193193
method = scope.get("method", "").upper()
194194
transaction = None
195-
if method in self.http_methods_to_capture:
196-
if ty in ("http", "websocket"):
195+
if ty in ("http", "websocket"):
196+
if ty == "websocket" or method in self.http_methods_to_capture:
197197
transaction = continue_trace(
198198
_get_headers(scope),
199199
op="{}.server".format(ty),
@@ -205,17 +205,18 @@ async def _run_app(self, scope, receive, send, asgi_version):
205205
"[ASGI] Created transaction (continuing trace): %s",
206206
transaction,
207207
)
208-
else:
209-
transaction = Transaction(
210-
op=OP.HTTP_SERVER,
211-
name=transaction_name,
212-
source=transaction_source,
213-
origin=self.span_origin,
214-
)
215-
logger.debug(
216-
"[ASGI] Created transaction (new): %s", transaction
217-
)
208+
else:
209+
transaction = Transaction(
210+
op=OP.HTTP_SERVER,
211+
name=transaction_name,
212+
source=transaction_source,
213+
origin=self.span_origin,
214+
)
215+
logger.debug(
216+
"[ASGI] Created transaction (new): %s", transaction
217+
)
218218

219+
if transaction:
219220
transaction.set_tag("asgi.type", ty)
220221
logger.debug(
221222
"[ASGI] Set transaction name and source on transaction: '%s' / '%s'",

tests/integrations/asgi/test_asgi.py

+11-14
Original file line numberDiff line numberDiff line change
@@ -349,35 +349,32 @@ async def test_trace_from_headers_if_performance_disabled(
349349

350350
@pytest.mark.asyncio
351351
async def test_websocket(sentry_init, asgi3_ws_app, capture_events, request):
352-
sentry_init(send_default_pii=True)
352+
sentry_init(send_default_pii=True, traces_sample_rate=1.0)
353353

354354
events = capture_events()
355355

356356
asgi3_ws_app = SentryAsgiMiddleware(asgi3_ws_app)
357357

358-
scope = {
359-
"type": "websocket",
360-
"endpoint": asgi3_app,
361-
"client": ("127.0.0.1", 60457),
362-
"route": "some_url",
363-
"headers": [
364-
("accept", "*/*"),
365-
],
366-
}
358+
request_url = "/ws"
367359

368360
with pytest.raises(ValueError):
369-
async with TestClient(asgi3_ws_app, scope=scope) as client:
370-
async with client.websocket_connect("/ws") as ws:
371-
await ws.receive_text()
361+
client = TestClient(asgi3_ws_app)
362+
async with client.websocket_connect(request_url) as ws:
363+
await ws.receive_text()
372364

373-
msg_event, error_event = events
365+
msg_event, error_event, transaction_event = events
374366

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

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

375+
assert transaction_event["transaction"] == request_url
376+
assert transaction_event["transaction_info"] == {"source": "url"}
377+
381378

382379
@pytest.mark.asyncio
383380
async def test_auto_session_tracking_with_aggregates(

0 commit comments

Comments
 (0)