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

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().



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store