Get delta between two custom timestamps in Prometheus
Asked Answered
G

2

12

I have a Prometheus metric called device_number. What I want is to show the difference in value between now and one day/week/month etc ago. Which means subtracting two values with two different timestamps. Checking around I don't find any useful documentation on how to do it.

Something I would do, but doesn't work is:

sum(device_number) - sum(device_number[$__range])
Groscr answered 15/5, 2019 at 11:19 Comment(0)
G
21

I found offset is the correct keyword.

Query like this:

sum(vss_device_number) - sum(vss_device_number offset 1d)

Will return difference between now and yesterday.

Docs.

Groscr answered 15/5, 2019 at 11:37 Comment(0)
A
5

PromQL also provides delta() function, which can be used for returning the delta between the current time and the time specified in square brackets passed to this function. For example, the following query should return the delta for vss_device_number over the last day (see [1d]):

delta(vss_device_number[1d])

The query returns deltas per each matching time series. If you need summary delta across all the matching time series, then wrap the query into sum():

sum(delta(vss_device_number[1d]))
Argentina answered 22/3, 2022 at 18:23 Comment(2)
What if I want to calculate the delta in the [from,to] period defined by ${__range}? Is that possible?Banshee
Yes, this is possible - just use delta(vss_device_number[$__range])Argentina

© 2022 - 2024 — McMap. All rights reserved.