Skip to main content

On This Page

How to Resolve NaN Values in Micrometer Gauges in Prometheus

2 min read
Share

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 Micrometer Gauge.Builder API 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