Skip to content

Commit 852d26c

Browse files
authored
fix(dropwizard5): use locale-independent placeholder keys in GraphiteNamePattern (#2619)
`GraphiteNamePattern.extractParameters` builds its placeholder keys with `String.format("${%d}", ...)`, so under a default locale with non-ASCII digits such as ar-EG the first key is `${٠}` and `CustomLabelMapper` leaves `${0}` in the mapped name and labels (`app_requests_${0}_total` instead of `app_requests_get_total`). This builds the keys with string concatenation, which does not depend on the locale, and also covers the dropwizard module since it uses the same mapper. The new `GraphiteNamePatternTest` case runs with ar-EG as the default locale and fails without the fix. @zeitlinger Signed-off-by: Sharang Gupta <sharang@sharanggupta.dev>
1 parent aee95f5 commit 852d26c

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

‎prometheus-metrics-instrumentation-dropwizard5/src/main/java/io/prometheus/metrics/instrumentation/dropwizard5/labels/GraphiteNamePattern.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Map<String, String> extractParameters(final String metricName) {
7171
final Map<String, String> params = new HashMap<String, String>();
7272
if (matcher.find()) {
7373
for (int i = 1; i <= matcher.groupCount(); i++) {
74-
params.put(String.format("${%d}", i - 1), matcher.group(i));
74+
params.put("${" + (i - 1) + "}", matcher.group(i));
7575
}
7676
}
7777

‎prometheus-metrics-instrumentation-dropwizard5/src/test/java/io/prometheus/metrics/instrumentation/dropwizard5/labels/GraphiteNamePatternTest.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Collections;
77
import java.util.HashMap;
88
import java.util.List;
9+
import java.util.Locale;
910
import java.util.Map;
1011
import org.assertj.core.api.Assertions;
1112
import org.junit.jupiter.api.Test;
@@ -132,6 +133,19 @@ void extractParameters() {
132133
.isEqualTo(expected);
133134
}
134135

136+
@Test
137+
void extractParameters_WHEN_DefaultLocaleHasNonAsciiDigits_THEN_ShouldUseAsciiKeys() {
138+
Locale defaultLocale = Locale.getDefault();
139+
Locale.setDefault(Locale.forLanguageTag("ar-EG"));
140+
try {
141+
GraphiteNamePattern pattern = new GraphiteNamePattern("org.test.controller.*.status.*");
142+
Assertions.assertThat(pattern.extractParameters("org.test.controller.gather.status.400"))
143+
.containsOnlyKeys("${0}", "${1}");
144+
} finally {
145+
Locale.setDefault(defaultLocale);
146+
}
147+
}
148+
135149
@Test
136150
void extractParameters_WHEN_emptyStringInDottedMetricsName_THEN_ShouldReturnEmptyString() {
137151
GraphiteNamePattern pattern;

0 commit comments

Comments
 (0)