PromQL / How to return 0 instead of ‘no data’

Nicolai Antiferov
1 min readOct 14, 2019

--

Another short note. I found the answer in the middle of issue on Grafana GitHub and decided to make a memo.

So I wanted to show in ‘single stat’ panel number of alets. But when there is no alerts, my request returns no data , which is not so informative.

And to solve this, you just need to add OR on() vector(0) to the end of your expression. It will return 0 if the metric expression does not return anything.

Explanation: Prometheus uses label matching in expressions. If your expression returns anything with labels, it won't match the time series generated by vector(0). In order to make this possible, it's necessary to tell Prometheus explicitly to not trying to match any labels by adding on().

--

--

Nicolai Antiferov
Nicolai Antiferov

Responses (6)