This library provides a straightforward way to instrument your FastAPI or Starlette applications with Prometheus metrics. It automatically exposes common HTTP request metrics and allows for custom metric definitions. Currently at version 7.1.0, it maintains an active development pace with frequent updates and bug fixes, typically releasing minor versions every few months and major versions annually.
pip install prometheus-fastapi-instrumentator prometheus_client uvicornVerified import paths — ran on the pinned version, not inferred.
This quickstart initializes a FastAPI app, instruments it with default Prometheus metrics on startup, and exposes a `/metrics` endpoint. Run `python your_app.py` and navigate to `http://localhost:8000/metrics` to see the metrics. It also includes comments on important considerations for multiprocessing.
Upgrade your Python environment to 3.8 or newer.
If absolutely necessary, re-enable this feature by setting `enable_body_in_metrics=True` in the `Instrumentator` constructor (e.g., `Instrumentator(enable_body_in_metrics=True)`). Exercise caution when re-enabling.
Use the uppercase `PROMETHEUS_MULTIPROC_DIR` environment variable for configuring multiprocessing support. The lowercase version might eventually be removed.
Wrap your `Instrumentator().instrument(app).expose(app)` call inside an `async def startup_event():` function decorated with `@app.on_event("startup")`.Before starting your application, set the `PROMETHEUS_MULTIPROC_DIR` environment variable (e.g., `export PROMETHEUS_MULTIPROC_DIR=/tmp/prom_data`). Ensure this directory exists and is writable by all worker processes.