Skip to content

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

Open
@frankie567

Description

@frankie567

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions