Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize Collector.sanitizeMetricName #777

Merged
merged 1 commit into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.prometheus.client.benchmark;

import com.codahale.metrics.MetricRegistry;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.openjdk.jmh.runner.options.TimeValue;

import io.prometheus.client.Collector;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@State(Scope.Benchmark)
public class SanitizeMetricNameBenchmark {

@Benchmark
@BenchmarkMode({ Mode.AverageTime })
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public void sanitizeSanitizedName() {
Collector.sanitizeMetricName("good_name");
}

@Benchmark
@BenchmarkMode({ Mode.AverageTime })
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public void sanitizeNonSanitizedName() {
Collector.sanitizeMetricName("9not_good_name!");
}

public static void main(String[] args) throws RunnerException {

Options opt = new OptionsBuilder()
.include(SanitizeMetricNameBenchmark.class.getSimpleName())
.warmupIterations(5)
.measurementIterations(4)
.measurementTime(TimeValue.seconds(1))
.warmupTime(TimeValue.seconds(1))
.threads(4)
.forks(1)
.build();

new Runner(opt).run();
}
}
20 changes: 14 additions & 6 deletions simpleclient/src/main/java/io/prometheus/client/Collector.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,16 +352,24 @@ protected static void checkMetricName(String name) {
}
}

private static final Pattern SANITIZE_PREFIX_PATTERN = Pattern.compile("^[^a-zA-Z_:]");
private static final Pattern SANITIZE_BODY_PATTERN = Pattern.compile("[^a-zA-Z0-9_:]");

/**
* Sanitize metric name
*/
public static String sanitizeMetricName(String metricName) {
return SANITIZE_BODY_PATTERN.matcher(
SANITIZE_PREFIX_PATTERN.matcher(metricName).replaceFirst("_")
).replaceAll("_");
int length = metricName.length();
char[] sanitized = new char[length];
for(int i = 0; i < length; i++) {
char ch = metricName.charAt(i);
if(ch == ':' ||
(ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ||
(i > 0 && ch >= '0' && ch <= '9')) {
sanitized[i] = ch;
} else {
sanitized[i] = '_';
}
}
return new String(sanitized);
}

/**
Expand Down
26 changes: 26 additions & 0 deletions simpleclient/src/test/java/io/prometheus/client/CollectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,32 @@
import static org.junit.Assert.*;

public class CollectorTest {

@Test
public void sanitizeMetricPrefix() throws Exception {
assertEquals("afoo", Collector.sanitizeMetricName("afoo"));
assertEquals("zfoo", Collector.sanitizeMetricName("zfoo"));
assertEquals("Afoo", Collector.sanitizeMetricName("Afoo"));
assertEquals("Zfoo", Collector.sanitizeMetricName("Zfoo"));
assertEquals(":foo", Collector.sanitizeMetricName(":foo"));

assertEquals("_foo", Collector.sanitizeMetricName("0foo"));
assertEquals("_foo", Collector.sanitizeMetricName("5foo"));
assertEquals("_foo", Collector.sanitizeMetricName("9foo"));
assertEquals("_foo", Collector.sanitizeMetricName("/foo"));
assertEquals("_foo", Collector.sanitizeMetricName("*foo"));
}

@Test
public void sanitizeMetricBody() throws Exception {
assertEquals("aamzAMZ059", Collector.sanitizeMetricName("aamzAMZ059"));
assertEquals("aaMzAmZ009", Collector.sanitizeMetricName("aaMzAmZ009"));
assertEquals("aZmA950aMz", Collector.sanitizeMetricName("aZmA950aMz"));
assertEquals("aZ9mA0a5Mz", Collector.sanitizeMetricName("aZ9mA0a5Mz"));
assertEquals("aZ9mA_0a5Mz", Collector.sanitizeMetricName("aZ9mA*0a5Mz"));
assertEquals("aZ9mA_0a5Mz", Collector.sanitizeMetricName("aZ9mA&0a5Mz"));
}

@Test
public void sanitizeMetricName() throws Exception {
assertEquals("_hoge", Collector.sanitizeMetricName("0hoge"));
Expand Down