You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "LiamClarkeNZ (via GitHub)" <gi...@apache.org> on 2025/10/04 04:03:57 UTC
Re: [PR] MINOR Metrics: use String.intern() to reduce duplicate metric names and tags [kafka]
LiamClarkeNZ commented on code in PR #20328:
URL: https://github.com/apache/kafka/pull/20328#discussion_r2403719873
##########
clients/src/main/java/org/apache/kafka/common/metrics/Metrics.java:
##########
@@ -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);
Review Comment:
I'm wondering if you need to intern at this level as they're (IIUC) later going to be replaced by cleaned tags, so presumably should be available for GC afterwards?
##########
clients/src/main/java/org/apache/kafka/common/telemetry/internals/TelemetryMetricNamingConvention.java:
##########
@@ -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);
Review Comment:
Out of curiosity, what prompted this switch from string method to regex?
##########
clients/src/main/java/org/apache/kafka/common/metrics/Metrics.java:
##########
@@ -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);
Review Comment:
Likewise with interning name and group as they create the full metric name later.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org