Search code examples
prometheuspromql

How do I perform a logical AND between two expressions for a prometheus alert?


I have two expressions A and B and would like to create a prometheus alert such that ((A != 0) AND (B <30)) The AND operator in PromQL performs set operations. And also the result of A and B have different labels. I am able to perform A!=0 and B<30 but can't figure out the AND.

Output of A: enter image description here

Output of B: enter image description here


Solution

  • By default Prometheus performs and operation individually per each pair of time series on the left and the right side of the and with identical set of labels according to these docs.

    In your case the A and B returns nothing because A returns a time series with empty labelset, while B returns a time series with non-empty labelset, so Prometheus cannot find a pair of time series with matching labels for performing the and operation.

    This can be fixed by using on() modifier, which instructs Prometheus to take into account the set of labels specified inside on() when searching for matching pairs of time series on the left and the right side of and operator. So the following query should work for your case:

    A and on() B
    

    This query specifies an empty labelset inside on(), so Prometheus matches any time series from A with any time series from B. See these docs about on() modifier.