starlette_exporter

Prometheus metrics middleware for Starlette and FastAPI applications, exposed through a ready-made /metrics endpoint.

Library
PyPI
v0.24.0
413stars
Apache License 2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
63/100Good
Development Activity60
Maintenance56
Community48
Maturity60
Momentum28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
79/100Good
Architecture80
Code Quality82
Innovation74
Learning Curve62

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

  • PrometheusMiddleware that records request totals, durations, and in-progress counts
  • A handle_metrics handler to expose a Prometheus-scrapeable /metrics endpoint
  • 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search