diff --git a/extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/Metrics.java b/extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/Metrics.java index 50c235538be..4b9ad716cdf 100644 --- a/extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/Metrics.java +++ b/extensions-contrib/prometheus-emitter/src/main/java/org/apache/druid/emitter/prometheus/Metrics.java @@ -64,7 +64,7 @@ public class Metrics public Metrics(String namespace, String path, boolean isAddHostAsLabel, boolean isAddServiceAsLabel, Map extraLabels) { - Map registeredMetrics = new HashMap<>(); + Map parsedRegisteredMetrics = new HashMap<>(); Map metrics = readConfig(path); if (extraLabels == null) { @@ -116,10 +116,10 @@ public class Metrics } if (collector != null) { - registeredMetrics.put(name, new DimensionsAndCollector(dimensions, collector, metric.conversionFactor)); + parsedRegisteredMetrics.put(name, new DimensionsAndCollector(dimensions, collector, metric.conversionFactor)); } } - this.registeredMetrics = Collections.unmodifiableMap(registeredMetrics); + this.registeredMetrics = Collections.unmodifiableMap(parsedRegisteredMetrics); } private Map readConfig(String path) diff --git a/extensions-contrib/prometheus-emitter/src/test/java/org/apache/druid/emitter/prometheus/MetricsTest.java b/extensions-contrib/prometheus-emitter/src/test/java/org/apache/druid/emitter/prometheus/MetricsTest.java index e37e7888d21..4567c7cd069 100644 --- a/extensions-contrib/prometheus-emitter/src/test/java/org/apache/druid/emitter/prometheus/MetricsTest.java +++ b/extensions-contrib/prometheus-emitter/src/test/java/org/apache/druid/emitter/prometheus/MetricsTest.java @@ -20,6 +20,7 @@ package org.apache.druid.emitter.prometheus; import io.prometheus.client.Histogram; +import org.apache.druid.java.util.common.ISE; import org.junit.Assert; import org.junit.Test; @@ -93,4 +94,20 @@ public class MetricsTest Assert.assertTrue(actualMessage.contains(expectedMessage)); } + + @Test + public void testMetricsConfigurationWithNonExistentMetric() + { + Metrics metrics = new Metrics("test_4", null, true, true, null); + DimensionsAndCollector nonExistentDimsCollector = metrics.getByName("non/existent", "historical"); + Assert.assertNull(nonExistentDimsCollector); + } + + @Test + public void testMetricsConfigurationWithUnSupportedType() + { + Assert.assertThrows(ISE.class, () -> { + new Metrics("test_5", "src/test/resources/defaultMetricsTest.json", true, true, null); + }); + } } diff --git a/extensions-contrib/prometheus-emitter/src/test/resources/defaultMetricsTest.json b/extensions-contrib/prometheus-emitter/src/test/resources/defaultMetricsTest.json new file mode 100644 index 00000000000..a63e2b45470 --- /dev/null +++ b/extensions-contrib/prometheus-emitter/src/test/resources/defaultMetricsTest.json @@ -0,0 +1,3 @@ +{ + "query/nonExistent" : { "dimensions" : ["dataSource"], "type" : "nonExistent", "help": "Non supported type."} +}