kuda.ai

Thoughts on programming and music theory.

Prometheus: PromQL to show 0 instead of "no data".

Created on April 17, 2025 Updated on May 13, 2025

prometheus/promql

Useful links:

or on() vector(0)

Taken from nklya.medium.com/promql-how-to-return-0-instead-of-no-data:

OR on() vector(0)

However, if you have labels on your metric, this will create an additional series and is only useful if you have no labels.

OR label_replace

With label_replace, you can add a label to vector(0) to match your data.

In the following two examples, failed may be non-existent. However, we want to see a 0 instead of a nil (or a break in your line chart).

sum by(status) (
increase(runs_total[1d])
  or label_replace(vector(0), "status", "failed", "", "")
)
sum by (status) (
  increase(jobs_total{status="success"}[1h])
  or (
    increase(jobs_total{status="failed"}[15m]) 
    or label_replace(vector(0), "status", "failed", "", "")
  )
)

clamp_min

default to 1 to not fail division:

sum by(label) (increase(metric{label=~"${label:regex}"}[1h]))
/
clamp_min(
    sum by(label) (increase(metric{label=~"${label:regex}"}[1h])),
    1
)