Learn to implement a histogram metric in Python for tracking latency and response time in a Flask application.
In this guide, you’ll learn how to implement a histogram metric in Python to track the latency and response time for each request in a Flask application. We will demonstrate how to record latency using a histogram metric on a per-path and per-method basis, similar to recording counter metrics. This approach provides an in-depth view of your application’s performance.
Begin by initializing the histogram metric. In this example, the histogram is named “request_latency_seconds” and includes two label names: “path” and “method.” These labels allow you to segment metric data based on the request path and HTTP method.
To capture request latency, define two functions. The first function executes before each request, recording the start time. The second function runs after the request and calculates the latency, which is then recorded by the histogram metric.
Before Request Callback:
The before_request function records the current time via time.time() when a request is received. This timestamp is stored on the request object for later use.
After Request Callback:
After the request is processed, the after_request function calculates the latency by subtracting the recorded start time from the current time. It then updates the histogram metric using the observe method, with the request’s HTTP method and path as labels.
This setup provides a robust mechanism to measure the processing time for each request, thereby enabling effective performance monitoring.
The Prometheus client library automatically creates default buckets to group latency values. However, these default settings might not be ideal for all use cases.
To tailor the histogram to your application’s needs, you can customize the bucket boundaries. Just provide a list of bucket boundaries when initializing the metric. The example below demonstrates how to configure custom buckets:
Configuring a summary metric is very similar to setting up a histogram. The only difference is that you replace Histogram with Summary. Like the histogram, the summary metric uses the observe method after applying the relevant labels.
Copy
Ask AI
from prometheus_client import SummaryLATENCY = Summary('request_latency_seconds', 'Flask Request Latency', labelnames=['path', 'method'])# During the after_request callbackLATENCY.labels(request.method, request.path).observe(request_latency)
The Python client library for Prometheus does not implement all the features available for summary metrics, such as configuring quantiles. These features are available in some other language clients.
By following these instructions, you can effectively monitor your Flask application’s performance by tracking request latency using both histogram and summary metrics. For additional resources on Prometheus and Flask monitoring, consider exploring Prometheus Documentation and Flask Documentation.