Skip to content

Commit 985bfad

Browse files
committed
fix(aiohttp): Correctly apply transaction/segment name
1 parent 8e6d73b commit 985bfad

2 files changed

Lines changed: 40 additions & 7 deletions

File tree

sentry_sdk/integrations/aiohttp.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
SegmentNameSource,
3030
SpanStatus,
3131
StreamedSpan,
32+
get_current_span,
3233
)
3334
from sentry_sdk.tracing import (
3435
BAGGAGE_HEADER_NAME,
@@ -339,7 +340,13 @@ async def sentry_urldispatcher_resolve(
339340
pass
340341

341342
if name is not None:
342-
current_span = sentry_sdk.get_current_span()
343+
current_scope = sentry_sdk.get_current_scope()
344+
current_scope.set_transaction_name(
345+
name,
346+
source=SOURCE_FOR_STYLE[integration.transaction_style],
347+
)
348+
349+
current_span = get_current_span()
343350
if isinstance(current_span, StreamedSpan) and not isinstance(
344351
current_span, NoOpStreamedSpan
345352
):
@@ -348,12 +355,6 @@ async def sentry_urldispatcher_resolve(
348355
"sentry.segment.name.source",
349356
SEGMENT_SOURCE_FOR_STYLE[integration.transaction_style].value,
350357
)
351-
else:
352-
current_scope = sentry_sdk.get_current_scope()
353-
current_scope.set_transaction_name(
354-
name,
355-
source=SOURCE_FOR_STYLE[integration.transaction_style],
356-
)
357358

358359
return rv
359360

tests/integrations/aiohttp/test_aiohttp.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,38 @@ async def hello(request):
19181918
assert server_span["status"] == "error"
19191919

19201920

1921+
@pytest.mark.asyncio
1922+
async def test_streaming_route_name_is_applied_to_event_and_segment(
1923+
sentry_init, aiohttp_client, capture_items
1924+
):
1925+
sentry_init(
1926+
integrations=[AioHttpIntegration(transaction_style="method_and_path_pattern")],
1927+
traces_sample_rate=1.0,
1928+
trace_lifecycle="stream",
1929+
)
1930+
1931+
async def hello(request):
1932+
1 / 0
1933+
1934+
app = web.Application()
1935+
app.router.add_get(r"/{var}", hello)
1936+
1937+
items = capture_items("event", "span")
1938+
1939+
client = await aiohttp_client(app)
1940+
resp = await client.get("/message")
1941+
assert resp.status == 500
1942+
1943+
sentry_sdk.flush()
1944+
1945+
assert len(items) == 2
1946+
error_event = items[0].payload
1947+
server_segment = items[1].payload
1948+
1949+
assert error_event["transaction"] == "GET /{var}"
1950+
assert server_segment["name"] == "GET /{var}"
1951+
1952+
19211953
@pytest.mark.asyncio
19221954
async def test_http_exception_span_streaming(
19231955
sentry_init, aiohttp_client, capture_items

0 commit comments

Comments
 (0)