How to Resolve NaN Values in Micrometer Gauges in Prometheus
These articles are AI-generated summaries. Please check the original sources for full details.
How to Resolve NaN Values in Micrometer Gauges in Prometheus
Micrometer Gauges expose in-memory object state as metrics, but can unexpectedly return NaN if not configured correctly. This impacts data accuracy in monitoring systems like Prometheus.
The issue arises because Micrometer uses weak references to avoid memory leaks, allowing garbage collection of monitored objects, resulting in NaN when the metric is scraped after the object is reclaimed. Maintaining strong references to the monitored object prevents premature garbage collection.
Key Insights
- Weak References: Micrometer’s default use of weak references can lead to NaN values if the observed object is garbage collected.
- Strong References: Using strong references ensures the monitored object persists, providing accurate metric values.
strongReference(true): The MicrometerGauge.BuilderAPI provides a direct option to force a strong reference, simplifying the solution.
Working Example
@Service
class FooService {
private final MeterRegistry registry;
@EventListener(ApplicationReadyEvent.class)
public void setupGauges() {
setupStrongReferenceGauge();
}
private void setupStrongReferenceGauge() {
Foo foo = new Foo(10);
Gauge.builder("foo.strong", foo, Foo::value)
.description("Foo value - strong reference (will persist)")
.strongReference(true)
.register(registry);
}
record Foo(int value) {
}
}
Practical Applications
- Spring Boot Actuator: Monitoring application state with Micrometer and exposing metrics via
/actuator/metrics. - Pitfall: Relying on default weak references for short-lived objects will result in intermittent NaN values, misleading monitoring alerts and dashboards.
References:
Continue reading
Next article
Automatic Grouping of Achievements with Workstreams
Related Content
Fix the Java-MySQL Connection Exception: Public Key Retrieval is not allowed
Learn how to resolve the 'Public Key Retrieval is not allowed' error when connecting Java applications to MySQL 8 databases, a common issue stemming from new security features.
Introduction to MyBatis Dynamic SQL
Learn how to use MyBatis Dynamic SQL to generate SQL statements from Java classes, ensuring typesafe and valid SQL syntax.
MapStruct Null Values Handling
Learn different ways to handle null values during object mapping with MapStruct, improving data integrity and application robustness.