Skip to content

Commit 747eaa8

Browse files
feat(quart): Add http.route attribute (#7348)
Set the `http.route` attribute on the ASGI server span in patches for Quart endpoints. Co-authored-by: Matt Quinn <matt.quinn@sentry.io>
1 parent 959bad7 commit 747eaa8

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

sentry_sdk/integrations/quart.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import TYPE_CHECKING
66

77
import sentry_sdk
8+
from sentry_sdk.consts import SPANDATA
89
from sentry_sdk.data_collection import _apply_data_collection_filtering_to_query_string
910
from sentry_sdk.integrations import DidNotEnable, Integration
1011
from sentry_sdk.integrations._wsgi_common import _filter_headers
@@ -181,6 +182,16 @@ async def _request_websocket_started(app: "Quart", **kwargs: "Any") -> None:
181182
if has_websocket_context():
182183
request_websocket = websocket._get_current_object()
183184

185+
route_path = None
186+
try:
187+
route_path = request.url_rule.rule
188+
except Exception:
189+
pass
190+
191+
server_span = sentry_sdk.get_current_scope()._server_segment_span
192+
if server_span is not None and route_path is not None:
193+
server_span.set_attribute(SPANDATA.HTTP_ROUTE, route_path)
194+
184195
# Set the transaction name here, but rely on ASGI middleware
185196
# to actually start the transaction
186197
_set_transaction_name_and_source(

tests/integrations/quart/test_quart.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
set_tag,
1515
)
1616
from sentry_sdk._types import SENSITIVE_DATA_SUBSTITUTE
17+
from sentry_sdk.consts import SPANDATA
1718
from sentry_sdk.integrations.logging import LoggingIntegration
1819
from sentry_sdk.utils import parse_version
1920

@@ -176,6 +177,29 @@ async def test_transaction_style(
176177
assert event["transaction"] == expected_transaction
177178

178179

180+
@pytest.mark.asyncio
181+
async def test_http_route(
182+
sentry_init,
183+
capture_items,
184+
):
185+
sentry_init(
186+
integrations=[quart_sentry.QuartIntegration()],
187+
traces_sample_rate=1.0,
188+
trace_lifecycle="stream",
189+
)
190+
191+
app = quart_app_factory()
192+
items = capture_items("span")
193+
194+
client = app.test_client()
195+
await client.get("/message/123456")
196+
197+
sentry_sdk.flush()
198+
199+
(segment,) = [item.payload for item in items if item.payload.get("is_segment")]
200+
assert segment["attributes"][SPANDATA.HTTP_ROUTE] == "/message/<message_id>"
201+
202+
179203
@pytest.mark.asyncio
180204
async def test_errors(
181205
sentry_init,

0 commit comments

Comments
 (0)