{"id":1636,"library":"prometheus-fastapi-instrumentator","title":"Prometheus FastAPI Instrumentator","description":"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.","status":"active","version":"7.1.0","language":"en","source_language":"en","source_url":"https://github.com/trallnag/prometheus-fastapi-instrumentator","tags":["fastapi","prometheus","metrics","instrumentation","starlette","monitoring"],"install":[{"cmd":"pip install prometheus-fastapi-instrumentator prometheus_client uvicorn","lang":"bash","label":"Install core library and Prometheus client (add uvicorn for quickstart)"}],"dependencies":[{"reason":"Primary framework for instrumentation, though it also supports Starlette.","package":"fastapi","optional":false},{"reason":"Core library for creating and exposing Prometheus metrics.","package":"prometheus_client","optional":false},{"reason":"Common ASGI server used for running FastAPI applications, useful for quickstarts.","package":"uvicorn","optional":true}],"imports":[{"symbol":"Instrumentator","correct":"from prometheus_fastapi_instrumentator import Instrumentator"}],"quickstart":{"code":"from fastapi import FastAPI\nfrom prometheus_fastapi_instrumentator import Instrumentator\nimport uvicorn\nimport os\n\napp = FastAPI()\n\n# Best practice: Instrument the app on startup\n@app.on_event(\"startup\")\nasync def startup_event():\n    Instrumentator().instrument(app).expose(app)\n    print(\"Prometheus metrics exposed at /metrics\")\n\n@app.get(\"/\")\nasync def read_root():\n    return {\"message\": \"Hello Prometheus!\"}\n\n@app.get(\"/items/{item_id}\")\nasync def read_item(item_id: int):\n    return {\"item_id\": item_id, \"message\": \"An item\"}\n\nif __name__ == \"__main__\":\n    # For multiprocessing environments (e.g., Gunicorn with multiple workers),\n    # PROMETHEUS_MULTIPROC_DIR must be set to a shared writable directory.\n    # Example: os.environ[\"PROMETHEUS_MULTIPROC_DIR\"] = \"/tmp/prom_data\"\n    uvicorn.run(app, host=\"0.0.0.0\", port=8000)","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade your Python environment to 3.8 or newer.","message":"Python 3.7 is no longer supported as of version 7.0.0. Applications running on Python 3.7 will fail.","severity":"breaking","affected_versions":">=7.0.0"},{"fix":"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.","message":"Accessing `info.response.body` in custom instrumentation functions is disabled by default in version 6.0.0 due to performance and security concerns.","severity":"breaking","affected_versions":">=6.0.0"},{"fix":"Use the uppercase `PROMETHEUS_MULTIPROC_DIR` environment variable for configuring multiprocessing support. The lowercase version might eventually be removed.","message":"The lowercase `prometheus_multiproc_dir` environment variable was deprecated in version 5.10.0.","severity":"deprecated","affected_versions":">=5.10.0"},{"fix":"Wrap your `Instrumentator().instrument(app).expose(app)` call inside an `async def startup_event():` function decorated with `@app.on_event(\"startup\")`.","message":"For reliable startup and to avoid potential issues during reloads or when using certain ASGI servers, instrumentation should occur within an `@app.on_event(\"startup\")` function.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"When running FastAPI with multiple worker processes (e.g., using Gunicorn), Prometheus metrics require special handling. The `PROMETHEUS_MULTIPROC_DIR` environment variable must be set to a shared, writable directory.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}