33import static org .assertj .core .api .Assertions .assertThat ;
44import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
55
6+ import java .util .HashMap ;
7+ import java .util .Map ;
68import java .util .Properties ;
79import org .junit .jupiter .api .Test ;
810import org .junitpioneer .jupiter .SetSystemProperty ;
@@ -28,10 +30,7 @@ void propertiesShouldBeLoadedFromPropertiesFile() {
2830 @ SetSystemProperty (key = "prometheus.config" , value = "nonexistent.properties" )
2931 void cantLoadPropertiesFile () {
3032 assertThatExceptionOfType (PrometheusPropertiesException .class )
31- .isThrownBy (
32- () -> {
33- PrometheusPropertiesLoader .load (new Properties ());
34- })
33+ .isThrownBy (() -> PrometheusPropertiesLoader .load (new Properties ()))
3534 .withMessage (
3635 "Failed to read Prometheus properties from nonexistent.properties:"
3736 + " nonexistent.properties" );
@@ -57,4 +56,93 @@ void externalPropertiesShouldOverridePropertiesFile() {
5756 assertThat (prometheusProperties .getExporterProperties ().getExemplarsOnAllMetricTypes ())
5857 .isFalse ();
5958 }
59+
60+ @ Test
61+ void environmentVariablesShouldConfigureMetrics () {
62+ // Simulate environment variables as they would be loaded
63+ Map <Object , Object > envVarProperties = new HashMap <>();
64+ envVarProperties .put (
65+ "io_prometheus_metrics_http_duration_seconds_histogram_classic_upper_bounds" ,
66+ ".001, .005, .01" );
67+ envVarProperties .put ("io_prometheus_metrics_exemplars_enabled" , "false" );
68+
69+ PropertySource propertySource =
70+ new PropertySource (new HashMap <>(), envVarProperties , new HashMap <>());
71+ Map <String , MetricsProperties > metricsConfigs =
72+ PrometheusPropertiesLoader .loadMetricsConfigs (propertySource );
73+
74+ assertThat (metricsConfigs .get ("http_duration_seconds" ).getHistogramClassicUpperBounds ())
75+ .hasSize (3 )
76+ .containsExactly (0.001 , 0.005 , 0.01 );
77+
78+ MetricsProperties defaultMetrics =
79+ MetricsProperties .load ("io.prometheus.metrics" , propertySource );
80+ assertThat (defaultMetrics .getExemplarsEnabled ()).isFalse ();
81+ }
82+
83+ @ Test
84+ void environmentVariablesShouldHandleSnakeCaseMetricNames () {
85+ // Simulate environment variable for metric with snake_case name
86+ Map <Object , Object > envVarProperties = new HashMap <>();
87+ envVarProperties .put ("io_prometheus_metrics_http_server_histogram_native_only" , "true" );
88+
89+ PropertySource propertySource =
90+ new PropertySource (new HashMap <>(), envVarProperties , new HashMap <>());
91+ Map <String , MetricsProperties > metricsConfigs =
92+ PrometheusPropertiesLoader .loadMetricsConfigs (propertySource );
93+
94+ assertThat (metricsConfigs .get ("http_server" ).getHistogramNativeOnly ()).isTrue ();
95+ }
96+
97+ @ Test
98+ void environmentVariablesShouldHandleMultipleSnakeCaseSegments () {
99+ // Simulate environment variable for metric with multiple snake_case segments
100+ Map <Object , Object > envVarProperties = new HashMap <>();
101+ envVarProperties .put ("io_prometheus_metrics_my_custom_metric_histogram_native_only" , "true" );
102+
103+ PropertySource propertySource =
104+ new PropertySource (new HashMap <>(), envVarProperties , new HashMap <>());
105+ Map <String , MetricsProperties > metricsConfigs =
106+ PrometheusPropertiesLoader .loadMetricsConfigs (propertySource );
107+
108+ assertThat (metricsConfigs .get ("my_custom_metric" ).getHistogramNativeOnly ()).isTrue ();
109+ }
110+
111+ @ Test
112+ void environmentVariablesShouldHandleMetricNamesContainingPropertyKeywords () {
113+ // Metric names can contain words like "summary" or "histogram"
114+ // This should not confuse the parser
115+ Map <Object , Object > envVarProperties = new HashMap <>();
116+ envVarProperties .put ("io_prometheus_metrics_my_summary_metric_histogram_native_only" , "true" );
117+ envVarProperties .put (
118+ "io_prometheus_metrics_histogram_processor_summary_quantiles" , "0.5, 0.95" );
119+
120+ PropertySource propertySource =
121+ new PropertySource (new HashMap <>(), envVarProperties , new HashMap <>());
122+ Map <String , MetricsProperties > metricsConfigs =
123+ PrometheusPropertiesLoader .loadMetricsConfigs (propertySource );
124+
125+ assertThat (metricsConfigs .get ("my_summary_metric" ).getHistogramNativeOnly ()).isTrue ();
126+ assertThat (metricsConfigs .get ("histogram_processor" ).getSummaryQuantiles ())
127+ .containsExactly (0.5 , 0.95 );
128+ }
129+
130+ @ Test
131+ void regularPropertiesShouldHandleComplexMetricNames () {
132+ // Test that suffix-based matching works correctly for regular properties
133+ // Metric names already use underscores (exposition format)
134+ Properties properties = new Properties ();
135+ properties .setProperty (
136+ "io.prometheus.metrics.http_server_requests_total.histogram_native_only" , "true" );
137+ properties .setProperty (
138+ "io.prometheus.metrics.my_app_custom_metric.summary_quantiles" , "0.5, 0.99" );
139+
140+ PropertySource propertySource = new PropertySource (properties );
141+ Map <String , MetricsProperties > metricsConfigs =
142+ PrometheusPropertiesLoader .loadMetricsConfigs (propertySource );
143+
144+ assertThat (metricsConfigs .get ("http_server_requests_total" ).getHistogramNativeOnly ()).isTrue ();
145+ assertThat (metricsConfigs .get ("my_app_custom_metric" ).getSummaryQuantiles ())
146+ .containsExactly (0.5 , 0.99 );
147+ }
60148}
0 commit comments