operatorsprometheuspromql

PromQL "if" condition "and" "or" operator


in PromQL, I want to write

If metric_a>=bool 3:
  return metric_b
else:
  return 1

I am thinking to write as

(metric_b and metric_a>=3) or metric_a<bool 3

but I found that when I switch the metric order, like A or B, B or A, the query result would change, also am not sure if what I have means my if-else why the or/and operator would give inconsist result? also what is a best way to present the if-else statement in here?


Solution

  • I checked your proposed solution:

    (metric_b and metric_a>=3) or metric_a<bool 3
    

    and it worked like expected, returning the value of metric_b when metric_a is >= 3, and 1 otherwise.

    It's important to note that "VECTOR1 and VECTOR2" it's not necessarily equals to "VECTOR2 and VECTOR1". Take a look at the Prometheus documentation about this:

    vector1 and vector2 results in a vector consisting of the elements of vector1 for which there are elements in vector2 with exactly matching label sets. Other elements are dropped.

    The results are always from the first vector of the "and" clause.

    For example, the following query:

    enter image description here

    Gives a different result of the following one:

    enter image description here