Skip to content

Commit dd05c57

Browse files
authored
feat: Add tracing to ASGI (#502)
* feat: Add tracing to ASGI * fix: Formatting
1 parent 0b44394 commit dd05c57

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

sentry_sdk/integrations/asgi.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from sentry_sdk.hub import Hub, _should_send_default_pii
1212
from sentry_sdk.integrations._wsgi_common import _filter_headers
1313
from sentry_sdk.utils import ContextVar, event_from_exception, transaction_from_function
14+
from sentry_sdk.tracing import Span
1415

1516
if MYPY:
1617
from typing import Dict
@@ -60,21 +61,23 @@ async def _run_app(self, scope, callback):
6061
hub = Hub(Hub.current)
6162
with hub:
6263
with hub.configure_scope() as sentry_scope:
64+
sentry_scope.clear_breadcrumbs()
6365
sentry_scope._name = "asgi"
64-
sentry_scope.transaction = (
65-
scope.get("path") or "unknown asgi request"
66-
)
67-
6866
processor = functools.partial(
6967
self.event_processor, asgi_scope=scope
7068
)
7169
sentry_scope.add_event_processor(processor)
7270

73-
try:
74-
await callback()
75-
except Exception as exc:
76-
_capture_exception(hub, exc)
77-
raise exc from None
71+
span = Span.continue_from_headers(dict(scope["headers"]))
72+
span.op = "http.server"
73+
span.transaction = "generic ASGI request"
74+
75+
with hub.start_span(span) as span:
76+
try:
77+
return await callback()
78+
except Exception as exc:
79+
_capture_exception(hub, exc)
80+
raise exc from None
7881
finally:
7982
_asgi_middleware_applied.set(False)
8083

sentry_sdk/tracing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def from_traceparent(cls, traceparent):
191191
if traceparent.startswith("00-") and traceparent.endswith("-00"):
192192
traceparent = traceparent[3:-3]
193193

194-
match = _traceparent_header_format_re.match(traceparent)
194+
match = _traceparent_header_format_re.match(str(traceparent))
195195
if match is None:
196196
return None
197197

tests/integrations/asgi/test_asgi.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_sync_request_data(sentry_init, app, capture_events):
3333
events = capture_events()
3434

3535
client = TestClient(app)
36-
response = client.get("/sync-message?foo=bar")
36+
response = client.get("/sync-message?foo=bar", headers={"Foo": u"ä"})
3737

3838
assert response.status_code == 200
3939

@@ -46,6 +46,7 @@ def test_sync_request_data(sentry_init, app, capture_events):
4646
"connection",
4747
"host",
4848
"user-agent",
49+
"foo",
4950
}
5051
assert event["request"]["query_string"] == "foo=bar"
5152
assert event["request"]["url"].endswith("/sync-message")

0 commit comments

Comments
 (0)