Search code examples
prometheuspromql

Setting default value during binary operation when value doesn't exist


I have 2 metrics and first metric doesn't always exist. In cases when it doesn't exist I want to behave as it had a value 0 (or result to have a value 0)

Metrics:

metric_1{label=1} 10
...
metric_2{label=1} 2
metric_2{label=2} 5
...

Operation:

metric_1 / metric_2

Result:

{label=1} 5

Expected:

{label=1} 5
{label=2} 0

My real-life example has many labels so creating a static vector with {label=2} doesn't work.


Solution

  • Try to use the following query:

    (metric_1 or metric_2 * 0) / metric_2