A Prometheus metrics exporter middleware for Starlette and FastAPI applications.
starlette_exporter is a Python library that provides Prometheus metrics export capabilities for web applications built with the Starlette framework or FastAPI. It automatically instruments applications to collect key performance metrics like request counts, durations, and concurrency, exposing them via a standard /metrics endpoint for Prometheus scraping. This solves the problem of adding observability to ASGI-based applications without manual instrumentation.
Developers and DevOps engineers building or maintaining Starlette or FastAPI applications who need production-grade monitoring and alerting via Prometheus. It's particularly useful for teams deploying microservices or APIs that require consistent metric collection.
Developers choose starlette_exporter because it offers a simple, standardized way to add Prometheus metrics to Starlette/FastAPI apps with minimal code, while providing robust customization options to handle path grouping, label management, and multiprocess deployments. Its tight integration with these frameworks and focus on preventing metric cardinality issues make it a reliable choice.
Prometheus exporter for Starlette and FastAPI
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Integrating metrics requires just two lines of code: adding the PrometheusMiddleware and exposing the /metrics endpoint, as shown in the FastAPI and Starlette examples in the README.
Defaults to grouping paths by named parameters (e.g., /api/items/{id}), which prevents metric cardinality spikes by aggregating requests, a critical feature highlighted in the documentation.
Allows adding default labels via static values or callbacks, with helpers like from_header for extraction, enabling rich contextual metrics without manual instrumentation as detailed in the Labels section.
Supports adding trace IDs to metrics via exemplars for correlation with tracing systems, though it requires using the handle_openmetrics handler instead of the standard one, as explained in the Exemplars section.
Using with gunicorn requires setting the PROMETHEUS_MULTIPROC_DIR environment variable and extra configuration, adding operational complexity compared to single-process deployments, as admitted in the documentation.
Out-of-the-box metrics are limited to request counts, durations, and concurrency; optional metrics like body sizes must be explicitly enabled, lacking deeper insights such as database or cache performance.
Tightly coupled with the prometheus_client library, making it difficult to switch to other monitoring systems without rewriting metric collection logic, and exemplars only work with the openmetrics format.