Skip to content
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
21 changes: 12 additions & 9 deletions sentry_sdk/integrations/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion sentry_sdk/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion tests/integrations/asgi/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

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