Skip to content

Commit 8031369

Browse files
committed
fix fastapi tests failure with >= 0.118.0
Signed-off-by: emdneto <9735060+emdneto@users.noreply.github.com>
1 parent f189a1f commit 8031369

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

instrumentation/opentelemetry-instrumentation-fastapi/test-requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
annotated-types==0.7.0
2-
anyio==4.3.0
2+
anyio==4.12.1
33
asgiref==3.8.1
44
certifi==2024.7.4
55
charset-normalizer==3.3.2
66
Deprecated==1.2.14
77
exceptiongroup==1.2.0
8-
fastapi==0.117.0
8+
fastapi==0.126.0
99
h11==0.16.0
1010
httpcore==1.0.9
1111
httpx==0.28.1
1212
idna==3.7
1313
iniconfig==2.0.0
1414
packaging==24.0
15-
pluggy==1.5.0
15+
pluggy==1.6.0
1616
py-cpuinfo==9.0.0
1717
pydantic==2.12.5
1818
pydantic_core==2.41.5

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import fastapi
2727
import pytest
28+
from fastapi.middleware.asyncexitstack import AsyncExitStackMiddleware
2829
from fastapi.middleware.httpsredirect import HTTPSRedirectMiddleware
2930
from fastapi.responses import JSONResponse, PlainTextResponse
3031
from fastapi.routing import APIRoute
@@ -2073,7 +2074,12 @@ def _create_fastapi_app():
20732074
app = TestBaseFastAPI._create_fastapi_app()
20742075

20752076
def build_middleware_stack():
2076-
return app.router
2077+
# Return something that is NOT a ServerErrorMiddleware so the
2078+
# instrumentation fallback path triggers, but still wrap the
2079+
# router with AsyncExitStackMiddleware so that newer FastAPI
2080+
# versions (which assert ``fastapi_middleware_astack`` exists in
2081+
# the request scope) can service requests normally.
2082+
return AsyncExitStackMiddleware(app.router)
20772083

20782084
app.build_middleware_stack = build_middleware_stack
20792085
return app
@@ -2098,7 +2104,7 @@ def test_no_instrumentation(self):
20982104
self.assertEqual(len(errors), 1)
20992105
self.assertEqual(
21002106
errors[0].getMessage(),
2101-
"Skipping FastAPI instrumentation due to unexpected middleware stack: expected ServerErrorMiddleware, got <class 'fastapi.routing.APIRouter'>",
2107+
"Skipping FastAPI instrumentation due to unexpected middleware stack: expected ServerErrorMiddleware, got <class 'fastapi.middleware.asyncexitstack.AsyncExitStackMiddleware'>",
21022108
)
21032109

21042110

0 commit comments

Comments
 (0)