Skip to content

starlette/fastapi: fix error on host-based routing #3507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `opentelemetry-instrumentation-botocore` Use `cloud.region` instead of `aws.region` span attribute as per semantic conventions.
([#3474](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3474))

### Fixed

- `opentelemetry-instrumentation-starlette` Fixes a crash when host-based routing is used ([#3507](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3507/))
- `opentelemetry-instrumentation-fastapi` Fixes a crash when host-based routing is used ([#3507](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3507/))

## Version 1.33.0/0.54b0 (2025-05-09)

Expand Down Expand Up @@ -44,7 +48,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `opentelemetry-instrumentation-botocore` Capture server attributes for botocore API calls
([#3448](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3448))


## Version 1.32.0/0.53b0 (2025-04-10)

### Added
Expand All @@ -70,7 +73,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `opentelemetry-instrumentation-aiokafka` Fix send_and_wait method no headers kwargs error.
([[#3332](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3332)])


## Version 1.31.0/0.52b0 (2025-03-12)

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,11 @@ def _get_route_details(scope):
for starlette_route in app.routes:
match, _ = starlette_route.matches(scope)
if match == Match.FULL:
route = starlette_route.path
try:
route = starlette_route.path
except AttributeError:
# routes added via host routing won't have a path attribute
route = scope.get("path")
break
if match == Match.PARTIAL:
route = starlette_route.path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ async def _():
return {"message": "ok"}

app.mount("/sub", app=sub_app)
app.host("testserver2", sub_app)

return app

Expand Down Expand Up @@ -263,6 +264,10 @@ def test_sub_app_fastapi_call(self):
span.attributes[SpanAttributes.HTTP_URL],
)

def test_host_fastapi_call(self):
client = TestClient(self._app, base_url="https://testserver2")
client.get("/")


class TestBaseAutoFastAPI(TestBaseFastAPI):
@classmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,11 @@ def _get_route_details(scope: dict[str, Any]) -> str | None:
for starlette_route in app.routes:
match, _ = starlette_route.matches(scope)
if match == Match.FULL:
route = starlette_route.path
try:
route = starlette_route.path
except AttributeError:
# routes added via host routing won't have a path attribute
route = scope.get("path")
break
if match == Match.PARTIAL:
route = starlette_route.path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from starlette import applications
from starlette.responses import PlainTextResponse
from starlette.routing import Mount, Route
from starlette.routing import Host, Mount, Route
from starlette.testclient import TestClient
from starlette.websockets import WebSocket

Expand Down Expand Up @@ -140,6 +140,10 @@ def test_sub_app_starlette_call(self):
span.attributes[SpanAttributes.HTTP_URL],
)

def test_host_starlette_call(self):
client = TestClient(self._app, base_url="http://testserver2")
client.get("/")

def test_starlette_route_attribute_added(self):
"""Ensure that starlette routes are used as the span name."""
self._client.get("/user/123")
Expand Down Expand Up @@ -298,6 +302,7 @@ def sub_home(_):
Route("/user/{username}", home),
Route("/healthzz", health),
Mount("/sub", app=sub_app),
Host("testserver2", sub_app),
],
)

Expand Down