Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(metrics): support FastAPI >=0.137 (bump prometheus instrumentator to >=8.0.2)#440

Open
ibbybuilds wants to merge 1 commit into
mainfrom
fix/prometheus-fastapi-0137
Open

fix(metrics): support FastAPI >=0.137 (bump prometheus instrumentator to >=8.0.2)#440
ibbybuilds wants to merge 1 commit into
mainfrom
fix/prometheus-fastapi-0137

Conversation

@ibbybuilds

@ibbybuilds ibbybuilds commented Jun 28, 2026

Copy link
Copy Markdown
Member

Problem

With ENABLE_PROMETHEUS_METRICS=true and FastAPI >=0.137, every request returns HTTP 500.

FastAPI 0.137 (fastapi/fastapi#15745) changed app.routes to hold _IncludedRouter tree nodes that have no .path. prometheus-fastapi-instrumentator 7.x walks app.routes and reads route.path on a match:

File ".../prometheus_fastapi_instrumentator/routing.py", line 55, in _get_route_name
    route_name = route.path
AttributeError: '_IncludedRouter' object has no attribute 'path'

Aegra mounts all its routers via include_router, so an _IncludedRouter is always present, and the instrumentator middleware raises on every request. The same FastAPI change broke opentelemetry-instrumentation-fastapi (open-telemetry/opentelemetry-python-contrib#4699).

Reported against aegra-api==0.9.21 + FastAPI 0.137.0.

Fix

Bump the floor: prometheus-fastapi-instrumentator>=7.1.0>=8.0.2. 8.0.2 handles the new route tree.

Verification

Reproduced the exact AttributeError 500 with 7.1.0 + FastAPI 0.137 on an include_router app, confirmed 8.0.2 returns 200 on both FastAPI 0.138.1 (the broken range) and the older FastAPI we previously resolved. Added an integration regression that drives a templated include_router route under metrics (the prior test used a direct @app.get, which never produces an _IncludedRouter and so could not catch this). Full unit + integration suite green on the bumped resolve (FastAPI 0.138.1 / starlette 1.3.1).

Scope

Confirmed metrics is the only thing affected. The other two places Aegra touches app.routes are safe on 0.137: the custom-route auth walker type-checks (isinstance APIRoute / recurses on .routes) and the root-route check already guards with hasattr(route, "path").

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved Prometheus metrics compatibility with newer FastAPI routing behavior, preventing 500 errors when metrics are enabled on routed endpoints.
    • Updated the metrics dependency to a newer version that handles the current router structure more reliably.
  • Tests

    • Added an integration test covering a routed endpoint with metrics enabled to verify successful responses and guard against regressions.

Greptile Summary

This PR updates the Prometheus metrics dependency for newer FastAPI route trees. The main changes are:

  • Raises prometheus-fastapi-instrumentator to >=8.0.2.
  • Refreshes the lockfile to FastAPI 0.138.1 and Starlette 1.3.1.
  • Adds an integration test for metrics on an include_router route.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.

Important Files Changed

Filename Overview
libs/aegra-api/pyproject.toml Updates the Prometheus instrumentator lower bound and documents the FastAPI route-tree compatibility reason.
libs/aegra-api/tests/integration/test_prometheus_metrics.py Adds an isolated regression test for an instrumented route mounted through include_router.
uv.lock Refreshes locked versions for the metrics and FastAPI/Starlette stack.

Reviews (1): Last reviewed commit: "fix(metrics): bump prometheus-fastapi-in..." | Re-trigger Greptile

Context used:

  • Context used - CLAUDE.md (source)
  • Context used - AGENTS.md (source)

…astAPI >=0.137

FastAPI 0.137 changed app.routes to hold _IncludedRouter nodes that have no
.path. The instrumentator 7.x walks app.routes and reads route.path on a match,
raising AttributeError on every request when metrics are enabled. Aegra mounts
all routers via include_router, so this 500'd every request under metrics on
FastAPI >=0.137. 8.0.2 handles the new route tree and stays compatible with the
older FastAPI we already resolve.

Adds an integration regression covering an include_router route under metrics
(the prior test used a direct @app.get, which never produced an _IncludedRouter
and so could not catch this).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 28, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The prometheus-fastapi-instrumentator minimum version is bumped from >=7.1.0 to >=8.0.2 in pyproject.toml, with inline comments explaining the FastAPI router change that caused 500 errors. A new regression integration test verifies that a Prometheus-instrumented app with an APIRouter templated route returns HTTP 200.

Changes

Prometheus instrumentator bump and regression test

Layer / File(s) Summary
Dependency bump and APIRouter regression test
libs/aegra-api/pyproject.toml, libs/aegra-api/tests/integration/test_prometheus_metrics.py
Version constraint updated to >=8.0.2 with comments explaining the route.path access issue. APIRouter imported and a new test asserts /items/42 returns HTTP 200 with metrics enabled.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~4 minutes

Poem

🐇 A version bump, a test so bright,
No more 500s in the night.
The router's path now found with ease,
Prometheus metrics, if you please!
Hopping forward, all is right. 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: fixing FastAPI 0.137 metrics support by bumping the Prometheus instrumentator.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/prometheus-fastapi-0137

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Jun 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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.

1 participant