diff --git a/sentry_sdk/integrations/asgi.py b/sentry_sdk/integrations/asgi.py index 07dc2c993b..11b8d1caa7 100644 --- a/sentry_sdk/integrations/asgi.py +++ b/sentry_sdk/integrations/asgi.py @@ -11,6 +11,7 @@ from sentry_sdk.hub import Hub, _should_send_default_pii from sentry_sdk.integrations._wsgi_common import _filter_headers from sentry_sdk.utils import ContextVar, event_from_exception, transaction_from_function +from sentry_sdk.tracing import Span if MYPY: from typing import Dict @@ -60,21 +61,23 @@ async def _run_app(self, scope, callback): hub = Hub(Hub.current) with hub: with hub.configure_scope() as sentry_scope: + sentry_scope.clear_breadcrumbs() sentry_scope._name = "asgi" - sentry_scope.transaction = ( - scope.get("path") or "unknown asgi request" - ) - processor = functools.partial( self.event_processor, asgi_scope=scope ) sentry_scope.add_event_processor(processor) - try: - await callback() - except Exception as exc: - _capture_exception(hub, exc) - raise exc from None + span = Span.continue_from_headers(dict(scope["headers"])) + span.op = "http.server" + span.transaction = "generic ASGI request" + + with hub.start_span(span) as span: + try: + return await callback() + except Exception as exc: + _capture_exception(hub, exc) + raise exc from None finally: _asgi_middleware_applied.set(False) diff --git a/sentry_sdk/tracing.py b/sentry_sdk/tracing.py index dbba75a1cf..1fced63001 100644 --- a/sentry_sdk/tracing.py +++ b/sentry_sdk/tracing.py @@ -191,7 +191,7 @@ def from_traceparent(cls, traceparent): if traceparent.startswith("00-") and traceparent.endswith("-00"): traceparent = traceparent[3:-3] - match = _traceparent_header_format_re.match(traceparent) + match = _traceparent_header_format_re.match(str(traceparent)) if match is None: return None diff --git a/tests/integrations/asgi/test_asgi.py b/tests/integrations/asgi/test_asgi.py index 8ee2e700b4..3a47eaca32 100644 --- a/tests/integrations/asgi/test_asgi.py +++ b/tests/integrations/asgi/test_asgi.py @@ -33,7 +33,7 @@ def test_sync_request_data(sentry_init, app, capture_events): events = capture_events() client = TestClient(app) - response = client.get("/sync-message?foo=bar") + response = client.get("/sync-message?foo=bar", headers={"Foo": u"รค"}) assert response.status_code == 200 @@ -46,6 +46,7 @@ def test_sync_request_data(sentry_init, app, capture_events): "connection", "host", "user-agent", + "foo", } assert event["request"]["query_string"] == "foo=bar" assert event["request"]["url"].endswith("/sync-message")