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

Conversation

lannuttia
Copy link

@lannuttia lannuttia commented Aug 5, 2025

Description

When an APIRoute 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.

Fixes #3671

Type of change

Please delete options that are not relevant.

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

How Has This Been Tested?

This has been validated with automated tests that are included in this pull request.

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

Copy link

linux-foundation-easycla bot commented Aug 5, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@lannuttia lannuttia marked this pull request as ready for review August 5, 2025 22:55
@lannuttia lannuttia changed the title Fix FastAPI issue with APIRouter subclasses Fix FastAPI issue with APIRoute subclasses Aug 5, 2025
Fixes: open-telemetry#3671

When an APIRoute 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.
This commit adds tests that illustrate the original issue that was being
experienced for custom api route implementations when they depended on
non-standard fields existing on the ASGI HTTP connection scope. Before
the fix was implemented, the inclusion of a custom API route in the
FastAPI application would cause an exception to be raised inside the
OpenTelemetryMiddleware since the non-standard fields do not exist on
the ASGI HTTP connection scope until after the subsequent middleware
runs and adds the expected fields.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FastAPI Instrumentation Bypasses Middleware for Custom Router
1 participant