Skip to content

Commit f3734d4

Browse files
committed
Allow custom transaction names in asgi
1 parent e463034 commit f3734d4

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

sentry_sdk/integrations/asgi.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
TRANSACTION_SOURCE_ROUTE,
2929
TRANSACTION_SOURCE_URL,
3030
TRANSACTION_SOURCE_COMPONENT,
31+
TRANSACTION_SOURCE_CUSTOM,
3132
)
3233
from sentry_sdk.utils import (
3334
ContextVar,
@@ -274,6 +275,7 @@ def event_processor(self, event, hint, asgi_scope):
274275
].get("source") in [
275276
TRANSACTION_SOURCE_COMPONENT,
276277
TRANSACTION_SOURCE_ROUTE,
278+
TRANSACTION_SOURCE_CUSTOM,
277279
]
278280
if not already_set:
279281
name, source = self._get_transaction_name_and_source(

tests/integrations/asgi/test_asgi.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,31 @@ async def app(scope, receive, send):
126126
return app
127127

128128

129+
@pytest.fixture
130+
def asgi3_custom_transaction_app():
131+
132+
async def app(scope, receive, send):
133+
sentry_sdk.get_current_scope().set_transaction_name("foobar", source="custom")
134+
await send(
135+
{
136+
"type": "http.response.start",
137+
"status": 200,
138+
"headers": [
139+
[b"content-type", b"text/plain"],
140+
],
141+
}
142+
)
143+
144+
await send(
145+
{
146+
"type": "http.response.body",
147+
"body": b"Hello, world!",
148+
}
149+
)
150+
151+
return app
152+
153+
129154
def test_invalid_transaction_style(asgi3_app):
130155
with pytest.raises(ValueError) as exp:
131156
SentryAsgiMiddleware(asgi3_app, transaction_style="URL")
@@ -679,3 +704,18 @@ def dummy_traces_sampler(sampling_context):
679704

680705
async with TestClient(app) as client:
681706
await client.get(request_url)
707+
708+
709+
@pytest.mark.asyncio
710+
async def test_custom_transaction_name(sentry_init, asgi3_custom_transaction_app, capture_events):
711+
sentry_init(traces_sample_rate=1.0)
712+
events = capture_events()
713+
app = SentryAsgiMiddleware(asgi3_custom_transaction_app)
714+
715+
async with TestClient(app) as client:
716+
await client.get("/test")
717+
718+
(transaction_event,) = events
719+
assert transaction_event["type"] == "transaction"
720+
assert transaction_event["transaction"] == "foobar"
721+
assert transaction_event["transaction_info"] == {"source": "custom"}

0 commit comments

Comments
 (0)