Get Total requests in a period of time

前端 未结 7 902
予麋鹿
予麋鹿 2020-12-08 00:02

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 w

相关标签:
7条回答
  • 2020-12-08 00:28
    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.

    0 讨论(0)
  • 2020-12-08 00:29

    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.

    0 讨论(0)
  • 2020-12-08 00:32

    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

    0 讨论(0)
  • 2020-12-08 00:35

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

    0 讨论(0)
  • 2020-12-08 00:36

    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
    
    0 讨论(0)
  • 2020-12-08 00:40

    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/

    0 讨论(0)
提交回复
热议问题