Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prometheus: How to calculate proportion of single value over time

Tags:

prometheus

I would like to calculate the percentage time that a given metric is non-zero in a time range. I know I can get the number of values in that time range using

count_over_time(my_metric[1m])

but what I would like is something like

count_over_time(my_metric[1m] != 0) / count_over_time(my_metric)

I can't do this because binary expression must contain only scalar and instant vector types.

Is there a way to do what I'm trying?

like image 372
thehouse Avatar asked Jul 20 '17 11:07

thehouse


People also ask

How is Prometheus percentage calculated?

CPU Usage. The above query produces the rate of increase over the last five minutes, which lets you see how much computing power the CPU is using. To get the result as a percentage, multiply the query by 100.

How do you use the rate function in Prometheus?

Prometheus rate function is the process of calculating the average per second rate of value increases. You would use this when you want to view how your server CPU usage has increased over a time range or how many requests come in over a time range and how that number increases.

How is Prometheus average response time calculated?

If you want to get the average then you should divide the _sum by the _count counter. If your labels are ok, then you will get data(if there any) by below PromQL.

What is increase function in Prometheus?

Prometheus' increase function calculates the counter increase over a specified time frame². The following PromQL expression calculates the number of job executions over the past 5 minutes. increase(job_execution_total[5m]) Since our job runs at a fixed interval of 30 seconds, our graph should show a value of around 10.


1 Answers

If the value can only be 0 or 1, then you can use avg_over_time.

If it can have other values, then you need to convert it to 0 or 1 via a recording rule:

my_metric_nonzero = my_metric != bool 0

And then you can do avg_over_time(my_metric_nonzero[1m])

See also https://www.robustperception.io/composing-range-vector-functions-in-promql/

like image 171
brian-brazil Avatar answered Apr 28 '23 09:04

brian-brazil