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
20 changes: 11 additions & 9 deletions sentry_sdk/integrations/starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,21 +843,24 @@ async def json(self: "StarletteRequestExtractor") -> "Optional[Dict[str, Any]]":
return None


def _transaction_name_from_router(scope: "StarletteScope") -> "Optional[str]":
def _transaction_name_and_source_from_router(
scope: "StarletteScope",
) -> "Tuple[Optional[str], TransactionSource]":
router = scope.get("router")
if not router:
return None
return None, TransactionSource.ROUTE

for route in router.routes:
match = route.matches(scope)
if match[0] == Match.FULL:
try:
return route.path
return route.path, TransactionSource.ROUTE
except AttributeError:
# routes added via app.host() won't have a path attribute
return scope.get("path")
# Host routes have no path template, so fall back to the
# concrete request path and classify it as a URL.
return scope.get("path"), TransactionSource.URL

return None
return None, TransactionSource.ROUTE


def _set_transaction_name_and_source(
Expand All @@ -872,7 +875,7 @@ def _set_transaction_name_and_source(
name = transaction_from_function(endpoint) or None

elif transaction_style == "url":
name = _transaction_name_from_router(request.scope)
name, source = _transaction_name_and_source_from_router(request.scope)

if name is None:
name = _DEFAULT_TRANSACTION_NAME
Expand All @@ -891,7 +894,6 @@ def _get_transaction_from_middleware(
name = transaction_from_function(app.__class__)
source = TransactionSource.COMPONENT
elif integration.transaction_style == "url":
name = _transaction_name_from_router(asgi_scope)
source = TransactionSource.ROUTE
name, source = _transaction_name_and_source_from_router(asgi_scope)

return name, source
25 changes: 25 additions & 0 deletions tests/integrations/starlette/test_starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,31 @@ def test_transaction_style(
assert event["transaction_info"] == {"source": expected_source}


def test_host_route_path_has_url_source(sentry_init, capture_events):
sentry_init(
integrations=[StarletteIntegration(transaction_style="url")],
traces_sample_rate=1.0,
)

async def hosted_endpoint(request):
return starlette.responses.JSONResponse({"status": "ok"})

subapp = starlette.applications.Starlette(
routes=[starlette.routing.Route("/users/{user_id}", hosted_endpoint)]
)
app = starlette.applications.Starlette(
routes=[starlette.routing.Host("subapp", subapp)]
)

events = capture_events()
client = TestClient(app)
client.get("/users/123456", headers={"Host": "subapp"})

(event,) = events
assert event["transaction"].endswith("/users/123456")
assert event["transaction_info"] == {"source": "url"}


@pytest.mark.parametrize(
"test_url,expected_error,expected_message",
[
Expand Down
Loading