9

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])
Matej
  • 565
  • 2
  • 9
  • 21

2 Answers2

18

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.

Matej
  • 565
  • 2
  • 9
  • 21
3

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]))
valyala
  • 11,669
  • 1
  • 59
  • 62