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?
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.
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.
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.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With