From d7a3b18760821854bb3dd3affb938758ab560cf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Em=C3=ADdio=20Neto?= <9735060+emdneto@users.noreply.github.com> Date: Fri, 6 Mar 2026 07:48:51 -0300 Subject: [PATCH] fix fastapi tests failure with >= 0.118.0 (#4277) Signed-off-by: emdneto <9735060+emdneto@users.noreply.github.com> Co-authored-by: Riccardo Magliocchetti --- .../test-requirements.txt | 6 +++--- .../tests/test_fastapi_instrumentation.py | 10 ++++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-fastapi/test-requirements.txt b/instrumentation/opentelemetry-instrumentation-fastapi/test-requirements.txt index 6c67a9f7c..09e4a15c5 100644 --- a/instrumentation/opentelemetry-instrumentation-fastapi/test-requirements.txt +++ b/instrumentation/opentelemetry-instrumentation-fastapi/test-requirements.txt @@ -1,18 +1,18 @@ annotated-types==0.7.0 -anyio==4.3.0 +anyio==4.12.1 asgiref==3.8.1 certifi==2024.7.4 charset-normalizer==3.3.2 Deprecated==1.2.14 exceptiongroup==1.2.0 -fastapi==0.117.0 +fastapi==0.126.0 h11==0.16.0 httpcore==1.0.9 httpx==0.28.1 idna==3.7 iniconfig==2.0.0 packaging==24.0 -pluggy==1.5.0 +pluggy==1.6.0 py-cpuinfo==9.0.0 pydantic==2.12.5 pydantic_core==2.41.5 diff --git a/instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py b/instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py index 21275366e..a2a8fb477 100644 --- a/instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py +++ b/instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py @@ -25,6 +25,7 @@ from unittest.mock import Mock, call, patch import fastapi import pytest +from fastapi.middleware.asyncexitstack import AsyncExitStackMiddleware from fastapi.middleware.httpsredirect import HTTPSRedirectMiddleware from fastapi.responses import JSONResponse, PlainTextResponse from fastapi.routing import APIRoute @@ -2073,7 +2074,12 @@ class TestFastAPIFallback(TestBaseFastAPI): app = TestBaseFastAPI._create_fastapi_app() def build_middleware_stack(): - return app.router + # Return something that is NOT a ServerErrorMiddleware so the + # instrumentation fallback path triggers, but still wrap the + # router with AsyncExitStackMiddleware so that newer FastAPI + # versions (which assert ``fastapi_middleware_astack`` exists in + # the request scope) can service requests normally. + return AsyncExitStackMiddleware(app.router) app.build_middleware_stack = build_middleware_stack return app @@ -2098,7 +2104,7 @@ class TestFastAPIFallback(TestBaseFastAPI): self.assertEqual(len(errors), 1) self.assertEqual( errors[0].getMessage(), - "Skipping FastAPI instrumentation due to unexpected middleware stack: expected ServerErrorMiddleware, got ", + "Skipping FastAPI instrumentation due to unexpected middleware stack: expected ServerErrorMiddleware, got ", )