client_java icon indicating copy to clipboard operation
client_java copied to clipboard

StatefulMetric clear() invocation causes noLabels increase to stop reporting on collect()

Open iaco86 opened this issue 1 year ago • 0 comments

It looks like the current implementation of the io.prometheus.metrics.core.metrics.StatefulMetric#clear method (see #935) is clearing the data, but not nullifying the io.prometheus.metrics.core.metrics.StatefulMetric#noLabels field, allowing that to be returned by the io.prometheus.metrics.core.metrics.StatefulMetric#getNoLabels method, but preventing it from being correctly collected.

This is easily reproducible in the io.prometheus.metrics.core.metrics.StatefulMetricTest#testClearNoLabels test by adding an inc() call and verification after the first clear:

...
        counter.clear();
        // No labels is always present, but as no value has been observed after clear() the value should be 0.0
        Assert.assertEquals(1, counter.collect().getDataPoints().size());
        Assert.assertEquals(0.0, counter.collect().getDataPoints().get(0).getValue(), 0.0);

        // Now we're testing label-less inc() still works correctly
        counter.inc();
        Assert.assertEquals(1, counter.collect().getDataPoints().size());
        Assert.assertEquals(1.0, counter.collect().getDataPoints().get(0).getValue(), 0.0);

If my understanding of the issue is correct, the io.prometheus.metrics.core.metrics.StatefulMetric#clear should also set noLabels = null.

iaco86 avatar Jul 19 '24 01:07 iaco86