Facundo Chambo
Facundo Chambo

Reputation: 3388

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)?

Upvotes: 203

Views: 381405

Answers (11)

donotreply
donotreply

Reputation: 839

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/

Upvotes: 73

Sudhakar MNSR
Sudhakar MNSR

Reputation: 764

sum(increase(http_requests_total[$__range])) worked for me. $__range is grafana variable which reflects time range selected in top right of grafana.

It is highly recommended to use increase to automatically adjust counter for resets.

Increase results in fractional values due to extrapollation at boundaries as well. Rounding functions with the query also worked for me.

round(sum(increase(http_requests_total[$__range])))

or

Since you are using grafana you can set decimals to 0 in standard options. (in grafana 9.1.8 this is not working, its bug). So I have set this in panel json as below.

  "fieldConfig": {
    "defaults": {
      "mappings": [],
      "thresholds": {
        "mode": "absolute",
        "steps": [
          {
            "value": null,
            "color": "green"
          },
          {
            "value": 80,
            "color": "red"
          }
        ]
      },
      "color": {
        "mode": "thresholds"
      },
      "decimals": 0,
      "unit": "none"
    },
    "overrides": []
  }

Note: These queries are tried with grafana stat panel. (grafana version 9.1.8)

Upvotes: 3

Vladislav
Vladislav

Reputation: 1995

The easiest way to do this in the modern version of Grafana is to set the Calculation field of the Value options section to "Difference" reduce function:

enter image description here

The query is your http_requests_total data without any aggregation.

Upvotes: 8

Snaper
Snaper

Reputation: 21

Recently, I have the confusion too, and I got some solutions for it, but all of them are not all work perfectly.


solution 1:

    sum(increase(your_point))[$__interval]

This one will cause some different value with same statement, and also will cause the zero-value (actually not zero).


Solution 2:

    max_over_time(your_point[$__range])- min_over_time(your_point[$__range])
    your_point[$__range] -  your_point offset $__range

Both of this have potentially bug (value reset), and only can get the value in [sometime-now], cannot get the answer in anytime period.


Solution 3:

    sum_over_time(your_point)[$__range]

This solution will take much time to change your metrics (reset in certain period), but truely work.

Is anyone can give me another solution?

Upvotes: 2

glep
glep

Reputation: 92

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()

Upvotes: 1

Sean Franklin
Sean Franklin

Reputation: 247

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.

Upvotes: 22

Haoyuan Ge
Haoyuan Ge

Reputation: 3689

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

Upvotes: 0

Aviv
Aviv

Reputation: 14467

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]))

Upvotes: 12

Andrii Soluk
Andrii Soluk

Reputation: 549

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 :)

Upvotes: 3

foxtrot9
foxtrot9

Reputation: 551

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

Upvotes: 32

Yoory N.
Yoory N.

Reputation: 5453

What you need is the increase() function, that will calculate the difference between the counter values at the start and at the end of the specified time interval. It also correctly handles counter resets during that time period (if any).

increase(http_requests_total[24h])

If you have multiple counters http_requests_total (e.g. from multiple instances) and you need to get the cumulative count of requests, use the sum() operator:

sum(increase(http_requests_total[24h]))

See also my answer to that part of the question about using Grafana's time range selection in queries.

Upvotes: 280

Related Questions