Skip to content

Commit

Permalink
Use PrometheusNaming in PrometheusNamingConvention
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatan-ivanov committed Mar 22, 2024
1 parent c727411 commit 8e05395
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<MicrometerCollector> consumer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand All @@ -45,9 +40,6 @@ public PrometheusNamingConvention(String timerSuffix) {

/**
* Names are snake-cased. They contain a base unit suffix when applicable.
* <p>
* 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) {
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8e05395

Please sign in to comment.