guozhangwang commented on code in PR #12121: URL: https://github.com/apache/kafka/pull/12121#discussion_r878742212
########## clients/src/main/java/org/apache/kafka/common/metrics/Metrics.java: ########## @@ -524,6 +527,26 @@ public void addMetric(MetricName metricName, MetricValueProvider<?> metricValueP addMetric(metricName, null, metricValueProvider); } + /** + * Create or get an existing metric to monitor an object that implements MetricValueProvider. + * This metric won't be associated with any sensor. This is a way to expose existing values as metrics. + * This method takes care of synchronisation while updating/accessing metrics by concurrent threads. + * + * @param metricName The name of the metric + * @param metricValueProvider The metric value provider associated with this metric + * @return Existing KafkaMetric if already registered or else a newly created one + */ + public KafkaMetric metricOrElseCreate(MetricName metricName, MetricConfig config, MetricValueProvider<?> metricValueProvider) { + KafkaMetric metric = new KafkaMetric(new Object(), + Objects.requireNonNull(metricName), + Objects.requireNonNull(metricValueProvider), + config == null ? this.config : config, + time); + + KafkaMetric maybeMetric = registerMetric(metric); Review Comment: nit: `maybeMetric` => existingMetric? ########## clients/src/main/java/org/apache/kafka/common/metrics/Metrics.java: ########## @@ -563,10 +586,11 @@ public synchronized void removeReporter(MetricsReporter reporter) { } } - synchronized void registerMetric(KafkaMetric metric) { + synchronized KafkaMetric registerMetric(KafkaMetric metric) { Review Comment: Could we also add a javadoc for this function, on its return value semantics? -- 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-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org