Skip to content

Commit 8f8ed36

Browse files
committed
fix
Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com>
1 parent cb5eb56 commit 8f8ed36

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

prometheus-metrics-config/src/test/java/io/prometheus/metrics/config/PrometheusPropertiesTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,62 @@ void testBuilder() {
7070
assertThat(result.getMetricProperties("unknown_metric")).isNull();
7171
assertThat(result.getExporterProperties()).isSameAs(defaults.getExporterProperties());
7272
}
73+
74+
@Test
75+
void testMetricNameNormalization() {
76+
PrometheusProperties.Builder builder = PrometheusProperties.builder();
77+
MetricsProperties customProps =
78+
MetricsProperties.builder().histogramClassicUpperBounds(0.1, 0.5).build();
79+
80+
// Test that metric names with dots are normalized to underscores
81+
builder.putMetricProperty("my.metric.name", customProps);
82+
PrometheusProperties result = builder.build();
83+
84+
// Should be able to retrieve with dots
85+
assertThat(result.getMetricProperties("my.metric.name")).isSameAs(customProps);
86+
// Should also be able to retrieve with underscores
87+
assertThat(result.getMetricProperties("my_metric_name")).isSameAs(customProps);
88+
}
89+
90+
@Test
91+
void testMetricNameWithInvalidCharacters() {
92+
PrometheusProperties.Builder builder = PrometheusProperties.builder();
93+
MetricsProperties customProps =
94+
MetricsProperties.builder().histogramClassicUpperBounds(0.1, 0.5).build();
95+
96+
// Test that invalid characters are converted to underscores
97+
builder.putMetricProperty("metric-name@with#invalid$chars", customProps);
98+
PrometheusProperties result = builder.build();
99+
100+
// Should normalize invalid characters to underscores
101+
assertThat(result.getMetricProperties("metric-name@with#invalid$chars")).isSameAs(customProps);
102+
assertThat(result.getMetricProperties("metric_name_with_invalid_chars")).isSameAs(customProps);
103+
}
104+
105+
@Test
106+
void testMetricNameWithValidCharacters() {
107+
PrometheusProperties.Builder builder = PrometheusProperties.builder();
108+
MetricsProperties customProps =
109+
MetricsProperties.builder().histogramClassicUpperBounds(0.1, 0.5).build();
110+
111+
// Test valid characters: letters, numbers (not at start), underscore, colon
112+
builder.putMetricProperty("my_metric:name123", customProps);
113+
PrometheusProperties result = builder.build();
114+
115+
assertThat(result.getMetricProperties("my_metric:name123")).isSameAs(customProps);
116+
}
117+
118+
@Test
119+
void testMetricNameStartingWithNumber() {
120+
PrometheusProperties.Builder builder = PrometheusProperties.builder();
121+
MetricsProperties customProps =
122+
MetricsProperties.builder().histogramClassicUpperBounds(0.1, 0.5).build();
123+
124+
// First digit is invalid (i=0), but subsequent digits are valid (i>0)
125+
builder.putMetricProperty("123metric", customProps);
126+
PrometheusProperties result = builder.build();
127+
128+
assertThat(result.getMetricProperties("123metric")).isSameAs(customProps);
129+
assertThat(result.getMetricProperties("_23metric")).isSameAs(customProps);
130+
}
73131
}

0 commit comments

Comments
 (0)