Skip to content

Commit de946d3

Browse files
committed
fix(fastapi): issue with APIRouter subclasses
Fixes: #3671 When an APIRouter subclass would overwrite the matches method with an implementation that depended on non-standard fields existing on the HTTP connection scope, this would cause a failure when the OpenTelemetryMiddleware tried to get the default span details for the incoming request. This has been fixed by using the matches implementation on the Route class for any subclass of Route. This should be sufficient since the only information we are trying to get from that method is the path for the request.
1 parent 74536f1 commit de946d3

File tree

1 file changed

+6
-2
lines changed
  • instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi

1 file changed

+6
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
190190
import fastapi
191191
from starlette.applications import Starlette
192192
from starlette.middleware.errors import ServerErrorMiddleware
193-
from starlette.routing import Match
193+
from starlette.routing import Match, Route
194194
from starlette.types import ASGIApp
195195

196196
from opentelemetry.instrumentation._semconv import (
@@ -448,7 +448,11 @@ def _get_route_details(scope):
448448
route = None
449449

450450
for starlette_route in app.routes:
451-
match, _ = starlette_route.matches(scope)
451+
match, _ = (
452+
Route.matches(starlette_route, scope)
453+
if isinstance(starlette_route, Route)
454+
else starlette_route.matches(scope)
455+
)
452456
if match == Match.FULL:
453457
route = starlette_route.path
454458
break

0 commit comments

Comments
 (0)