'Get Total requests in a period of time

I need to show, in Grafana, a panel with the number of requests in the period of time selected in the upper right corner.

For this I need to solve 2 issues here, I will ask the prometheus question here and the Grafana question in another link.

If I have a Counter http_requests_total, How can I build a query to get an integer with the total number of requests during a period of time (for example:24hs)?



Solution 1:[1]

SO won't let me comment on Yoory's answer so I have to make a new one...

In Grafana 5.3, they introduced $__range for Prometheus that's easier to use:

sum(rate(http_requests_total[$__range]))

This variable represents the range for the current dashboard. It is calculated by to - from

http://docs.grafana.org/features/datasources/prometheus/

Solution 2:[2]

As per increase() documentation, it is not aggregation operator. Thus, it will give wrong answer. (See note.)

You should use sum_over_time() function which aggregates over time interval.

sum_over_time(http_requests_total[24h])

If you have multiple counters, use sum() operator:

sum(sum_over_time(http_requests_total[24h]))

Note: I have 5 datapoints which has values: 847, 870, 836, 802, 836. (updated every minute)

increase(http_requests_total[5m]) returns 2118.75 

sum_over_time(http_requests_total[5m]) returns 4191

Solution 3:[3]

http_requests_total - http_requests_total offset $__interval > 0

This builds off another answer and comment that works and handles restart situations.

The offset keeps the value always as an integer and does not try to perform interpolation like the increase and rate functions.

The > 0 filter at the end will ignore all of the negative values that could be captured due to a restart.

The end result is the accurate total number of requests over time if you choose to chose the total value in the legend.

Solution 4:[4]

Solution: In order to calculate sum of https counters on prometheus grafana you should use increase method and set generic Time Range $interval in order to sum and calculate all http requests counters.

increase(http_requests_total[$interval])

According to Prometheus Reference:

increase() increase(v range-vector) calculates the increase in the time series in the range vector. Breaks in monotonicity (such as counter resets due to target restarts) are automatically adjusted for. The increase is extrapolated to cover the full time range as specified in the range vector selector, so that it is possible to get a non-integer result even if a counter increases only by integer increments.

The following example expression returns the number of HTTP requests as measured over the last 5 minutes, per time series in the range vector:

increase(http_requests_total{job="api-server"}[5m]) increase should only be used with counters. It is syntactic sugar for rate(v) multiplied by the number of seconds under the specified time range window, and should be used primarily for human readability. Use rate in recording rules so that increases are tracked consistently on a per-second basis.

P.S

  1. You should set the correct Quick range on Grafana for setting the right time frame you choose (that straight rendered to $interval variable) In addition I suggest to set on the Graph visualisation the right resolution and Min time interval ( in your case it's per day -> 1d)

2.In order to sum all amount of requests just perform sum function

sum(increase(http_requests_total[$interval]))

Solution 5:[5]

To get the exact count for the last 24 for hours I have created the following query:

max_over_time(http_requests_total[6s])- min_over_time(http_requests_total[24h])

Note: works for me :)

Solution 6:[6]

To get the accurate total requests in a period of time, we can use offset:

http_requests_total - http_requests_total offset 24h

increase will extrapolate the range so that we can see float number in the result.

By using offset, the value is always integer because it just calculates the difference between start and end

Solution 7:[7]

It seems to me that all the previous answers have misinterpreted the questions, which is to get a count from t0 to t1, where the value at t0 should be 0.

For this one can use the @ modifier as per the documentation https://prometheus.io/docs/prometheus/latest/querying/basics/#modifier:

http_requests_total - http_requests_total @ start()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 donotreply
Solution 2 Optimus Prime
Solution 3 Sean Franklin
Solution 4 avivamg
Solution 5 Andrii Soluk
Solution 6 Haoyuan Ge
Solution 7 glep