From 00f45385294653b0e4045b517fb8f082a5830d5f Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 20 Jan 2016 11:06:58 +0000 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20check=20that=20a=20Gauge?= =?UTF-8?q?=E2=80=99s=20value=20is=20a=20Number=20until=20it=E2=80=99s=20b?= =?UTF-8?q?eing=20read?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Spring Boot’s metrics require all values to be Numbers. A Dropwizard Gauge can have a non-Number value. Previously, to prevent this causing a problem, MetricRegistryMetricReader would check the value of a Gauge when it’s being added and ignore it if it had a non-Number value. Unfortunately, retrieving the value of a Gauge can take a non-trivial amount of time (hence CachedGauge) so this approach, while functional, could be improved. This commit updates the filtering to happen when a Metric is being retrieved from MetricRegistryMetricReader (via findOne or findAll) when its value is required anyway. At this point, any Gauge with a non-Number value is ignored. Closes gh-4874 --- .../reader/MetricRegistryMetricReader.java | 21 +++++++++---------- .../MetricRegistryMetricReaderTests.java | 2 +- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/reader/MetricRegistryMetricReader.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/reader/MetricRegistryMetricReader.java index b5c97de6ee3..5034f3fd8da 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/reader/MetricRegistryMetricReader.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/reader/MetricRegistryMetricReader.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2015 the original author or authors. + * Copyright 2013-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -87,9 +87,15 @@ public class MetricRegistryMetricReader implements MetricReader, MetricRegistryL return new Metric(metricName, counter.getCount()); } if (metric instanceof Gauge) { - @SuppressWarnings("unchecked") - Gauge value = (Gauge) metric; - return new Metric(metricName, value.getValue()); + Object value = ((Gauge) metric).getValue(); + if (value instanceof Number) { + return new Metric(metricName, (Number) value); + } + if (logger.isDebugEnabled()) { + logger.debug("Ignoring gauge '" + name + "' (" + metric + + ") as its value is not a Number"); + } + return null; } if (metric instanceof Sampling) { if (metricName.contains(".snapshot.")) { @@ -129,13 +135,6 @@ public class MetricRegistryMetricReader implements MetricReader, MetricRegistryL @Override public void onGaugeAdded(String name, Gauge gauge) { - if (!(gauge.getValue() instanceof Number)) { - if (logger.isDebugEnabled()) { - logger.debug("Ignoring gauge '" + name + "' (" + gauge - + ") as its value is not a Number"); - } - return; - } this.names.put(name, name); synchronized (this.monitor) { this.reverse.add(name, name); diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/reader/MetricRegistryMetricReaderTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/reader/MetricRegistryMetricReaderTests.java index 9caadbd0299..077549d98cf 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/reader/MetricRegistryMetricReaderTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/reader/MetricRegistryMetricReaderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.