diff --git a/clients/src/main/java/org/apache/kafka/common/metrics/Metrics.java b/clients/src/main/java/org/apache/kafka/common/metrics/Metrics.java index 398819016cb5b..a7581442f5d12 100644 --- a/clients/src/main/java/org/apache/kafka/common/metrics/Metrics.java +++ b/clients/src/main/java/org/apache/kafka/common/metrics/Metrics.java @@ -502,6 +502,7 @@ public void addMetric(MetricName metricName, MetricConfig config, Measurable mea * * @param metricName The name of the metric * @param metricValueProvider The metric value provider associated with this metric + * @throws IllegalArgumentException if a metric with same name already exists. */ public void addMetric(MetricName metricName, MetricConfig config, MetricValueProvider metricValueProvider) { KafkaMetric m = new KafkaMetric(new Object(), @@ -587,18 +588,19 @@ public synchronized void removeReporter(MetricsReporter reporter) { } /** - * Register a metric if not present or return an already existing metric otherwise. + * Register a metric if not present or return the already existing metric with the same name. * When a metric is newly registered, this method returns null * * @param metric The KafkaMetric to register - * @return KafkaMetric if the metric already exists, null otherwise + * @return the existing metric with the same name or null */ synchronized KafkaMetric registerMetric(KafkaMetric metric) { MetricName metricName = metric.metricName(); - if (this.metrics.containsKey(metricName)) { - return this.metrics.get(metricName); + KafkaMetric existingMetric = this.metrics.putIfAbsent(metricName, metric); + if (existingMetric != null) { + return existingMetric; } - this.metrics.put(metricName, metric); + // newly added metric for (MetricsReporter reporter : reporters) { try { reporter.metricChange(metric); diff --git a/docs/upgrade.html b/docs/upgrade.html index b29259aa2b8b0..498944646c85e 100644 --- a/docs/upgrade.html +++ b/docs/upgrade.html @@ -19,6 +19,15 @@