Skip to content

Commit

Permalink
Allow colon in Prometheus meter name; add test (#9689)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjquinno authored Jan 24, 2025
1 parent 391365e commit c39850f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, 2024 Oracle and/or its affiliates.
* Copyright (c) 2023, 2025 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -92,7 +92,7 @@ public static String normalizeNameToPrometheus(String name) {
}

// Replace non-identifier characters.
result = result.replaceAll("[^A-Za-z0-9_]", "_");
result = result.replaceAll("[^A-Za-z0-9_:]", "_");

return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
* Copyright (c) 2023, 2025 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -202,6 +202,42 @@ void testRetrievingByScope() {
endsWith(OPENMETRICS_EOF)));
}

@Test
void testMeterNameWithColon() {
Counter withColon = meterRegistry.getOrCreate(Counter.builder("c:withColon"));
withColon.increment();

Counter withoutColon = meterRegistry.getOrCreate(Counter.builder("cWithoutColon"));
withoutColon.increment(2L);

Counter withQuestionMark = meterRegistry.getOrCreate(Counter.builder("c?withQuestionMark"));
withQuestionMark.increment();

MicrometerPrometheusFormatter formatter = MicrometerPrometheusFormatter.builder(meterRegistry)
.resultMediaType(MediaTypes.APPLICATION_OPENMETRICS_TEXT)
.scopeTagName(SCOPE_TAG_NAME)
.scopeSelection(Set.of("app"))
.build();

Optional<Object> outputOpt = formatter.format();

assertThat("Formatted output",
checkAndCast(outputOpt),
allOf(containsString(scopeExpr("c:withColon_total",
"this_scope",
"app",
"1.0")),
containsString(scopeExpr("cWithoutColon_total",
"this_scope",
"app",
"2.0")),
containsString(scopeExpr("c_withQuestionMark_total",
"this_scope",
"app",
"1.0")),
endsWith(OPENMETRICS_EOF)));
}

private static String scopeExpr(String meterName, String key, String value, String suffix) {
return meterName + "{" + key + "=\"" + value + "\"} " + suffix;
}
Expand Down

0 comments on commit c39850f

Please sign in to comment.