Prometheus rate function output to whole number

4/13/2021

Is there a way to convert the output of the rate function in Prometheus to a whole number?

I'm trying to get the total number of pod restarts over a specified timeframe and although this query produces output, I get the result in decimals which isn't what I need.

rate(kube_pod_container_status_restarts_total{namespace=~"jenkins"}[10h]) * 60 * 5 > 0

What I get back is 0.21761280931586605 but I'd rather get a whole number.

I'm just not sure what function to use.

-- Hammed
kubernetes
prometheus
prometheus-operator
promql

2 Answers

4/14/2021

You can use the following functions to round a decimal number:

  • round = Rounds to the nearest integer.
  • ceil = Rounds UP to the nearest integer.
  • floor = Rounds DOWN to the nearest integer.

See more details about the "round" function in the Prometheus documentation here.

In your case, you're using "rate" so you get the number of restarts per second 60 5. Which is the same as the number of restarts every 5 minutes.

But, if you want to count the number of restarts (and not calculate the rate of restarts), maybe you should use the "increase" function instead.

See more details about the "increase" function in the Prometheus documentation here.

-- Marcelo Ávila de Oliveira
Source: StackOverflow

4/14/2021

You probably don't want to use rate. I'm guessing you're looking for increase which will count how many restarts have occurred over a time period

increase(kube_pod_container_status_restarts_total{namespace=~"jenkins"}[10h])
-- Brandon Kauffman
Source: StackOverflow