Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Comments

docs: Add FastAPI + Uvicorn monitoring example#1103

Open
yuhao541 wants to merge 1 commit intoprometheus:master from
yuhao541:docs/add-fastapi-uvicorn-example
Open

docs: Add FastAPI + Uvicorn monitoring example #1103
yuhao541 wants to merge 1 commit intoprometheus:master from
yuhao541:docs/add-fastapi-uvicorn-example

Conversation

@yuhao541
Copy link

@yuhao541 yuhao541 commented Apr 19, 2025

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 <yuhao541@qq.com>
@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

@csmarchbanks csmarchbanks Apr 21, 2025

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

@csmarchbanks csmarchbanks Apr 21, 2025

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

@csmarchbanks csmarchbanks Apr 21, 2025

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

@csmarchbanks csmarchbanks Apr 21, 2025

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

@csmarchbanks csmarchbanks Apr 21, 2025

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

Reviewers

@csmarchbanks csmarchbanks csmarchbanks left review comments

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /