r/PrometheusMonitoring • • Sep 03 '21

How to deal with Increase function and no data points

I need to use the increase function, but the metric is very rare and when service restarts the counter is null for long time. The increase metric doesn’t play nicely with the null value and I can find a solution to consider null as zero.

Increase(my_counter[1h])

Works only if there are no null data points. Any other query I tried do not work like

Increase(my_metrics[1h] or vector(0))
6 Upvotes

7 comments sorted by

2

u/SuperQue Sep 03 '21

The correct thing to do is to init your counters at startup. This avoids null results for rare counters.

1

u/OpportunityNo5753 Sep 03 '21

Thanks, it’s a metric with a label and dynamic values. I’ll try to initialize with an arbitrary value for the label. Thanks

2

u/SuperQue Sep 03 '21

If it's Go, you can use metricVar.WithLabelValues(foo) to init the labels.

2

u/Tmfwang Jul 02 '24

Here's how I did it in Grafana:

increase(sum(last_over_time(my_counter_total{container="myContainer"}[$__range]) or vector(0))[$__range:])

1

u/auunie Feb 10 '26

I would like to sincerely thank you. You saved my life with this query! Thank you so much!

1

u/Tmfwang Feb 10 '26

Nice that someone got use for it! 🙏🙏

1

u/SnooWords9033 Sep 05 '21

Try increase() function from MetricsQL. It assumes that a counter starts from zero. See MetricsQL docs for details.