Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

yuhao541
Copy link

Description

This PR adds documentation for monitoring FastAPI applications running with Uvicorn ASGI server.

- Demonstrate native ASGI integration
- Include multiprocess guidance

Signed-off-by: yuhao <[email protected]>
@yuhao541 yuhao541 force-pushed the docs/add-fastapi-uvicorn-example branch from 4dc211d to 695e0d8 Compare April 19, 2025 13:56
Copy link
Member

@csmarchbanks csmarchbanks left a 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Define metrics with multiprocess aggregation
# Define metrics with multiprocess aggregation

)
return make_asgi_app(registry=registry)

Mount endpoint with trailing slash
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Mount endpoint with trailing slash
# Mount endpoint with trailing slash

"http_requests_total",
"Total HTTP requests by endpoint",
["endpoint"],
registry=CollectorRegistry() # Isolated registry
Copy link
Member

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
Copy link
Member

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:
Copy link
Member

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.

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