From 5ccb5909d02045090ae9c8a5cf497b367a523a16 Mon Sep 17 00:00:00 2001 From: Guo Dong Date: Tue, 15 Apr 2025 01:07:29 +0800 Subject: [PATCH 1/3] Fix ASGI integration not capture in websocket --- sentry_sdk/integrations/asgi.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/sentry_sdk/integrations/asgi.py b/sentry_sdk/integrations/asgi.py index 3569336aae..fc8ee29b1a 100644 --- a/sentry_sdk/integrations/asgi.py +++ b/sentry_sdk/integrations/asgi.py @@ -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), @@ -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'", From da2d4f7591ec7df53fc8a3e1cfd15987d7381645 Mon Sep 17 00:00:00 2001 From: Guo Dong Date: Tue, 15 Apr 2025 23:50:14 +0800 Subject: [PATCH 2/3] fix test_asgi.test_websocket --- tests/integrations/asgi/test_asgi.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/tests/integrations/asgi/test_asgi.py b/tests/integrations/asgi/test_asgi.py index f95ea14d01..cbc84b2de4 100644 --- a/tests/integrations/asgi/test_asgi.py +++ b/tests/integrations/asgi/test_asgi.py @@ -355,23 +355,17 @@ async def test_websocket(sentry_init, asgi3_ws_app, capture_events, request): 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 + 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"] From c963b6fd70f04922c6016789b7ccaa394cadadf4 Mon Sep 17 00:00:00 2001 From: Guo Dong Date: Fri, 18 Apr 2025 23:27:22 +0800 Subject: [PATCH 3/3] test: update test_asgi.test_websocket --- tests/integrations/asgi/test_asgi.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/integrations/asgi/test_asgi.py b/tests/integrations/asgi/test_asgi.py index cbc84b2de4..ec2796c140 100644 --- a/tests/integrations/asgi/test_asgi.py +++ b/tests/integrations/asgi/test_asgi.py @@ -349,7 +349,7 @@ 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() @@ -362,16 +362,19 @@ async def test_websocket(sentry_init, asgi3_ws_app, capture_events, request): 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["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(