feat: add FastAPI middleware for per-request emissions tracking#1203
Open
davidberenstein1957 wants to merge 6 commits into
Open
feat: add FastAPI middleware for per-request emissions tracking#1203davidberenstein1957 wants to merge 6 commits into
davidberenstein1957 wants to merge 6 commits into
Conversation
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
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 ? |
f44740f to
15c0ccf
Compare
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. |
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.
eabd526 to
bb6995c
Compare
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
codecarbon[fastapi]integration withCodeCarbonMiddlewareto measure CO₂ emissions per HTTP request across all FastAPI/Starlette routesheader_formattercallback) so clients can read emissions, energy, duration, and more from response headerscreate_codecarbon_lifespanfor shared app-level tracking, route-based task naming, path exclusions, and two tracking modes (requestfor concurrency-safe per-request trackers,appfor lower-overhead serialized measurement)Test plan
uv run pytest tests/integrations/ -v— 17 tests passuv run --extra fastapi uvicorn examples.fastapi_middleware:app --reloadthencurl -i localhost:8000/predictuv run task docsUsage
See docs/how-to/fastapi.md for full configuration options.
Made with Cursor