Search code examples
prometheusgrafana

How to get increase value for a given time range?


I'm using prometheus as grafana's datasoure. I want to get growth of a my_metrics(type Count) for a given time range. For example I can calculate the increase over the last few hours:

my_metrics{label="label1"} - my_metrics{label="label1"} offset $__range

But how can I calculate the increase for given time range? For example increase for 2022/05/19 18:00:00 - 2022/05/20 00:00:00

Thanks.


Solution

  • Combine sum with rate. Rate will be per second, so if you sum up all rate per seconds data points over a given interval you will get the increase over a given time range:

    sum by(label) (rate(my_metrics{label="label1"}[time range]))

    Edit: (delta and some concrete time slot)

    It seems as if the delta function is an easier way to achieve this in the case of gauges.

    You will of course get a time series of computed values. To get the value for 2022/05/19 18:00:00 - 2022/05/20 00:00:00 just use an interval of 2h and get the computed value for 2022/05/20 00:00:00 by using a Table.

    See answer of Lentil1016 to a similar question.