prometheus / client_java

Prometheus instrumentation library for JVM applications
http://prometheus.github.io/client_java/
Apache License 2.0
2.18k stars 796 forks source link

Add counter name to exception message when negative value is detected #1090

Open AntuanRokanten opened 2 weeks ago

AntuanRokanten commented 2 weeks ago

Description:

I am using micrometer-registry-prometheus version 1.13.0 with Spring Boot 3.3.0, and sometimes I encounter the following exception when Prometheus scrapes metrics:

java.lang.IllegalArgumentException: -2.0: counters cannot have a negative value
at io.prometheus.metrics.model.snapshots.CounterSnapshot$CounterDataPointSnapshot.validate(CounterSnapshot.java:74)
at io.prometheus.metrics.model.snapshots.CounterSnapshot$CounterDataPointSnapshot.(CounterSnapshot.java:58)
at io.prometheus.metrics.model.snapshots.CounterSnapshot$CounterDataPointSnapshot.(CounterSnapshot.java:46)
at io.micrometer.prometheusmetrics.PrometheusMeterRegistry.lambda$newFunctionCounter$20(PrometheusMeterRegistry.java:372)
at io.micrometer.prometheusmetrics.MicrometerCollector.collect(MicrometerCollector.java:77)
at io.prometheus.metrics.model.registry.PrometheusRegistry.scrape(PrometheusRegistry.java:72)
at io.prometheus.metrics.model.registry.PrometheusRegistry.scrape(PrometheusRegistry.java:57)
at org.springframework.boot.actuate.metrics.export.prometheus.PrometheusScrapeEndpoint.scrape(PrometheusScrapeEndpoint.java:59)

The error message indicates that a counter has a negative value, but it does not specify which counter is causing the problem. This makes it difficult to identify the source of the issue in large applications with multiple counters.

Suggestion:

It would be helpful if the exception message included the name of the counter that is causing the problem. This additional information would significantly improve the ability to troubleshoot issues, especially in complex applications.

zeitlinger commented 1 week ago

PR is welcome :smile: