Skip to Content

How do you instrument FastAPI with Prometheus?

Category: Observability & Tracing

Answer

prometheus-fastapi-instrumentator (or starlette-exporter) adds middleware that emits http_request_duration_seconds (histogram) per route, plus http_requests_in_progress (gauge). You exclude /metrics, /health, /docs from the histogram so probe traffic doesn’t dominate.

Concrete examples from the fca project context

Example 1

Instrumentator().instrument(app).expose(app, endpoint=“/metrics”, include_in_schema=False).

Example 2

exclude_handlers=[“/metrics”, “/health”, “/docs”, “/openapi.json”].

Example 3

Histogram buckets default to ~ms; tune for your SLO.

Data flow / flow chart

request -> middleware -> {route, status, duration} -> /metrics
  Prometheus scrapes /metrics -> Grafana queries

Takeaway

Default buckets past; tune them for your latency SLO, or the dashboard lies.