'Simplest way to reset all values of a Prometheus Gauge to 0

Currently, I'm looping through all the labels and setting their values to 0 manually. I was wondering whether there is less error-prone way.

Context: I'm using a Prometheus Gauge to monitor the maximum number of duplicates found in my data. The data duplication check itself is triggered once on every 4 hours. When the latter happens, all labels' values are reset to 0, then every time I detect duplicates the Gauge's value for the corresponding labels is set to max(previousValue, newValue).



Solution 1:[1]

When I was checking the Prometheus github issues, I stumbles upon this line of code that basically resets the gauge.

YOUR_GAUGE._metrics.clear()

Solution 2:[2]

https://github.com/prometheus/client_java/blob/master/simpleclient/src/main/java/io/prometheus/client/SimpleCollector.java

Confirmed that we can initialize Gauge with

YOUR_GAUSE.clear() 

I put it to catch block to handle exception :) Note that, it clears all metrics instead setting 0.

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 Ron Zhang
Solution 2