Skip to content

Starlette/FastAPI: host-based routing raises AttributeError #3506

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
frankie567 opened this issue May 16, 2025 · 0 comments · May be fixed by #3507
Open

Starlette/FastAPI: host-based routing raises AttributeError #3506

frankie567 opened this issue May 16, 2025 · 0 comments · May be fixed by #3507
Labels
bug Something isn't working

Comments

@frankie567
Copy link

Describe your environment

OS: macOS
Python version: 3.12.7
Package version: v0.51b0

What happened?

When instrumenting a FastAPI/Starlette app which uses Host-based routing (ref: https://www.starlette.io/routing/#host-based-routing), the instrument fails with the following error:

  File "/Users/fvoron/Development/polar/server/.venv/lib/python3.12/site-packages/opentelemetry/instrumentation/fastapi/__init__.py", line 427, in _get_route_details
    route = starlette_route.path
            ^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Host' object has no attribute 'path'

Steps to Reproduce

Create the following Starlette app:

# app.py
from opentelemetry.instrumentation.starlette import StarletteInstrumentor
from starlette import applications
from starlette.routing import Host, Router

app = applications.Starlette(
    routes=[
        Host("127.0.0.1", Router()),
    ]
)
StarletteInstrumentor.instrument_app(app)

Run it:

uvicorn app:app

Make an HTTP request:

curl http://127.0.0.1:8000

Expected Result

The request should be processed without error.

Actual Result

The request triggers an exception.

Additional context

A similar issue was reported and recently fixed on Sentry: getsentry/sentry-python#2631

The fix involved to fallback to the scope path when the path attribute was not directly available on the route object: https://github.com/getsentry/sentry-python/pull/4157/files

Would you like to implement a fix?

Yes

@frankie567 frankie567 added the bug Something isn't working label May 16, 2025
frankie567 added a commit to frankie567/opentelemetry-python-contrib that referenced this issue May 16, 2025
@frankie567 frankie567 linked a pull request May 16, 2025 that will close this issue
6 tasks
frankie567 added a commit to frankie567/opentelemetry-python-contrib that referenced this issue May 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant