Prometheus Histogram Functions

In Prometheus, histograms are a type of metric used to track the distribution of values over a given range. Histograms are often used to measure the latency or response time of an operation and provide insights into the distribution of those values. PromQL provides several histogram-specific functions that can be used to analyze and manipulate histogram metrics. Here's an explanation of each histogram function:

1. histogram_quantile(φ, histogram_vector): This function calculates the φ-quantile (where φ is a value between 0 and 1) of the given histogram vector. It returns the value below which the given fraction of observations in the histogram falls. For example, "histogram_quantile(0.95, my_histogram)" will calculate the 95th percentile value of the histogram.

2. histogram_metric_bucket: When you define a histogram metric in Prometheus, it automatically generates multiple time series, each representing a specific bucket or range of values. The generated time series have the suffix _bucket. You can query these time series to access the count of observations falling into specific value ranges.

3. histogram_metric_sum: This function returns the sum of all observed values in a histogram metric. It provides the total sum of the values across all buckets.

4. histogram_metric_count: This function returns the total count of observations in a histogram metric. It represents the number of values that have been recorded across all buckets.

5. histogram_metric_bucket(le): This function returns the count of observations in a histogram metric that are less than or equal to the given value le. It is useful for querying the count of values falling into specific buckets or value ranges.

These functions allow you to perform various calculations and analysis on histogram metrics in Prometheus. You can retrieve specific quantiles, access individual buckets, obtain the sum and count of observations, and perform other operations to gain insights into the distribution of values captured by the histogram.

Examples

These examples demonstrate how you can use each histogram function in Prometheus to query and analyze histogram metrics:

1. histogram_quantile(φ, histogram_vector):

histogram_quantile(0.95, my_histogram)
This example calculates the 95th percentile value of the my_histogram metric.

2. histogram_metric_bucket:

my_histogram_bucket{le="0.1"}
This example retrieves the count of observations falling into the bucket with an upper bound of 0.1 in the "my_histogram" metric.

3. histogram_metric_sum:

my_histogram_sum
This example returns the sum of all observed values in the "my_histogram" metric

4. histogram_metric_count:

my_histogram_count
This example returns the total count of observations in the "my_histogram" metric.

5. histogram_metric_bucket("le"):

my_histogram_bucket{le="0.5"}
This example retrieves the count of observations falling into the bucket with an upper bound of 0.5 in the my_histogram metric.