Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
255fcee
fix(asgi): Stop duplicating root_path in URLs
alexander-alderman-webb Jun 16, 2026
89023f4
starlette and fastapi tests
alexander-alderman-webb Jun 19, 2026
a58e42d
remanining tests
alexander-alderman-webb Jun 19, 2026
42aca21
update
alexander-alderman-webb Jun 19, 2026
ad1d00f
update
alexander-alderman-webb Jun 19, 2026
539b1cd
logic error
alexander-alderman-webb Jun 19, 2026
a7112ba
fix quart tests
alexander-alderman-webb Jun 19, 2026
574b5e5
fix django tests
alexander-alderman-webb Jun 19, 2026
e7c7fab
fix django tests
alexander-alderman-webb Jun 19, 2026
79cf430
Merge branch 'master' into webb/asgi/double-mount-prefix
alexander-alderman-webb Jun 19, 2026
518acbe
fix quart url
alexander-alderman-webb Jun 19, 2026
b420587
update docstrings
alexander-alderman-webb Jun 19, 2026
eda0dec
revert litestar and starlite tests as behavior unchanged
alexander-alderman-webb Jun 23, 2026
dc3d352
update django condition
alexander-alderman-webb Jun 23, 2026
a96d284
restore whitespace
alexander-alderman-webb Jun 23, 2026
0b1570b
only change starlette and fastapi
alexander-alderman-webb Jun 26, 2026
e5d65f8
Merge branch 'master' into webb/asgi/double-mount-prefix
alexander-alderman-webb Jun 26, 2026
ae1b1d0
update url.path
alexander-alderman-webb Jun 26, 2026
cb28622
add path_includes_root_path in channels middleware
alexander-alderman-webb Jun 26, 2026
7d263e4
remove test
alexander-alderman-webb Jun 26, 2026
c3050ce
use enum and tolerate both
alexander-alderman-webb Jun 26, 2026
557c418
remove default arguments
alexander-alderman-webb Jun 26, 2026
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
6 changes: 6 additions & 0 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,12 @@ class SPANDATA:
Example: "details"
"""

URL_PATH = "url.path"
"""
The URI path component.
Example: "/foo"
"""

URL_QUERY = "url.query"
"""
The query string present in the URL. Note that this does not contain the leading ? character, while the `http.query` attribute does.
Expand Down
50 changes: 42 additions & 8 deletions sentry_sdk/integrations/_asgi_common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import urllib
from enum import Enum
from typing import TYPE_CHECKING

from sentry_sdk.integrations._wsgi_common import _filter_headers
Expand All @@ -12,6 +13,11 @@
from sentry_sdk.utils import AnnotatedValue


class _RootPathInPath(Enum):
EXCLUDED = "excluded"
EITHER = "either"


def _get_headers(asgi_scope: "Any") -> "Dict[str, str]":
"""
Extract headers from the ASGI scope, in the format that the Sentry protocol expects.
Expand All @@ -28,18 +34,34 @@ def _get_headers(asgi_scope: "Any") -> "Dict[str, str]":
return headers


def _get_path(
asgi_scope: "Dict[str, Any]", root_path_in_path: "_RootPathInPath"
) -> "str":
if root_path_in_path is _RootPathInPath.EXCLUDED:
return asgi_scope.get("root_path", "") + asgi_scope.get("path", "")

# Inverse of https://github.com/Kludex/starlette/blob/de970d7b3facb853eb7ad077decbf3d94f2aab6c/starlette/_utils.py#L96
path = asgi_scope["path"]
root_path = asgi_scope.get("root_path", "")

if not root_path or path == root_path or path.startswith(root_path + "/"):
return path

return root_path + path


def _get_url(
asgi_scope: "Dict[str, Any]",
default_scheme: "Literal['ws', 'http']",
host: "Optional[Union[AnnotatedValue, str]]",
path: str,
) -> str:
"""
Extract URL from the ASGI scope, without also including the querystring.
"""
scheme = asgi_scope.get("scheme", default_scheme)

server = asgi_scope.get("server", None)
path = asgi_scope.get("root_path", "") + asgi_scope.get("path", "")

if host:
return "%s://%s%s" % (scheme, host, path)
Expand Down Expand Up @@ -81,7 +103,10 @@ def _get_ip(asgi_scope: "Any") -> str:
return asgi_scope.get("client")[0]


def _get_request_data(asgi_scope: "Any") -> "Dict[str, Any]":
def _get_request_data(
asgi_scope: "Any",
root_path_in_path: "_RootPathInPath",
) -> "Dict[str, Any]":
"""
Returns data related to the HTTP request from the ASGI scope.
"""
Expand All @@ -96,7 +121,10 @@ def _get_request_data(asgi_scope: "Any") -> "Dict[str, Any]":
request_data["query_string"] = _get_query(asgi_scope)

request_data["url"] = _get_url(
asgi_scope, "http" if ty == "http" else "ws", headers.get("host")
asgi_scope,
"http" if ty == "http" else "ws",
headers.get("host"),
path=_get_path(asgi_scope=asgi_scope, root_path_in_path=root_path_in_path),
)

client = asgi_scope.get("client")
Expand All @@ -106,7 +134,10 @@ def _get_request_data(asgi_scope: "Any") -> "Dict[str, Any]":
return request_data


def _get_request_attributes(asgi_scope: "Any") -> "dict[str, Any]":
def _get_request_attributes(
asgi_scope: "Any",
root_path_in_path: "_RootPathInPath",
) -> "dict[str, Any]":
"""
Return attributes related to the HTTP request from the ASGI scope.
"""
Expand All @@ -126,18 +157,21 @@ def _get_request_attributes(asgi_scope: "Any") -> "dict[str, Any]":
if query:
attributes["http.query"] = query

path = _get_path(asgi_scope=asgi_scope, root_path_in_path=root_path_in_path)
attributes["url.path"] = path

url_without_query_string = _get_url(
asgi_scope, "http" if ty == "http" else "ws", headers.get("host")
asgi_scope,
"http" if ty == "http" else "ws",
headers.get("host"),
path=path,
)
query_string = _get_query(asgi_scope)
attributes["url.full"] = (
Comment thread
alexander-alderman-webb marked this conversation as resolved.
f"{url_without_query_string}?{query_string}"
if query_string is not None
else url_without_query_string
)
attributes["url.path"] = asgi_scope.get("root_path", "") + asgi_scope.get(
"path", ""
)

client = asgi_scope.get("client")
if client and should_send_default_pii():
Expand Down
48 changes: 42 additions & 6 deletions sentry_sdk/integrations/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.integrations._asgi_common import (
_get_headers,
_get_ip,

Check warning on line 18 in sentry_sdk/integrations/asgi.py

View check run for this annotation

@sentry/warden / warden: code-review

[687-F25] New `root_path` deduplication logic in `_get_path()` has no test coverage (additional location)

The PR adds a `root_path_in_path` parameter and a `_RootPathInPath.EITHER` branch in `_get_path()` (`sentry_sdk/integrations/_asgi_common.py`) to avoid duplicating `scope["root_path"]` in URLs. The branch performs non-trivial prefix detection (`path.startswith(root_path + "/")`) and the Starlette integration selects between `EXCLUDED` and `EITHER` based on the Starlette version (`>= (0, 33)`). Despite the PR description stating that tests were added to each ASGI integration, no test in the suite exercises this logic: there are zero references to `root_path` in any test file. The version-sensitive, prefix-detecting behavior is therefore regression-prone, with edge cases (e.g. `root_path="/api"`, `path="/api-docs"`) silently untested.
_get_path,
_get_request_attributes,
_get_request_data,
_get_url,
_RootPathInPath,
)
from sentry_sdk.integrations._wsgi_common import (
DEFAULT_HTTP_METHODS_TO_CAPTURE,
Expand Down Expand Up @@ -105,6 +107,7 @@
"mechanism_type",
"span_origin",
"http_methods_to_capture",
"root_path_in_path",
)

def __init__(
Expand All @@ -116,6 +119,7 @@
span_origin: str = "manual",
http_methods_to_capture: "Tuple[str, ...]" = DEFAULT_HTTP_METHODS_TO_CAPTURE,
asgi_version: "Optional[int]" = None,
root_path_in_path: "_RootPathInPath" = _RootPathInPath.EXCLUDED,

Check warning on line 122 in sentry_sdk/integrations/asgi.py

View check run for this annotation

@sentry/warden / warden: code-review

New `root_path` deduplication logic in `_get_path()` has no test coverage

The PR adds a `root_path_in_path` parameter and a `_RootPathInPath.EITHER` branch in `_get_path()` (`sentry_sdk/integrations/_asgi_common.py`) to avoid duplicating `scope["root_path"]` in URLs. The branch performs non-trivial prefix detection (`path.startswith(root_path + "/")`) and the Starlette integration selects between `EXCLUDED` and `EITHER` based on the Starlette version (`>= (0, 33)`). Despite the PR description stating that tests were added to each ASGI integration, no test in the suite exercises this logic: there are zero references to `root_path` in any test file. The version-sensitive, prefix-detecting behavior is therefore regression-prone, with edge cases (e.g. `root_path="/api"`, `path="/api-docs"`) silently untested.
Comment thread
alexander-alderman-webb marked this conversation as resolved.
) -> None:
"""
Instrument an ASGI application with Sentry. Provides HTTP/websocket
Expand Down Expand Up @@ -152,6 +156,7 @@
self.span_origin = span_origin
self.app = app
self.http_methods_to_capture = http_methods_to_capture
self.root_path_in_path = root_path_in_path

if asgi_version is None:
if _looks_like_asgi3(app):
Expand Down Expand Up @@ -319,7 +324,8 @@
with span_ctx as span:
if isinstance(span, StreamedSpan):
for attribute, value in _get_request_attributes(
scope
scope,
root_path_in_path=self.root_path_in_path,
).items():
span.set_attribute(attribute, value)

Expand Down Expand Up @@ -401,7 +407,9 @@
self, event: "Event", hint: "Hint", asgi_scope: "Any"
) -> "Optional[Event]":
request_data = event.get("request", {})
request_data.update(_get_request_data(asgi_scope))
request_data.update(
_get_request_data(asgi_scope, root_path_in_path=self.root_path_in_path)
)
event["request"] = deepcopy(request_data)

# Only set transaction name if not already set by Starlette or FastAPI (or other frameworks)
Expand Down Expand Up @@ -447,7 +455,14 @@
if endpoint:
name = transaction_from_function(endpoint) or ""
else:
name = _get_url(asgi_scope, "http" if ty == "http" else "ws", host=None)
name = _get_url(
asgi_scope,
"http" if ty == "http" else "ws",
host=None,
path=_get_path(
asgi_scope=asgi_scope, root_path_in_path=self.root_path_in_path
),
)
source = TransactionSource.URL

elif transaction_style == "url":
Expand All @@ -459,7 +474,14 @@
if path is not None:
name = path
else:
name = _get_url(asgi_scope, "http" if ty == "http" else "ws", host=None)
name = _get_url(
asgi_scope,
"http" if ty == "http" else "ws",
host=None,
path=_get_path(
asgi_scope=asgi_scope, root_path_in_path=self.root_path_in_path
),
)
source = TransactionSource.URL

if name is None:
Expand All @@ -484,7 +506,14 @@
if endpoint:
name = qualname_from_function(endpoint) or ""
else:
name = _get_url(asgi_scope, "http" if ty == "http" else "ws", host=None)
name = _get_url(
asgi_scope,
"http" if ty == "http" else "ws",
host=None,
path=_get_path(
asgi_scope=asgi_scope, root_path_in_path=self.root_path_in_path
),
)
source = SegmentSource.URL.value

elif segment_style == "url":
Expand All @@ -496,7 +525,14 @@
if path is not None:
name = path
else:
name = _get_url(asgi_scope, "http" if ty == "http" else "ws", host=None)
name = _get_url(
asgi_scope,
"http" if ty == "http" else "ws",
host=None,
path=_get_path(
asgi_scope=asgi_scope, root_path_in_path=self.root_path_in_path
),
)
source = SegmentSource.URL.value

if name is None:
Expand Down
11 changes: 9 additions & 2 deletions sentry_sdk/integrations/starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
DidNotEnable,
Integration,
)
from sentry_sdk.integrations._asgi_common import _RootPathInPath
from sentry_sdk.integrations._wsgi_common import (
DEFAULT_HTTP_METHODS_TO_CAPTURE,
HttpCodeRangeContainer,
Expand Down Expand Up @@ -143,7 +144,12 @@
)

patch_middlewares()
patch_asgi_app()
# Starlette tolerates both starting with:
# https://github.com/Kludex/starlette/commit/e8f0dcd54e4ceec47e02c45f5275374e292339ad.
root_path_in_path = (
_RootPathInPath.EITHER if version >= (0, 33) else _RootPathInPath.EXCLUDED
)
patch_asgi_app(root_path_in_path=root_path_in_path)
patch_request_response()

if version >= (0, 24):
Expand Down Expand Up @@ -427,7 +433,7 @@
Middleware.__init__ = _sentry_middleware_init


def patch_asgi_app() -> None:
def patch_asgi_app(root_path_in_path: "_RootPathInPath") -> None:
"""
Instrument Starlette ASGI app using the SentryAsgiMiddleware.
"""
Expand All @@ -448,9 +454,10 @@
http_methods_to_capture=(
integration.http_methods_to_capture
if integration
else DEFAULT_HTTP_METHODS_TO_CAPTURE

Check warning on line 457 in sentry_sdk/integrations/starlette.py

View check run for this annotation

@sentry/warden / warden: code-review

[687-F25] New `root_path` deduplication logic in `_get_path()` has no test coverage (additional location)

The PR adds a `root_path_in_path` parameter and a `_RootPathInPath.EITHER` branch in `_get_path()` (`sentry_sdk/integrations/_asgi_common.py`) to avoid duplicating `scope["root_path"]` in URLs. The branch performs non-trivial prefix detection (`path.startswith(root_path + "/")`) and the Starlette integration selects between `EXCLUDED` and `EITHER` based on the Starlette version (`>= (0, 33)`). Despite the PR description stating that tests were added to each ASGI integration, no test in the suite exercises this logic: there are zero references to `root_path` in any test file. The version-sensitive, prefix-detecting behavior is therefore regression-prone, with edge cases (e.g. `root_path="/api"`, `path="/api-docs"`) silently untested.
),
asgi_version=3,
root_path_in_path=root_path_in_path,
)

return await middleware(scope, receive, send)
Expand Down
24 changes: 0 additions & 24 deletions tests/integrations/asgi/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,30 +243,6 @@ async def test_capture_transaction(
}


@pytest.mark.asyncio
async def test_capture_transaction_with_root_path(
sentry_init,
asgi3_app,
capture_items,
):
sentry_init(
send_default_pii=True,
traces_sample_rate=1.0,
_experiments={"trace_lifecycle": "stream"},
)
app = SentryAsgiMiddleware(asgi3_app)

async with TestClient(app, scope={"root_path": "/api"}) as client:
items = capture_items("span")
await client.get("/some_url")

sentry_sdk.flush()

assert len(items) == 1
span = items[0].payload
assert span["attributes"]["url.path"] == "/api/some_url"


@pytest.mark.asyncio
@pytest.mark.parametrize(
"span_streaming",
Expand Down
46 changes: 46 additions & 0 deletions tests/integrations/fastapi/test_fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@

def fastapi_app_factory():
app = FastAPI()
mounted_app = FastAPI()

@app.get("/error")
async def _error():
Expand All @@ -74,6 +75,7 @@ async def _message():
capture_message("Hi")
return {"message": "Hi"}

@mounted_app.get("/nomessage")
@app.delete("/nomessage")
@app.get("/nomessage")
@app.head("/nomessage")
Expand Down Expand Up @@ -118,6 +120,8 @@ async def body_form(
capture_message("hi")
return {"status": "ok"}

app.mount("/root", mounted_app)

return app


Expand Down Expand Up @@ -1043,6 +1047,48 @@ def test_transaction_http_method_custom(sentry_init, capture_events):
assert event2["request"]["method"] == "HEAD"


@pytest.mark.parametrize("span_streaming", [True, False])
def test_request_url(sentry_init, capture_events, capture_items, span_streaming):
sentry_init(
traces_sample_rate=1.0,
send_default_pii=True,
integrations=[
StarletteIntegration(),
],
_experiments={
"trace_lifecycle": "stream" if span_streaming else "static",
},
)

starlette_app = fastapi_app_factory()

client = TestClient(starlette_app)

if span_streaming:
items = capture_items("span")

client.get("/root/nomessage")
sentry_sdk.flush()
spans = [item.payload for item in items]

(server_span,) = (
span
for span in spans
if span["attributes"].get("sentry.op") == "http.server"
)
assert server_span["attributes"][SPANDATA.URL_FULL] == (
"http://testserver/root/nomessage"
)
assert server_span["attributes"][SPANDATA.URL_PATH] == "/root/nomessage"
else:
events = capture_events()

client.get("/root/nomessage")

(event,) = events
assert event["request"]["url"] == "http://testserver/root/nomessage"


@parametrize_test_configurable_status_codes
def test_configurable_status_codes(
sentry_init,
Expand Down
Loading
Loading