11package io .prometheus .metrics .config ;
22
3+ import javax .annotation .Nullable ;
34import java .util .HashMap ;
45import java .util .Map ;
5- import javax .annotation .Nullable ;
66
77/**
88 * The Prometheus Java client library can be configured at runtime (e.g. using a properties file).
@@ -14,14 +14,51 @@ public class PrometheusProperties {
1414 private static final PrometheusProperties instance = PrometheusPropertiesLoader .load ();
1515
1616 private final MetricsProperties defaultMetricsProperties ;
17- private final Map < String , MetricsProperties > metricProperties = new HashMap <>() ;
17+ private final MetricPropertiesMap metricProperties ;
1818 private final ExemplarsProperties exemplarProperties ;
1919 private final ExporterProperties exporterProperties ;
2020 private final ExporterFilterProperties exporterFilterProperties ;
2121 private final ExporterHttpServerProperties exporterHttpServerProperties ;
2222 private final ExporterOpenTelemetryProperties exporterOpenTelemetryProperties ;
2323 private final ExporterPushgatewayProperties exporterPushgatewayProperties ;
2424
25+ /**
26+ * Map that stores metric-specific properties keyed by metric name in exposition format
27+ * (underscores instead of dots).
28+ *
29+ * <p>This wrapper makes it explicit that metric names are normalized to underscore format for
30+ * storage, matching how they appear in Prometheus exposition formats.
31+ */
32+ static class MetricPropertiesMap {
33+ private final Map <String , MetricsProperties > map = new HashMap <>();
34+
35+ void putAll (Map <String , MetricsProperties > properties ) {
36+ properties .forEach (this ::put );
37+ }
38+
39+ void put (String metricName , MetricsProperties properties ) {
40+ map .put (normalize (metricName ), properties );
41+ }
42+
43+ /**
44+ * Get metric properties by metric name.
45+ *
46+ * <p>Accepts metric names in any format (with dots or underscores) and automatically converts
47+ * them to the normalized underscore format used for storage.
48+ *
49+ * @param metricName the metric name (dots will be converted to underscores)
50+ * @return the metric properties, or null if not configured
51+ */
52+ @ Nullable
53+ MetricsProperties get (String metricName ) {
54+ return map .get (normalize (metricName ));
55+ }
56+
57+ private static String normalize (String metricName ) {
58+ return metricName .replace ("." , "_" );
59+ }
60+ }
61+
2562 /**
2663 * Get the properties instance. When called for the first time, {@code get()} loads the properties
2764 * from the following locations:
@@ -41,17 +78,18 @@ public static Builder builder() {
4178 return new Builder ();
4279 }
4380
44- public PrometheusProperties (
81+ // Package-private constructor for PrometheusPropertiesLoader and Builder
82+ PrometheusProperties (
4583 MetricsProperties defaultMetricsProperties ,
46- Map < String , MetricsProperties > metricProperties ,
84+ MetricPropertiesMap metricProperties ,
4785 ExemplarsProperties exemplarProperties ,
4886 ExporterProperties exporterProperties ,
4987 ExporterFilterProperties exporterFilterProperties ,
5088 ExporterHttpServerProperties httpServerConfig ,
5189 ExporterPushgatewayProperties pushgatewayProperties ,
5290 ExporterOpenTelemetryProperties otelConfig ) {
5391 this .defaultMetricsProperties = defaultMetricsProperties ;
54- this .metricProperties . putAll ( metricProperties ) ;
92+ this .metricProperties = metricProperties ;
5593 this .exemplarProperties = exemplarProperties ;
5694 this .exporterProperties = exporterProperties ;
5795 this .exporterFilterProperties = exporterFilterProperties ;
@@ -72,10 +110,13 @@ public MetricsProperties getDefaultMetricProperties() {
72110 * Properties specific for one metric. Should be merged with {@link
73111 * #getDefaultMetricProperties()}. May return {@code null} if no metric-specific properties are
74112 * configured for a metric name.
113+ *
114+ * @param metricName the metric name (dots will be automatically converted to underscores to match
115+ * exposition format)
75116 */
76117 @ Nullable
77118 public MetricsProperties getMetricProperties (String metricName ) {
78- return metricProperties .get (metricName . replace ( "." , "_" ) );
119+ return metricProperties .get (metricName );
79120 }
80121
81122 public ExemplarsProperties getExemplarProperties () {
@@ -104,7 +145,7 @@ public ExporterOpenTelemetryProperties getExporterOpenTelemetryProperties() {
104145
105146 public static class Builder {
106147 private MetricsProperties defaultMetricsProperties = MetricsProperties .builder ().build ();
107- private Map < String , MetricsProperties > metricProperties = new HashMap <> ();
148+ private final MetricPropertiesMap metricProperties = new MetricPropertiesMap ();
108149 private ExemplarsProperties exemplarProperties = ExemplarsProperties .builder ().build ();
109150 private ExporterProperties exporterProperties = ExporterProperties .builder ().build ();
110151 private ExporterFilterProperties exporterFilterProperties =
@@ -124,7 +165,7 @@ public Builder defaultMetricsProperties(MetricsProperties defaultMetricsProperti
124165 }
125166
126167 public Builder metricProperties (Map <String , MetricsProperties > metricProperties ) {
127- this .metricProperties = metricProperties ;
168+ this .metricProperties . putAll ( metricProperties ) ;
128169 return this ;
129170 }
130171
0 commit comments