diff --git a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/PrometheusNaming.java b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/PrometheusNaming.java index 06e004398..21c162632 100644 --- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/PrometheusNaming.java +++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/PrometheusNaming.java @@ -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; @@ -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')) { diff --git a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/PrometheusNamingTest.java b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/PrometheusNamingTest.java index 3a7788743..9631f9df3 100644 --- a/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/PrometheusNamingTest.java +++ b/prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/snapshots/PrometheusNamingTest.java @@ -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")); @@ -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")));