Search code examples
prometheus

A promQL query to calculate the difference between now and one day ago ( consider zero if no data)?


My metric, which is of type ‘counter’, is named ‘http_counters’ and has two labels: ‘node’ and ‘status’.

I want to calculate the difference between the current counter value and the value from 1 day ago, categorized by ‘node’ and ‘status’.

Please note, if there is no data from 1 day ago, the value from 1 day ago is considered as zero. Also, the difference still retains the two labels ‘node’ and ‘status’

I think the statement is about:

http_counters{} - (http_counters{} offset 1d or on(????) vector(????))

How fill it? thank you


Solution

  • The easiest way here is not to apply or inside of the right operand, but to whole expression instead:

    http_counters - http_counters offset 1d
     or http_counters
    

    Here, you'll get difference for cases when metric was present an our ago, and if it was not, you'll get current value instead.


    Please, be advised about possible issues near counter resets: you might get large negative values as a result of your query. Function increase accounts for counter resets automatically (but it has it's own problems, like unexpected extrapolated)