Skip to content

fix(fastapi): Fix FastAPI Instrumentation Compatibility with Middleware-Wrapped Apps#4041

Open
punitmahes wants to merge 3 commits intoopen-telemetry:mainfrom
punitmahes:fix/cors-middleware-error
Open

fix(fastapi): Fix FastAPI Instrumentation Compatibility with Middleware-Wrapped Apps#4041
punitmahes wants to merge 3 commits intoopen-telemetry:mainfrom
punitmahes:fix/cors-middleware-error

Conversation

@punitmahes
Copy link
Contributor

@punitmahes punitmahes commented Dec 17, 2025

Description

This pull request resolves issue #4031, which reported that FastAPI instrumentation did not work with middleware-wrapped applications such as CORSMiddleware. The changes here ensure that the FastAPIInstrumentor can properly identify and instrument FastAPI or Starlette instances, even if wrapped by middleware.

Fixes #4031

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Added test coverage for middleware-wrapped FastAPI apps.
  • Verified instrumentation works as expected with middleware layers.

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Dec 17, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: punitmahes / name: Punit Maheshwari (3556dc9)

@punitmahes punitmahes force-pushed the fix/cors-middleware-error branch from 3e478e3 to 9e3b02a Compare December 17, 2025 18:43
@punitmahes punitmahes requested a review from a team as a code owner December 17, 2025 18:43
@punitmahes punitmahes force-pushed the fix/cors-middleware-error branch 2 times, most recently from 23d5e85 to 9aa86ce Compare December 17, 2025 18:50
@punitmahes punitmahes force-pushed the fix/cors-middleware-error branch from 9aa86ce to a85bee6 Compare December 17, 2025 18:50
@xrmx xrmx moved this to Ready for review in @xrmx's Python PR digest Dec 18, 2025
@punitmahes
Copy link
Contributor Author

@xrmx Following up if you got chance to have a look.

@@ -409,6 +417,9 @@ async def __call__(

@staticmethod
def uninstrument_app(app: fastapi.FastAPI):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and in instrument_app you are also taking a generic object I guess, CorsMiddleware does not inherit from anything

Returns:
The unwrapped FastAPI or Starlette application.
"""
while hasattr(app, "app"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we break once app is fastapi.FastAPI?

> Use [this search for a list of all CHANGELOG.md files in this repo](https://github.com/search?q=repo%3Aopen-telemetry%2Fopentelemetry-python-contrib+path%3A**%2FCHANGELOG.md&type=code).

## Unreleased
- `opentelemetry-instrumentation-fastapi` Support for Middleware Wrapped FastAPI Application [#4041](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/4031)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go under Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Ready for review

Development

Successfully merging this pull request may close these issues.

Incompatible with CORSMiddleware app

2 participants