Skip to content

Commit 4b6e78f

Browse files
jonatan-ivanovfstab
authored andcommitted
Add load(Map) overload to PrometheusPropertiesLoader
Signed-off-by: Jonatan Ivanov <[email protected]>
1 parent eb9a77f commit 4b6e78f

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

prometheus-metrics-config/src/main/java/io/prometheus/metrics/config/PrometheusPropertiesLoader.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ public class PrometheusPropertiesLoader {
2525
* See {@link PrometheusProperties#get()}.
2626
*/
2727
public static PrometheusProperties load() throws PrometheusPropertiesException {
28-
Map<Object, Object> properties = loadProperties();
28+
return load(new Properties());
29+
}
30+
31+
public static PrometheusProperties load(Map<Object, Object> externalProperties) throws PrometheusPropertiesException {
32+
Map<Object, Object> properties = loadProperties(externalProperties);
2933
Map<String, MetricsProperties> metricsConfigs = loadMetricsConfigs(properties);
3034
MetricsProperties defaultMetricsProperties = MetricsProperties.load("io.prometheus.metrics", properties);
3135
ExemplarsProperties exemplarConfig = ExemplarsProperties.load("io.prometheus.exemplars", properties);
@@ -72,11 +76,12 @@ private static void validateAllPropertiesProcessed(Map<Object, Object> propertie
7276
}
7377
}
7478

75-
private static Map<Object, Object> loadProperties() {
79+
private static Map<Object, Object> loadProperties(Map<Object, Object> externalProperties) {
7680
Map<Object, Object> properties = new HashMap<>();
7781
properties.putAll(loadPropertiesFromClasspath());
7882
properties.putAll(loadPropertiesFromFile()); // overriding the entries from the classpath file
7983
properties.putAll(System.getProperties()); // overriding the entries from the properties file
84+
properties.putAll(externalProperties); // overriding all the entries above
8085
// TODO: Add environment variables like EXEMPLARS_ENABLED.
8186
return properties;
8287
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.prometheus.metrics.config;
2+
3+
import java.util.Properties;
4+
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
/**
9+
* Tests for {@link PrometheusPropertiesLoader}.
10+
*/
11+
public class PrometheusPropertiesLoaderTests {
12+
13+
@Test
14+
public void propertiesShouldBeLoadedFromPropertiesFile() {
15+
PrometheusProperties prometheusProperties = PrometheusPropertiesLoader.load();
16+
Assert.assertEquals(11, prometheusProperties.getDefaultMetricProperties().getHistogramClassicUpperBounds().size());
17+
Assert.assertEquals(4, prometheusProperties.getMetricProperties("http_duration_seconds").getHistogramClassicUpperBounds().size());
18+
Assert.assertTrue(prometheusProperties.getExporterProperties().getExemplarsOnAllMetricTypes());
19+
}
20+
21+
@Test
22+
public void externalPropertiesShouldOverridePropertiesFile() {
23+
Properties properties = new Properties();
24+
properties.setProperty("io.prometheus.metrics.histogramClassicUpperBounds", ".005, .01");
25+
properties.setProperty("io.prometheus.metrics.http_duration_seconds.histogramClassicUpperBounds", ".005, .01, .015");
26+
properties.setProperty("io.prometheus.exporter.exemplarsOnAllMetricTypes", "false");
27+
28+
PrometheusProperties prometheusProperties = PrometheusPropertiesLoader.load(properties);
29+
Assert.assertEquals(2, prometheusProperties.getDefaultMetricProperties().getHistogramClassicUpperBounds().size());
30+
Assert.assertEquals(3, prometheusProperties.getMetricProperties("http_duration_seconds").getHistogramClassicUpperBounds().size());
31+
Assert.assertFalse(prometheusProperties.getExporterProperties().getExemplarsOnAllMetricTypes());
32+
}
33+
}

0 commit comments

Comments
 (0)