'PromQL: how to change null value to an arbitrary value?

I have situation where my metric is set to 0 by a program when everything works fine. I would like to treat null value as an error value (in my case 1). The easiest approach I came up with is to replace null values in metric to 1. Grafana doesn't seem to support that, so my question is whether there is a PromQL expression to replace each null value with different one.



Solution 1:[1]

There is no null in Prometheus, only samples that don't exist. It's easiest to always have your application expose a metric, however you can use unless on applications which don't do so. See Absent Alerting for Scraped Metrics.

Solution 2:[2]

The or vector(1) can help in cases when your query returns a single time series without additional labels. In this case the following query fills gaps with 1:

sum(your_query) or vector(1)

If your_query returns multiple time series or a time series with arbitrary set of additional labels, then Prometheus doesn't provide an easy universal way do fill gaps with the specified value :(

Fortunately, MetricsQL in VictoriaMetrics provides an easy solution - default operator. For example, the following query fills all the gaps for all the time series returned from your_query with the specified value 1:

your_query default 1

See more details about default operator and about other MetricsQL features in these docs.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 brian-brazil
Solution 2 valyala