From 1c3776b5e700b6182f900bb2c49458d4890d665f Mon Sep 17 00:00:00 2001 From: Jorge Esteban Quilcate Otoya Date: Sun, 8 Mar 2020 22:28:10 +0100 Subject: [PATCH] refactor: simplify currentMeters ref --- .../instrument/binder/kafka/KafkaMetrics.java | 5 ++--- .../binder/kafka/KafkaMetricsTest.java | 21 ++++++++++++------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/kafka/KafkaMetrics.java b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/kafka/KafkaMetrics.java index 12de7ef684..853b42a81d 100644 --- a/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/kafka/KafkaMetrics.java +++ b/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/kafka/KafkaMetrics.java @@ -113,7 +113,7 @@ void checkAndBindMetrics(MeterRegistry registry) { synchronized (this) { //Enforce only happens once when metrics change if (!currentMeters.equals(metrics.keySet())) { //Register meters - currentMeters = new HashSet<>(); + currentMeters = new HashSet<>(metrics.keySet()); metrics.forEach((name, metric) -> { //Filter out metrics from group "app-info", that includes metadata if (METRIC_GROUP_APP_INFO.equals(name.group())) return; @@ -124,7 +124,7 @@ void checkAndBindMetrics(MeterRegistry registry) { for (Meter meter : meters) { if (meter.getId().getTags().size() < (metricTags(metric).size() + extraTagsSize)) registry.remove(meter); - // if already exists + // Check if already exists else if (meter.getId().getTags().equals(metricTags(metric))) return; else hasLessTags = true; } @@ -132,7 +132,6 @@ void checkAndBindMetrics(MeterRegistry registry) { //Filter out non-numeric values if (!isNumber(metric)) return; bindMeter(registry, metric); - currentMeters.add(name); }); } } diff --git a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/kafka/KafkaMetricsTest.java b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/kafka/KafkaMetricsTest.java index 3ca6e3a600..678e5a9ea1 100644 --- a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/kafka/KafkaMetricsTest.java +++ b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/kafka/KafkaMetricsTest.java @@ -21,6 +21,7 @@ import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; import org.apache.kafka.common.Metric; import org.apache.kafka.common.MetricName; @@ -57,13 +58,14 @@ class KafkaMetricsTest { @Test void shouldAddNewMetersWhenMetricsChange() { //Given - Map metrics = new LinkedHashMap<>(); - Supplier> supplier = () -> { + AtomicReference> metrics = new AtomicReference<>(); + metrics.set(new LinkedHashMap<>()); + Supplier> supplier = () -> metrics.updateAndGet(map -> { MetricName metricName = new MetricName("a0", "b0", "c0", new LinkedHashMap<>()); KafkaMetric metric = new KafkaMetric(this, metricName, new Value(), new MetricConfig(), Time.SYSTEM); - metrics.put(metricName, metric); - return metrics; - }; + map.put(metricName, metric); + return map; + }); KafkaMetrics kafkaMetrics = new KafkaMetrics(supplier); MeterRegistry registry = new SimpleMeterRegistry(); //When @@ -71,9 +73,12 @@ class KafkaMetricsTest { //Then assertThat(registry.getMeters()).hasSize(1); //Given - MetricName metricName = new MetricName("a1", "b1", "c1", new LinkedHashMap<>()); - KafkaMetric metric = new KafkaMetric(this, metricName, new Value(), new MetricConfig(), Time.SYSTEM); - metrics.put(metricName, metric); + metrics.updateAndGet(map -> { + MetricName metricName = new MetricName("a1", "b1", "c1", new LinkedHashMap<>()); + KafkaMetric metric = new KafkaMetric(this, metricName, new Value(), new MetricConfig(), Time.SYSTEM); + map.put(metricName, metric); + return map; + }); //When kafkaMetrics.checkAndBindMetrics(registry); //Then