starlette_exporter
Prometheus metrics middleware for Starlette and FastAPI applications, exposed through a ready-made /metrics endpoint.
Repository Health
Technical Analysis
starlette_exporter is a Prometheus exporter implemented as ASGI middleware for Starlette and FastAPI apps. Adding it instruments your application with request-level metrics — total request counts, request-duration histograms, and an in-progress gauge — each labelled by HTTP method, path, and status code.
It ships a PrometheusMiddleware you attach to your app and a handle_metrics handler you mount at /metrics for Prometheus to scrape. Options cover metric prefixes, default labels, path templating for route parameters, exemplars, custom metrics, and multiprocess (gunicorn) deployments.
What You Get
PrometheusMiddlewarethat records request totals, durations, and in-progress counts- A
handle_metricshandler to expose a Prometheus-scrapeable/metricsendpoint - Automatic labels for HTTP method, path, and status code
- Configurable app name, metric prefix, default labels, and path templating for route params
- Support for exemplars, custom metrics, and multiprocess (gunicorn) deployments
Common Use Cases
- Instrumenting a FastAPI service with Prometheus metrics for latency and error-rate dashboards
- Exposing RED metrics (rate, errors, duration) from a Starlette ASGI app
- Adding per-route request histograms to power Grafana SLO panels
- Collecting metrics across multiple gunicorn workers in multiprocess mode
Under The Hood
Architecture - The core is an ASGI middleware that wraps each request: it increments an in-progress gauge, times the request, and on completion records a counter and a duration histogram with method/path/status_code labels, using route templating so path parameters collapse to a stable label (e.g. /items/{id}). The handle_metrics route renders the prometheus_client registry, honoring multiprocess mode when configured. Tech Stack - Pure Python built on Starlette’s middleware interface and the official prometheus_client library; works unchanged under FastAPI since FastAPI is built on Starlette. Packaged with a modern pyproject/poetry setup. Code Quality - A focused, mature codebase with a test suite covering middleware behavior, options, and multiprocess handling; development activity is low, consistent with a small, feature-complete utility. API Design - Integration is two lines — add the middleware, add the route — and every behavior (prefix, labels, filtering unhandled paths, exemplars, custom metrics) is a keyword option, keeping the common case trivial while leaving room for advanced tuning.