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
10 changes: 8 additions & 2 deletions sentry_sdk/integrations/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@
},
parent_span=None,
)
scope.get_current_scope()._server_segment_span = span_ctx
else:
transaction = continue_trace(
headers,
Expand Down Expand Up @@ -323,14 +324,19 @@
if integration is None:
return rv

route_info = rv.get_info()
pattern = route_info.get("path") or route_info.get("formatter")

server_span = sentry_sdk.get_current_scope()._server_segment_span
if server_span is not None and pattern is not None:
server_span.set_attribute(SPANDATA.HTTP_ROUTE, pattern)

Check warning on line 332 in sentry_sdk/integrations/aiohttp.py

View check run for this annotation

@sentry/warden / warden: code-review

Route info lookup no longer guarded, can break request resolve

Keep `rv.get_info()` inside the existing try/except (or wrap it with `capture_internal_exceptions`); if it raises, resolve now fails the request instead of only skipping transaction naming.

Check warning on line 332 in sentry_sdk/integrations/aiohttp.py

View check run for this annotation

@sentry/warden / warden: find-bugs

Route lookup moved outside try/except can break request handling

Keep `rv.get_info()` inside the existing try/except (or wrap it with `capture_internal_exceptions`); an unexpected exception there now fails the whole request instead of only skipping transaction naming.
Comment thread
alexander-alderman-webb marked this conversation as resolved.
Comment thread
alexander-alderman-webb marked this conversation as resolved.

name = None

try:
if integration.transaction_style == "handler_name":
name = transaction_from_function(rv.handler)
elif integration.transaction_style == "method_and_path_pattern":
route_info = rv.get_info()
pattern = route_info.get("path") or route_info.get("formatter")
name = "{} {}".format(request.method, pattern)
except Exception:
pass
Expand Down
36 changes: 36 additions & 0 deletions tests/integrations/aiohttp/test_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1882,6 +1882,42 @@ async def hello(request):
assert server_segment["attributes"]["sentry.segment.name.source"] == expected_source


@pytest.mark.asyncio
@pytest.mark.parametrize(
"url,expected_route",
[
("/message", "/{var}"),
],
)
async def test_http_route(
sentry_init,
aiohttp_client,
capture_items,
url,
expected_route,
):
sentry_init(
integrations=[AioHttpIntegration()],
traces_sample_rate=1.0,
trace_lifecycle="stream",
)

async def hello(request):
return web.Response(text="hello")

app = web.Application()
app.router.add_get(r"/{var}", hello)

items = capture_items("span")

client = await aiohttp_client(app)
await client.get(url)

sentry_sdk.flush()
(segment,) = (item.payload for item in items if item.payload.get("is_segment"))
assert segment["attributes"][SPANDATA.HTTP_ROUTE] == expected_route


@pytest.mark.asyncio
async def test_server_error_span_streaming(sentry_init, aiohttp_client, capture_items):
sentry_init(
Expand Down
Loading