Skip to content

Commit 40f57aa

Browse files
committed
starlette/fastapi: fix error on host-based routing
Fix #3506
1 parent ccdf522 commit 40f57aa

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,11 @@ def _get_route_details(scope):
428428
for starlette_route in app.routes:
429429
match, _ = starlette_route.matches(scope)
430430
if match == Match.FULL:
431-
route = starlette_route.path
432-
break
431+
try:
432+
route = starlette_route.path
433+
except AttributeError:
434+
# routes added via host routing won't have a path attribute
435+
route = scope.get("path")
433436
if match == Match.PARTIAL:
434437
route = starlette_route.path
435438
return route

instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ async def _():
207207
return {"message": "ok"}
208208

209209
app.mount("/sub", app=sub_app)
210+
app.host("testserver2", sub_app)
210211

211212
return app
212213

@@ -263,6 +264,10 @@ def test_sub_app_fastapi_call(self):
263264
span.attributes[SpanAttributes.HTTP_URL],
264265
)
265266

267+
def test_host_fastapi_call(self):
268+
client = TestClient(self._app, base_url="https://testserver2")
269+
client.get("/")
270+
266271

267272
class TestBaseAutoFastAPI(TestBaseFastAPI):
268273
@classmethod

instrumentation/opentelemetry-instrumentation-starlette/src/opentelemetry/instrumentation/starlette/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,11 @@ def _get_route_details(scope: dict[str, Any]) -> str | None:
355355
for starlette_route in app.routes:
356356
match, _ = starlette_route.matches(scope)
357357
if match == Match.FULL:
358-
route = starlette_route.path
358+
try:
359+
route = starlette_route.path
360+
except AttributeError:
361+
# routes added via host routing won't have a path attribute
362+
route = scope.get("path")
359363
break
360364
if match == Match.PARTIAL:
361365
route = starlette_route.path

instrumentation/opentelemetry-instrumentation-starlette/tests/test_starlette_instrumentation.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from starlette import applications
2020
from starlette.responses import PlainTextResponse
21-
from starlette.routing import Mount, Route
21+
from starlette.routing import Host, Mount, Route
2222
from starlette.testclient import TestClient
2323
from starlette.websockets import WebSocket
2424

@@ -140,6 +140,10 @@ def test_sub_app_starlette_call(self):
140140
span.attributes[SpanAttributes.HTTP_URL],
141141
)
142142

143+
def test_host_starlette_call(self):
144+
client = TestClient(self._app, base_url="http://testserver2")
145+
client.get("/")
146+
143147
def test_starlette_route_attribute_added(self):
144148
"""Ensure that starlette routes are used as the span name."""
145149
self._client.get("/user/123")
@@ -298,6 +302,7 @@ def sub_home(_):
298302
Route("/user/{username}", home),
299303
Route("/healthzz", health),
300304
Mount("/sub", app=sub_app),
305+
Host("testserver2", sub_app),
301306
],
302307
)
303308

0 commit comments

Comments
 (0)