Skip to content

Commit

Permalink
Fix PrometheusNaming.sanitizeMetricName() methods (#975)
Browse files Browse the repository at this point in the history
Signed-off-by: Mickael Maison <mickael.maison@gmail.com>
  • Loading branch information
mimaison authored Sep 9, 2024
1 parent 8a24bde commit 72b79d4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public static String sanitizeMetricName(String metricName) {
* as a suffix if the unit is not {@code null}.
*/
public static String sanitizeMetricName(String metricName, Unit unit) {
String result = sanitizeLabelName(metricName);
String result = sanitizeMetricName(metricName);
if (unit != null) {
if (!result.endsWith("_" + unit) && !result.endsWith("." + unit)) {
result += "_" + unit;
Expand Down Expand Up @@ -224,8 +224,7 @@ private static String replaceIllegalCharsInMetricName(String name) {
char[] sanitized = new char[length];
for (int i = 0; i < length; i++) {
char ch = name.charAt(i);
if (ch == ':' ||
ch == '.' ||
if (ch == '.' ||
(ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ||
(i > 0 && ch >= '0' && ch <= '9')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class PrometheusNamingTest {
@Test
public void testSanitizeMetricName() {
Assert.assertEquals("_abc_def", prometheusName(sanitizeMetricName("0abc.def")));
Assert.assertEquals("___ab_:c0", prometheusName(sanitizeMetricName("___ab.:c0")));
Assert.assertEquals("___ab__c0", prometheusName(sanitizeMetricName("___ab.:c0")));
Assert.assertEquals("my_prefix_my_metric", sanitizeMetricName("my_prefix/my_metric"));
Assert.assertEquals("my_counter", prometheusName(sanitizeMetricName("my_counter_total")));
Assert.assertEquals("jvm", sanitizeMetricName("jvm.info"));
Expand All @@ -21,6 +21,20 @@ public void testSanitizeMetricName() {
Assert.assertEquals("total", sanitizeMetricName("total"));
}

@Test
public void testSanitizeMetricNameWithUnit() {
Assert.assertEquals("_abc_def_" + Unit.RATIO, prometheusName(sanitizeMetricName("0abc.def", Unit.RATIO)));
Assert.assertEquals("___ab__c0_" + Unit.RATIO, prometheusName(sanitizeMetricName("___ab.:c0", Unit.RATIO)));
Assert.assertEquals("my_prefix_my_metric_" + Unit.RATIO, sanitizeMetricName("my_prefix/my_metric", Unit.RATIO));
Assert.assertEquals("my_counter_" + Unit.RATIO, prometheusName(sanitizeMetricName("my_counter_total", Unit.RATIO)));
Assert.assertEquals("jvm_" + Unit.RATIO, sanitizeMetricName("jvm.info", Unit.RATIO));
Assert.assertEquals("jvm_" + Unit.RATIO, sanitizeMetricName("jvm_info", Unit.RATIO));
Assert.assertEquals("jvm_" + Unit.RATIO, sanitizeMetricName("jvm.info", Unit.RATIO));
Assert.assertEquals("a.b_" + Unit.RATIO, sanitizeMetricName("a.b", Unit.RATIO));
Assert.assertEquals("total_" + Unit.RATIO, sanitizeMetricName("_total", Unit.RATIO));
Assert.assertEquals("total_" + Unit.RATIO, sanitizeMetricName("total", Unit.RATIO));
}

@Test
public void testSanitizeLabelName() {
Assert.assertEquals("_abc_def", prometheusName(sanitizeLabelName("0abc.def")));
Expand Down

0 comments on commit 72b79d4

Please sign in to comment.