docs: Add FastAPI + Uvicorn monitoring example#1103
docs: Add FastAPI + Uvicorn monitoring example#1103yuhao541 wants to merge 1 commit intoprometheus:masterfrom
Conversation
- Demonstrate native ASGI integration - Include multiprocess guidance Signed-off-by: yuhao <yuhao541@qq.com>
4dc211d to
695e0d8
Compare
csmarchbanks
left a comment
There was a problem hiding this comment.
Some comments on this approach, I do wonder if we should combine this with the existing Gunicorn page? Start with native, then have an additional section for gunicorn rather than two separate pages?
|
|
||
| app = FastAPI() | ||
|
|
||
| Define metrics with multiprocess aggregation |
There was a problem hiding this comment.
| Define metrics with multiprocess aggregation | |
| # Define metrics with multiprocess aggregation |
| ) | ||
| return make_asgi_app(registry=registry) | ||
|
|
||
| Mount endpoint with trailing slash |
There was a problem hiding this comment.
| Mount endpoint with trailing slash | |
| # Mount endpoint with trailing slash |
| "http_requests_total", | ||
| "Total HTTP requests by endpoint", | ||
| ["endpoint"], | ||
| registry=CollectorRegistry() # Isolated registry |
There was a problem hiding this comment.
Typically we recommend not assigning any registry when using multiprocess. What about just leaving a comment here like # do not assign registry= when using multiprocess mode instead of assigning?
| CPU_USAGE = Gauge( | ||
| "system_cpu_usage_percent", | ||
| "Current CPU utilization percentage (aggregated)", | ||
| multiprocess_mode='livesum' # Critical for worker aggregation |
There was a problem hiding this comment.
Not critical for an example, but livemostrecent is probably the best here to avoid counting the CPU used multiple times.
| ``` | ||
| 2. **Storage Management** | ||
| - Use dedicated volume for `PROMETHEUS_MULTIPROC_DIR` | ||
| - Implement cleanup cron job: |
There was a problem hiding this comment.
Rather than a cron-job it is usually best practice to delete the data (if it exists) when your service restarts. Otherwise some problems with counter resets can occur.
Description
This PR adds documentation for monitoring FastAPI applications running with Uvicorn ASGI server.