Skip to content

MINOR Metrics: use String.intern() to reduce duplicate metric names and tags #20328

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

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
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
Expand Up @@ -41,6 +41,7 @@
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;

import static java.util.Collections.emptyList;

Expand Down Expand Up @@ -192,9 +193,13 @@ public Metrics(MetricConfig defaultConfig, List<MetricsReporter> reporters, Time
* @param tags additional key/value attributes of the metric
*/
public MetricName metricName(String name, String group, String description, Map<String, String> tags) {
Map<String, String> combinedTag = new LinkedHashMap<>(config.tags());
combinedTag.putAll(tags);
return new MetricName(name, group, description, combinedTag);
Map<String, String> combinedTag = new LinkedHashMap<>();
BiConsumer<String, String> combine = (k, v) -> {
combinedTag.put(k.intern(), v.intern());
};
config.tags().forEach(combine);
tags.forEach(combine);
return new MetricName(name.intern(), group.intern(), description, combinedTag);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
import org.apache.kafka.common.MetricName;

import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* This class encapsulates naming and mapping conventions defined as part of
Expand All @@ -37,6 +36,7 @@ public class TelemetryMetricNamingConvention {

// remove metrics as it is redundant for telemetry metrics naming convention
private static final Pattern GROUP_PATTERN = Pattern.compile("\\.(metrics)");
private static final Pattern DASH_PATTERN = Pattern.compile("-");

public static MetricNamingStrategy<MetricName> getClientTelemetryMetricNamingStrategy(String prefix) {
Objects.requireNonNull(prefix, "prefix cannot be null");
Expand All @@ -46,14 +46,17 @@ public static MetricNamingStrategy<MetricName> getClientTelemetryMetricNamingStr
public MetricKey metricKey(MetricName metricName) {
Objects.requireNonNull(metricName, "metric name cannot be null");

return new MetricKey(fullMetricName(prefix, metricName.group(), metricName.name()),
return new MetricKey(
fullMetricName(prefix, metricName.group(), metricName.name()).intern(),
Collections.unmodifiableMap(cleanTags(metricName.tags())));
}

@Override
public MetricKey derivedMetricKey(MetricKey key, String derivedComponent) {
Objects.requireNonNull(derivedComponent, "derived component cannot be null");
return new MetricKey(key.name() + NAME_JOINER + derivedComponent, key.tags());
return new MetricKey(
(key.name() + NAME_JOINER + derivedComponent).intern(),
key.tags());
}
};
}
Expand Down Expand Up @@ -111,14 +114,16 @@ private static String cleanMetric(String metric) {
* @return the new map with keys replaced by snake_case representations.
*/
private static Map<String, String> cleanTags(Map<String, String> raw) {
return raw.entrySet()
.stream()
.collect(Collectors.toMap(s -> clean(s.getKey(), TAG_JOINER), Entry::getValue));
Map<String, String> result = new HashMap<>();
raw.forEach((k, v) -> {
result.put(clean(k, TAG_JOINER).intern(), v.intern());
});
return result;
}

private static String clean(String raw, String joiner) {
Objects.requireNonNull(raw, "metric data cannot be null");
String lowerCase = raw.toLowerCase(Locale.ROOT);
return lowerCase.replaceAll("-", joiner);
return DASH_PATTERN.matcher(lowerCase).replaceAll(joiner);
}
}