fix(starlette): handle FastAPI 0.137 _IncludedRouter in route name resolution#2682
Open
HosseinNejatiJavaremi wants to merge 1 commit into
Open
Conversation
…solution FastAPI 0.137 nests include_router() routes under _IncludedRouter objects that lack a .path attribute, causing AttributeError on every HTTP request when ElasticAPM middleware resolves the transaction name. Flatten included-router trees via iter_route_contexts() (FastAPI >= 0.137.2) or effective_route_contexts() fallback, and guard partial-match path access. Fixes elastic#2681 Co-authored-by: Cursor <cursoragent@cursor.com>
|
💚 CLA has been signed |
🤖 GitHub commentsJust comment with:
|
Author
|
run |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2681
FastAPI 0.137.0 changed
app.routesto include_IncludedRouterwrapper objects for routes added viainclude_router(). These wrappers do not expose a.pathattribute, butElasticAPM._get_route_name()accessedroute.pathunconditionally on full and partial matches, causingAttributeErrorand 500 responses on every HTTP request.This PR:
_flatten_routes()using FastAPI's publiciter_route_contexts()(>= 0.137.2) witheffective_route_contexts()fallback for 0.137.0/0.137.1_IncludedRouternodes are present, preserving existing StarletteMountbehavior.pathaccess withgetattr()fastapi>=0.137.2inreqs-starlette-newest.txtPrior art: open-telemetry/opentelemetry-python-contrib#4700
Reproduction (before fix)
fastapi>=0.137andelastic-apmapp.include_router(...)app.add_middleware(ElasticAPM, client=apm_client)AttributeError: '_IncludedRouter' object has no attribute 'path'Test plan
pytest tests/contrib/asyncio/starlette_tests.py tests/contrib/asyncio/fastapi_tests.py -m starlette— 30 passed locallyMade with Cursor