Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate Max over time on sum function in Prometheus

I am running prometheus in my kubernetes cluster. I have the following system in kubernetes:

I have 4 nodes. I want to calculate free memory. I want to have the summation of those four nodes. Then I want to find the maximum over 1 day. So, for example,

at time=t1 node1: 500 MB node2: 600 MB node3: 200 MB node4: 300 MB Total = 1700 MB

at time=t2 node1: 400 MB node2: 700 MB node3: 100 MB node4: 200 MB Total = 1300 MB

at time=t3 node1: 600 MB node2: 800 MB node3: 1200 MB node4: 1300 MB Total = 3900 MB

at time=t4 node1: 100 MB node2: 200 MB node3: 300 MB node4: 400 MB Total = 1000 MB

So, The answer to my query should be 3900 MB. I am not able to do max_over_time for the sum.

I have done like this(which is not working at all):

max_over_time(sum(node_memory_MemFree)[2m])
like image 975
Darshil Avatar asked Jul 13 '17 01:07

Darshil


People also ask

What is Avg_over_time Prometheus?

The following functions allow aggregating each series of a given range vector over time and return an instant vector with per-series aggregation results: avg_over_time(range-vector) : the average value of all points in the specified interval.

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.

What is Group_left in Prometheus?

This is where group_left comes in. It says that for each set of matching labels, many time series on the left hand side can match with just one time series on the right hand side. It'll then perform the binary operation, and keep all the labels of the left hand side. This gives us the expression.


1 Answers

Since version 2.7 (Jan 2019), Prometheus supports sub-queries:

max_over_time( sum(node_memory_MemFree_bytes{instance=~"foobar.*"})[1d:1h] )

(metric for the past 2 days, with a resolution of 1 hour.)

Read the documentation for more informations on using recording rules: https://prometheus.io/docs/prometheus/latest/querying/examples/#subquery

However, do note the blog recommendation:

Epilogue

Though subqueries are very convenient to use in place of recording rules, using them unnecessarily has performance implications. Heavy subqueries should eventually be converted to recording rules for efficiency.

It is also not recommended to have subqueries inside a recording rule. Rather create more recording rules if you do need to use subqueries in a recording rule.

The use of recording rules is explained in brian brazil article: https://www.robustperception.io/composing-range-vector-functions-in-promql/

like image 198
Franklin Piat Avatar answered Sep 25 '22 23:09

Franklin Piat