Skip to main content
In this lesson you’ll implement a Prometheus histogram to measure request latency (response time) per HTTP request using Flask and the Prometheus Python client. This approach captures the time from when a request arrives until the response is sent, records it in a histogram, and exposes metrics for Prometheus to scrape. Why this matters:
  • Track latency distributions, not just averages.
  • Compute quantiles using Prometheus (histogram_quantile()).
  • Aggregate latency across many instances with histogram buckets.
Links and references

Approach (step-by-step)

  1. Create a histogram metric with the label names you need (for example, path and method).
  2. Record a timestamp when the request starts (use @app.before_request).
  3. Record another timestamp right before sending the response (use @app.after_request), subtract to get latency, then call .observe() on the histogram.
Example — full Flask application with a histogram:
Notes on Flask hooks
  • @app.before_request runs before each request handler and is the ideal place to capture the start timestamp.
  • @app.after_request runs when a view returns but before the response is sent. Use it to compute and record latency. Always return the response from after_request.
Important details and best practices
  • Keep label ordering consistent. The histogram is created with labelnames=['path', 'method']. When using positional labels, pass values in the same order: .labels(request.path, request.method). You can also use keyword labels: .labels(path=request.path, method=request.method).
  • Record metric values by calling .observe(value), where value is the measured latency in seconds (a float).
  • Expose metrics via the Prometheus client (here start_http_server(8000)), and configure Prometheus to scrape that endpoint.
When Prometheus scrapes /metrics the histogram exposes bucket counts, the total count, and the sum. Example scraped output:

Customizing buckets

Default buckets are useful for many web apps, but you may need different boundaries for your latency distribution. Supply a buckets= list when creating the Histogram:
Choose bucket boundaries that reflect the performance characteristics you care about (e.g., sub-ms, ms, or seconds ranges).

Summary vs Histogram: when to use which

  • Histograms
    • Allow you to configure buckets and compute quantiles across instances using Prometheus functions like histogram_quantile().
    • Are usually the best choice when you need aggregated, cluster-wide quantiles.
  • Summaries
    • Can provide client-side quantiles (e.g., 0.5, 0.9, 0.99) without bucket boundaries.
    • Are limited because client-side quantiles do not aggregate well across instances.
Comparison table Example: switching to a Summary is as simple as replacing Histogram with Summary:
Histograms and summaries serve different use cases. Histograms are preferred when you need configurable buckets and aggregated quantiles via Prometheus’s histogram_quantile() function. Summaries provide client-side quantiles per instance but do not aggregate quantiles across multiple instances effectively.
A quick Python client note:
The Prometheus Python client has fewer features for Summary (for example, client-side quantile objectives) compared with some other language clients. If you require aggregated quantiles across multiple instances, prefer Histograms and calculate quantiles in Prometheus with histogram_quantile().
Further reading

Watch Video