How to merge zero values (vector(0) with metric values in PromQL

36.3k Views Asked by At

I'm using flexlm_exporter to export my license usage to Prometheus and from Prometheus to custom service (Not Grafana).

As you know Prometheus hides missing values.

However, I need those missing values in my metric values, therefore I added to my prom query or vector(0)

For example:

flexlm_feature_used_users{app="vendor_lic-server01",name="Temp"} or vector(0)

This query adds a empty metric with zero values.

My question is if there's a way to merge the zero vector with each metric values?

Edit:

I need grouping, at least for a user and name labels, so vector(0) is probably not the best option here? Example query with a specific user with missing values and zero vector

I tried multiple solutions in different StackOverflow threads, however, nothing works.

Please assist.

4

There are 4 best solutions below

2
On

If you do sum(flexlm_feature_used_users{app="vendor_lic-server01",name="Temp"} or vector(0)) you should get what you're looking for, but you'll lose possibility to do group by, since vector(0) doesn't have any labels.

1
On

It would help if you used Absent with labels to convert the value from 1 to zero, use clamp_max

( Metrics{label=“a”} OR clamp_max(absent(notExists{label=“a”}),0))
+
( Metrics2{label=“a”} OR clamp_max(absent(notExists{label=“a”}),0)

Vector(0) has no label.

clamp_max(Absent(notExists{label=“a”},0) is 0 with label.

0
On

There is no an easy generic way to fill gaps in returned time series with zeroes in Prometheus. But this can be easily done via default operator in VictoriaMetrics:

flexlm_feature_used_users{app="vendor_lic-server01",name="Temp"} default 0

The q default N fills gaps with the given default value N per each time series returned from q. See more details in MetricsQL docs.

0
On

I needed a similar thing, and ended up flattening the options. What worked for me was something like:

(sum by xyz(flexlm_feature_used_users{app="vendor_lic-server01",name="Temp1"} + sum by xyz(flexlm_feature_used_users{app="vendor_lic-server01",name="Temp2"}) or

sum by xyz(flexlm_feature_used_users{app="vendor_lic-server01",name="Temp1"} or

sum by xyz(flexlm_feature_used_users{app="vendor_lic-server01",name="Temp2"}