Search code examples
expressionprometheusalertpromql

PromQL expression to find common labels in two intervals


Lets say i have the following metrics collected from now-60m to now-30m -

fruits{name="apple"} 1
fruits{name="orange"} 1
fruits{name="pear"} 1

and the following metrics collected from now-30m to now

fruits{name="banana"} 1
fruits{name="pear"} 1
fruits{name="watermelon"} 1

How would i go about writing an alert rule to check if any fruit is present in both collections and it's name? In this case it will be

pear

Solution

  • Try the following query:

    count(last_over_time(fruits[30m] offset 30m)) by (name)
    and
    count(last_over_time(fruits[30m])) by (name)
    

    It should return name labels, which existed on both time ranges - (now-60m .. now-30m] and (now-30m .. now].

    It uses the following functions: