diff --git a/implementations/micrometer-registry-prometheus/src/main/java/io/micrometer/prometheusmetrics/PrometheusMeterRegistry.java b/implementations/micrometer-registry-prometheus/src/main/java/io/micrometer/prometheusmetrics/PrometheusMeterRegistry.java index 28b109abea..2f47eb1167 100644 --- a/implementations/micrometer-registry-prometheus/src/main/java/io/micrometer/prometheusmetrics/PrometheusMeterRegistry.java +++ b/implementations/micrometer-registry-prometheus/src/main/java/io/micrometer/prometheusmetrics/PrometheusMeterRegistry.java @@ -15,7 +15,6 @@ */ package io.micrometer.prometheusmetrics; -import io.micrometer.common.lang.NonNull; import io.micrometer.common.lang.Nullable; import io.micrometer.core.instrument.*; import io.micrometer.core.instrument.cumulative.CumulativeFunctionCounter; @@ -536,20 +535,9 @@ private MetricMetadata getMetadata(Meter.Id id) { private MetricMetadata getMetadata(Meter.Id id, String suffix) { String name = config().namingConvention().name(id.getName(), id.getType(), id.getBaseUnit()) + suffix; - String sanitizedName = sanitize(name); String help = prometheusConfig.descriptions() ? Optional.ofNullable(id.getDescription()).orElse(" ") : " "; Unit unit = id.getBaseUnit() != null ? new Unit(id.getBaseUnit()) : null; - return new MetricMetadata(sanitizedName, help, unit); - } - - private String sanitize(@NonNull String name) { - if (name.endsWith("_info") || name.endsWith("_total") || name.endsWith("_created") - || name.endsWith("_bucket")) { - return name.substring(0, name.lastIndexOf('_')); - } - else { - return name; - } + return new MetricMetadata(name, help, unit); } private void applyToCollector(Meter.Id id, Consumer consumer) { diff --git a/implementations/micrometer-registry-prometheus/src/main/java/io/micrometer/prometheusmetrics/PrometheusNamingConvention.java b/implementations/micrometer-registry-prometheus/src/main/java/io/micrometer/prometheusmetrics/PrometheusNamingConvention.java index 1f96185800..9460c8b84b 100644 --- a/implementations/micrometer-registry-prometheus/src/main/java/io/micrometer/prometheusmetrics/PrometheusNamingConvention.java +++ b/implementations/micrometer-registry-prometheus/src/main/java/io/micrometer/prometheusmetrics/PrometheusNamingConvention.java @@ -18,8 +18,7 @@ import io.micrometer.common.lang.Nullable; import io.micrometer.core.instrument.Meter; import io.micrometer.core.instrument.config.NamingConvention; - -import java.util.regex.Pattern; +import io.prometheus.metrics.model.snapshots.PrometheusNaming; /** * See https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels for a @@ -29,10 +28,6 @@ */ public class PrometheusNamingConvention implements NamingConvention { - private static final Pattern nameChars = Pattern.compile("[^a-zA-Z0-9_:]"); - - private static final Pattern tagKeyChars = Pattern.compile("[^a-zA-Z0-9_]"); - private final String timerSuffix; public PrometheusNamingConvention() { @@ -45,9 +40,6 @@ public PrometheusNamingConvention(String timerSuffix) { /** * Names are snake-cased. They contain a base unit suffix when applicable. - *

- * Names may contain ASCII letters and digits, as well as underscores and colons. They - * must match the regex [a-zA-Z_:][a-zA-Z0-9_:]* */ @Override public String name(String name, Meter.Type type, @Nullable String baseUnit) { @@ -81,11 +73,7 @@ else if (!conventionName.endsWith("_seconds")) { break; } - String sanitized = nameChars.matcher(conventionName).replaceAll("_"); - if (!Character.isLetter(sanitized.charAt(0))) { - sanitized = "m_" + sanitized; - } - return sanitized; + return PrometheusNaming.sanitizeMetricName(conventionName); } /** @@ -95,13 +83,7 @@ else if (!conventionName.endsWith("_seconds")) { */ @Override public String tagKey(String key) { - String conventionKey = NamingConvention.snakeCase.tagKey(key); - - String sanitized = tagKeyChars.matcher(conventionKey).replaceAll("_"); - if (!Character.isLetter(sanitized.charAt(0))) { - sanitized = "m_" + sanitized; - } - return sanitized; + return PrometheusNaming.sanitizeLabelName(key); } } diff --git a/implementations/micrometer-registry-prometheus/src/test/java/io/micrometer/prometheusmetrics/PrometheusNamingConventionTest.java b/implementations/micrometer-registry-prometheus/src/test/java/io/micrometer/prometheusmetrics/PrometheusNamingConventionTest.java index 8ba28bee9e..34934b8949 100644 --- a/implementations/micrometer-registry-prometheus/src/test/java/io/micrometer/prometheusmetrics/PrometheusNamingConventionTest.java +++ b/implementations/micrometer-registry-prometheus/src/test/java/io/micrometer/prometheusmetrics/PrometheusNamingConventionTest.java @@ -30,12 +30,12 @@ class PrometheusNamingConventionTest { @Test void formatName() { - assertThat(convention.name("123abc/{:id}水", Meter.Type.GAUGE)).startsWith("m_123abc__:id__"); + assertThat(convention.name("123abc/{:id}水", Meter.Type.GAUGE)).startsWith("_23abc__:id__"); } @Test void formatTagKey() { - assertThat(convention.tagKey("123abc/{:id}水")).startsWith("m_123abc___id__"); + assertThat(convention.tagKey("123abc/{:id}水")).startsWith("_23abc___id__"); } @Test