liveBook · Manning

Starlette_exporter

As explained in Machine Learning Platform Engineering

starlette_exporter is a Python library that exposes basic metrics for Starlette-based web applications.

starlette_exporter adds basic Prometheus metrics to Starlette-based applications, making request volume and response performance visible to an operational monitoring stack. section 3.5

Expose metrics from a FastAPI application

from fastapi import FastAPI from starlette_exporter import PrometheusMiddleware, handle_metrics app = FastAPI() app.add_middleware(PrometheusMiddleware) app.add_route("/metrics", handle_metrics) @app.get("/") async def root(): return {"status": "ok"} section 3.5

The exposed metrics support several operational views:

  • Counters track request totals through metrics such as starlette_requests_total. section 3.5
  • Gauges represent values that can rise or fall, such as memory consumption. section 3.5
  • Histograms group response times into buckets for percentile reporting. section 3.5
Figure 3.17 Application 1 and Application 2 expose an endpoint for metrics. Prometheus scrapes those metrics, which are then visualized by Grafana. Alertmanager uses those metrics to check if any alerts need to be routed to a channel; in this case, it’s an email.
A diagram of a diagram

AI-generated content may be incorrect.

Connects the application's /metrics endpoint to Prometheus scraping, Grafana visualization, and Alertmanager routing. section 3.5

How Machine Learning Platform Engineering covers it

How deep it goes
Shows middleware and route configuration, then distinguishes counters, gauges, and histograms without detailing exporter internals.
Examples
Uses a FastAPI endpoint, starlette_requests_total, response-time histograms, custom counters, and Prometheus queries.
What it assumes
Assumes a Starlette-based application and introduces the metrics endpoint and metric types directly.
What you can do after
Apply it via PrometheusMiddleware and handle_metrics to expose request metrics from a FastAPI application.
How it connects
Connects FastAPI and Starlette middleware with Prometheus, Grafana, Alertmanager, and the Prometheus client library.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning Platform Engineering. The section links point to the parts of the book it draws on.