Histogram is a metric that samples observations and counts them in configurable buckets. It is useful to measure things like request durations or response sizes.
When scraped an histogram produces multiple time series:
- cumulative counters for the observation buckets (ex.
- total sum of observed values (ex.
- count of events that have been observed (ex.
You can use the
histogram_quantile function in prometheus to calculate quantiles, for examples if you have an
http_request_duration_seconds histogram you could see how an endpoint is doing in the 95th percentile.
For more information on how to use histograms check the prometheus docs
By default an
Histogram will be created with the following buckets:
Default buckets are tailored to broadly measure the response time (in seconds) of a network, you most likely will want to pass a customized list and you can with the
buckets parameter when creating the histogram:
buckets accepts a
+Inf bucket will be added automatically, this is
float('inf') in python
Observe a value
To observe a value you can call the
You can track time with the
time() context manager, it will track the duration in seconds:
As a Decorator
When used as a decorator the
Histogram will time the piece of code, syntactic sugar to the
time() context manager: