Skip to content

Fix FastAPI issue with APIRoute subclasses #3681

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 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
import fastapi
from starlette.applications import Starlette
from starlette.middleware.errors import ServerErrorMiddleware
from starlette.routing import Match
from starlette.routing import Match, Route
from starlette.types import ASGIApp

from opentelemetry.instrumentation._semconv import (
Expand Down Expand Up @@ -448,7 +448,11 @@ def _get_route_details(scope):
route = None

for starlette_route in app.routes:
match, _ = starlette_route.matches(scope)
match, _ = (
Route.matches(starlette_route, scope)
if isinstance(starlette_route, Route)
else starlette_route.matches(scope)
)
if match == Match.FULL:
route = starlette_route.path
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import fastapi
from fastapi.middleware.httpsredirect import HTTPSRedirectMiddleware
from fastapi.responses import JSONResponse
from fastapi.routing import APIRoute
from fastapi.testclient import TestClient
from starlette.routing import Match
from starlette.types import Receive, Scope, Send

import opentelemetry.instrumentation.fastapi as otel_fastapi
from opentelemetry import trace
Expand All @@ -38,9 +41,7 @@
from opentelemetry.instrumentation.auto_instrumentation._load import (
_load_instrumentors,
)
from opentelemetry.instrumentation.dependencies import (
DependencyConflict,
)
from opentelemetry.instrumentation.dependencies import DependencyConflict
from opentelemetry.sdk.metrics.export import (
HistogramDataPoint,
NumberDataPoint,
Expand Down Expand Up @@ -123,6 +124,23 @@
)


class CustomMiddleware:
def __init__(self, app: fastapi.FastAPI) -> None:
self.app = app

async def __call__(
self, scope: Scope, receive: Receive, send: Send
) -> None:
scope["nonstandard_field"] = "here"
await self.app(scope, receive, send)


class CustomRoute(APIRoute):
def matches(self, scope: Scope) -> tuple[Match, Scope]:
assert "nonstandard_field" in scope
return super().matches(scope)


class TestBaseFastAPI(TestBase):
def _create_app(self):
app = self._create_fastapi_app()
Expand Down Expand Up @@ -183,6 +201,7 @@ def setUp(self):
self._instrumentor = otel_fastapi.FastAPIInstrumentor()
self._app = self._create_app()
self._app.add_middleware(HTTPSRedirectMiddleware)
self._app.add_middleware(CustomMiddleware)
self._client = TestClient(self._app, base_url="https://testserver:443")
# run the lifespan, initialize the middleware stack
# this is more in-line with what happens in a real application when the server starts up
Expand All @@ -202,6 +221,7 @@ def tearDown(self):
def _create_fastapi_app():
app = fastapi.FastAPI()
sub_app = fastapi.FastAPI()
custom_router = fastapi.APIRouter(route_class=CustomRoute)

@sub_app.get("/home")
async def _():
Expand All @@ -227,6 +247,12 @@ async def _():
async def _():
raise UnhandledException("This is an unhandled exception")

@custom_router.get("/success")
async def _():
return None

app.include_router(custom_router, prefix="/custom-router")

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

return app
Expand Down Expand Up @@ -304,6 +330,14 @@ def test_sub_app_fastapi_call(self):
span.attributes[HTTP_URL],
)

def test_custom_api_router(self):
"""
This test is to ensure that custom API routers the OpenTelemetryMiddleware does not cause issues with
custom API routers that depend on non-standard fields on the ASGI scope.
"""
resp = self._client.get("/custom-router/success")
self.assertEqual(resp.status_code, 200)


class TestBaseAutoFastAPI(TestBaseFastAPI):
@classmethod
Expand Down Expand Up @@ -988,6 +1022,7 @@ def test_metric_uninstrument(self):
def _create_fastapi_app():
app = fastapi.FastAPI()
sub_app = fastapi.FastAPI()
custom_router = fastapi.APIRouter(route_class=CustomRoute)

@sub_app.get("/home")
async def _():
Expand All @@ -1013,6 +1048,12 @@ async def _():
async def _():
raise UnhandledException("This is an unhandled exception")

@custom_router.get("/success")
async def _():
return None

app.include_router(custom_router, prefix="/custom-router")

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

return app
Expand Down