-
Notifications
You must be signed in to change notification settings - Fork 811
docs: Add FastAPI + Uvicorn monitoring example #1103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
docs: Add FastAPI + Uvicorn monitoring example #1103
Conversation
- Demonstrate native ASGI integration - Include multiprocess guidance Signed-off-by: yuhao <[email protected]>
4dc211d
to
695e0d8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.