Skip to content

Return NaN for null KafkaMetrics values #6324

@ArneKlein

Description

@ArneKlein

Describe the bug
toDouble() can potentially throw a null pointer exception, as it's only checking for the metric to exist.

Unfortunately, Kafka 3.9.1 returns a metric for addOldestOpenIteratorGauge that can have null as a metric value, leading to a NPE in our code when trying to read the metric. As the gauge exists but has a null value.

java.lang.NullPointerException: Cannot invoke "java.lang.Number.doubleValue()" because the return value of "org.apache.kafka.common.Metric.metricValue()" is null
at kafka.KafkaMetrics.toDouble(KafkaMetrics.java:326) ~[micrometer-core-1.14.7-jakartaee.jar:1.14.7-migrated-1.0.8]
at kafka.KafkaMetrics.lambda$toMetricValue$6(KafkaMetrics.java:322) ~[micrometer-core-1.14.7-jakartaee.jar:1.14.7-migrated-1.0.8]

Environment

  • Micrometer version: 1.14.7
  • Micrometer registry: PushMeterRegistry
  • OS: Ubuntu
  • Java version: 17

To Reproduce
https://github.com/ArneKlein/micrometer-npe-example will create the metric, but I couldn't get a simple enough example for reproducing the issue with a push meter registry yet

Expected behavior
toDouble returns NaN instead of null for missing metric values

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions