Use duration variable as label selector

170 views Asked by At

I have a variable latency_target = 5s. How use it in a PromQL query as a histogram label selector:

sum(rate(histogram{le="$latency_target"}[$interval]))

This does not work, dues to s suffix. I tried to use duration function, but it does not work

1

There are 1 answers

0
valyala On

If you want calculating the percentage of requests with durations not exceeding the given threshold (aka SLI / SLA / SLO or the inverse of histogram_quantile()), from Prometheus histogram, then there are bad and good news:

  • The bad news is that Prometheus doesn't provide such a functionality :(
  • The good news is that other Prometheus-like monitoring solution I work on - VictoriaMetrics - provides histogram_share() function for calculating the SLI / SLA from Prometheus histograms. For example, the following query returns the percentage (on the range 0..1) of queries with durations not exceeding 1.5 seconds over the last 10 minutes:
histogram_share(1.5s, sum(rate(http_request_duration_seconds_bucket[10m])) by (le))

You can freely substitute the first arg to histogram_share() function with Grafana variable, which contains Prometheus-compatible duration, as well as the lookbehind window in square brackets for the rate() function:

histogram_share($latency_target, sum(rate(http_request_duration_seconds_bucket[$interval])) by (le))

This query should work correctly for any Prometheus-compatible durations behind latency_target and $interval variables.