Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot average per day from Prometheus

I have to create a visualization from Prometheus metrics. I have a counter metrics http_request_counter, I wanted to show a summary of total requests served in a day. This is how I did when we had graphite as our data source.

alias(summarize(sumSeries(consolidateBy(nonNegativeDerivative(http_request_counter.count), 'sum')), '1d', 'sum', false), 'TPS per day')

I saw few documentations and tried with increase(http_requests_total[24h]) which plotted the graph with t-24h values.

Can someone help me find an equivalent of summarize function in prometheus please?

like image 228
NewUser Avatar asked Nov 01 '25 21:11

NewUser


1 Answers

You need sum(increase(http_requests_total[24h])). It returns the summary number of requests during the last 24 hours for every point on the graph. By default Grafana queries many points from Prometheus in order to draw smooth graph for the current horizontal resolution of the graph. The number of points on the graph can be adjusted with the min step option when editing the graph in Grafana. Another option is to use subqueries with the desired step for the outer query:

last_over_time(sum(increase(http_requests_total[24h]))[1d:1d])

It will return continous line with one-day steps. These steps will be shifted by one day in the past, e.g. the step for the current day will contain the number of requests made on the previous day. This shift can be removed with negative offset:

last_over_time(sum(increase(http_requests_total[1d] offset -1d))[1d:1d])

Unfortunately, this query doesn't work out of the box in Prometheus, since it doesn't accept negative offsets by default. They must be enabled with --enable-feature=promql-negative-offset command-line flag when starting Prometheus. Fortunately, negative offsets are supported by default in MetricsQL in VictoriaMetrics - the system I work on.

like image 53
valyala Avatar answered Nov 03 '25 22:11

valyala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!