Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
[branch-2.10] Use a more efficient implementation of sanitizeMetricNa…
Browse files Browse the repository at this point in the history
…me (#1654)

Fixes #1652

### Motivation

Pulsar 2.10 or earlier depends on io.prometheus:simpleclient:0.5.0,
whose implementation of `Collectors.sanitizeMetricName` uses regex,
which is inefficient and costs much CPU.

### Modifications

Migrate the implementation of `sanitizeMetricName` from
io:prometheus:simpleclient:0.16.0 to branch-2.10 or earlier.
  • Loading branch information
BewareMyPower authored Dec 30, 2022
1 parent d7287cd commit 5302f3f
Showing 1 changed file with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package io.streamnative.pulsar.handlers.kop.stats;

import com.google.common.base.Joiner;
import io.prometheus.client.Collector;
import java.util.Map;
import java.util.TreeMap;
import org.apache.bookkeeper.stats.Counter;
Expand Down Expand Up @@ -78,6 +77,22 @@ private ScopeContext scopeContext(String name) {
}

private String completeName(String name) {
return Collector.sanitizeMetricName(scope.isEmpty() ? name : Joiner.on('_').join(scope, name));
return sanitizeMetricName(scope.isEmpty() ? name : Joiner.on('_').join(scope, name));
}

private static String sanitizeMetricName(String metricName) {
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] = '_';
} else {
sanitized[i] = ch;
}
}

return new String(sanitized);
}
}

0 comments on commit 5302f3f

Please sign in to comment.