[Prometheus Emitter] Add to code coverage and remove code smell (#17362)

* [Prometheus Emitter] Add to code coverage and remove code smell
This commit is contained in:
Ashwin Tumma 2024-10-16 22:19:16 -07:00 committed by GitHub
parent 26e2ca66d7
commit 90175b8927
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 3 deletions

View File

@ -64,7 +64,7 @@ public class Metrics
public Metrics(String namespace, String path, boolean isAddHostAsLabel, boolean isAddServiceAsLabel, Map<String, String> extraLabels)
{
Map<String, DimensionsAndCollector> registeredMetrics = new HashMap<>();
Map<String, DimensionsAndCollector> parsedRegisteredMetrics = new HashMap<>();
Map<String, Metric> 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<String, Metric> readConfig(String path)

View File

@ -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);
});
}
}

View File

@ -0,0 +1,3 @@
{
"query/nonExistent" : { "dimensions" : ["dataSource"], "type" : "nonExistent", "help": "Non supported type."}
}