Skip to content

feat: add FastAPI middleware for per-request emissions tracking#1203

Open
davidberenstein1957 wants to merge 6 commits into
masterfrom
feat/add-fastapi-middleware
Open

feat: add FastAPI middleware for per-request emissions tracking#1203
davidberenstein1957 wants to merge 6 commits into
masterfrom
feat/add-fastapi-middleware

Conversation

@davidberenstein1957
Copy link
Copy Markdown
Collaborator

Summary

  • Adds optional codecarbon[fastapi] integration with CodeCarbonMiddleware to measure CO₂ emissions per HTTP request across all FastAPI/Starlette routes
  • Supports configurable response headers (presets, field lists, custom maps, or header_formatter callback) so clients can read emissions, energy, duration, and more from response headers
  • Includes create_codecarbon_lifespan for shared app-level tracking, route-based task naming, path exclusions, and two tracking modes (request for concurrency-safe per-request trackers, app for lower-overhead serialized measurement)

Test plan

  • uv run pytest tests/integrations/ -v — 17 tests pass
  • Manual smoke: uv run --extra fastapi uvicorn examples.fastapi_middleware:app --reload then curl -i localhost:8000/predict
  • Verify docs build: uv run task docs

Usage

from fastapi import FastAPI
from codecarbon.integrations.fastapi import add_codecarbon_middleware

app = FastAPI()
add_codecarbon_middleware(app, project_name="my-api", response_headers="default")

See docs/how-to/fastapi.md for full configuration options.

Made with Cursor

@codecov
Copy link
Copy Markdown

codecov Bot commented May 19, 2026

Codecov Report

❌ Patch coverage is 94.68599% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.17%. Comparing base (d2ce064) to head (bb6995c).

Files with missing lines Patch % Lines
codecarbon/integrations/fastapi/middleware.py 91.89% 9 Missing ⚠️
codecarbon/integrations/fastapi/_routing.py 95.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1203      +/-   ##
==========================================
+ Coverage   88.88%   89.17%   +0.28%     
==========================================
  Files          45       50       +5     
  Lines        4302     4509     +207     
==========================================
+ Hits         3824     4021     +197     
- Misses        478      488      +10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@benoit-cty
Copy link
Copy Markdown
Contributor

Hello, thanks for this. There is a problem with your branch : there are many changes that are already merged. Can you do a rebase ?

@davidberenstein1957 davidberenstein1957 force-pushed the feat/add-fastapi-middleware branch from f44740f to 15c0ccf Compare May 20, 2026 09:31
@davidberenstein1957 davidberenstein1957 marked this pull request as ready for review May 20, 2026 09:33
@davidberenstein1957 davidberenstein1957 requested a review from a team as a code owner May 20, 2026 09:33
@davidberenstein1957
Copy link
Copy Markdown
Collaborator Author

@benoit-cty I have reached out to some people at FastAPI, if they would be interested in a quick review :) For visibility, we could also consider deploying it as a standalone integration, but let's see if people like it.

davidberenstein1957 and others added 6 commits May 20, 2026 17:59
Ship optional codecarbon[fastapi] integration with CodeCarbonMiddleware,
configurable response headers, route-based task naming, and lifespan helper
for shared app-level tracking.

Co-authored-by: Cursor <cursoragent@cursor.com>
Add tests for app tracking mode, deprecated include_emissions_header,
on_request_complete callbacks, header preset edge cases, and routing
formatters so Codecov patch coverage meets the PR threshold.

Co-authored-by: Cursor <cursoragent@cursor.com>
…iltering

Update the FastAPI middleware to support include and exclude patterns for request tracking, allowing users to specify which endpoints to measure. Refactor routing helpers for improved clarity and add support for deferred measurement. Update documentation to reflect new features and usage examples.

Co-authored-by: Cursor <cursoragent@cursor.com>
Refactor FastAPI middleware and routing code for improved readability by adjusting line breaks and indentation. Remove the product telemetry link from the documentation navigation. Update test cases for consistency in formatting and structure.
Refactor the test for FastAPI middleware to handle deferred task execution using a thread pool. This change enhances the test's reliability by ensuring that asynchronous tasks are properly awaited in a separate thread, improving overall test coverage and stability.
Refactor the exception handling in gpu_amd.py to specifically catch AttributeError when importing amdsmi. Update the warning message to provide clearer guidance on ensuring proper configuration of amdsmi for AMD GPU metrics.
@davidberenstein1957 davidberenstein1957 force-pushed the feat/add-fastapi-middleware branch 2 times, most recently from eabd526 to bb6995c Compare May 20, 2026 16:03
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.

2 participants