Skip to content

Commit 9811845

Browse files
feat(fastapi): Add http.route attribute
1 parent 58823cc commit 9811845

4 files changed

Lines changed: 12 additions & 0 deletions

File tree

sentry_sdk/consts.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,12 @@ class SPANDATA:
878878
Example: GET
879879
"""
880880

881+
HTTP_ROUTE = "http.route"
882+
"""
883+
The matched route, that is, the path template used to match the request.
884+
Example: /users/{id}
885+
"""
886+
881887
HTTP_QUERY = "http.query"
882888
"""
883889
The Query string present in the URL.

sentry_sdk/integrations/asgi.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,9 @@ async def _sentry_wrapped_send(
412412
span.set_attribute(
413413
"sentry.segment.name.source", source
414414
)
415+
http_route = getattr(sentry_sdk.get_current_scope(), "_http_route", None)
416+
if http_route is not None:
417+
span.set_attribute(SPANDATA.HTTP_ROUTE, http_route)
415418
finally:
416419
_asgi_middleware_applied.set(False)
417420

sentry_sdk/integrations/fastapi.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ def _set_transaction_name_and_source(
7979
if path is not None:
8080
name = path
8181

82+
scope._http_route = route
83+
8284
if not name:
8385
name = _DEFAULT_TRANSACTION_NAME
8486
source = TransactionSource.ROUTE

sentry_sdk/scope.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ class Scope:
240240
"_last_event_id",
241241
"_flags",
242242
"_attributes",
243+
"_http_route",
243244
)
244245

245246
def __init__(

0 commit comments

Comments
 (0)