Search code examples
kibanaprometheuspromql

How to calculate average over last week for the same time in prometheus?


I want to find if there is drop in traffic comparing the average value for that given time. For example, if the current load at 6PM is 10000 RPS, I want to compare the current load with average of 6PM data points over last 7 days. Average over last 7 days will not be right as we have peak time and low time.

Is there any way in prometheus to calculate average over the data points at the same of previous 7 days?

What i have tried so far is Query for current RPS:

sum(rate(kong_http_requests_total{}[5m]))

Query of RPS average over last three days

(sum(rate(kong_http_requests_total{}[5m] offset 1d))  + sum(rate(kong_http_requests_total{}[5m] offset 2d))  + sum(rate(kong_http_requests_total{}[5m] offset 3d)) /3

Is there any function to calculate this? Ideally I want to calculate this for last 7 days.


Solution

  • You query is correct for desired output.

    Only thin, is that you can apply sum to added up rates, not individually.

    sum(
      rate(kong_http_requests_total{}[5m] offset 1d) +
      rate(kong_http_requests_total{}[5m] offset 2d) +
      rate(kong_http_requests_total{}[5m] offset 3d) +
      rate(kong_http_requests_total{}[5m] offset 4d) +
      rate(kong_http_requests_total{}[5m] offset 5d) +
      rate(kong_http_requests_total{}[5m] offset 6d) +
      rate(kong_http_requests_total{}[5m] offset 7d)
    ) / 7
    

    Built-in functionality for such calculations is not provided.