-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-13846: Adding overloaded addMetricIfAbsent method #12121
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
Changes from all commits
43b6a31
9a49629
d3ea1fc
233a173
a653023
24387ce
4da9f48
a58f8dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -509,7 +509,10 @@ public void addMetric(MetricName metricName, MetricConfig config, MetricValuePro | |
| Objects.requireNonNull(metricValueProvider), | ||
| config == null ? this.config : config, | ||
| time); | ||
| registerMetric(m); | ||
| KafkaMetric existingMetric = registerMetric(m); | ||
| if (existingMetric != null) { | ||
| throw new IllegalArgumentException("A metric named '" + metricName + "' already exists, can't register another one."); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -524,6 +527,26 @@ public void addMetric(MetricName metricName, MetricValueProvider<?> metricValueP | |
| addMetric(metricName, null, metricValueProvider); | ||
| } | ||
|
|
||
| /** | ||
|
vamossagar12 marked this conversation as resolved.
Outdated
|
||
| * 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 addMetricIfAbsent(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 existingMetric = registerMetric(metric); | ||
| return existingMetric == null ? metric : existingMetric; | ||
| } | ||
|
|
||
| /** | ||
| * Remove a metric if it exists and return it. Return null otherwise. If a metric is removed, `metricRemoval` | ||
| * will be invoked for each reporter. | ||
|
|
@@ -563,10 +586,18 @@ public synchronized void removeReporter(MetricsReporter reporter) { | |
| } | ||
| } | ||
|
|
||
| synchronized void registerMetric(KafkaMetric metric) { | ||
| /** | ||
| * Register a metric if not present or return an already existing metric otherwise. | ||
| * 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: we can just say "the existing metric with the same name" |
||
| */ | ||
| synchronized KafkaMetric registerMetric(KafkaMetric metric) { | ||
|
vamossagar12 marked this conversation as resolved.
Outdated
|
||
| MetricName metricName = metric.metricName(); | ||
| if (this.metrics.containsKey(metricName)) | ||
| throw new IllegalArgumentException("A metric named '" + metricName + "' already exists, can't register another one."); | ||
| if (this.metrics.containsKey(metricName)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not efficient to do
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or even better, call |
||
| return this.metrics.get(metricName); | ||
| } | ||
| this.metrics.put(metricName, metric); | ||
| for (MetricsReporter reporter : reporters) { | ||
| try { | ||
|
|
@@ -576,6 +607,7 @@ synchronized void registerMetric(KafkaMetric metric) { | |
| } | ||
| } | ||
| log.trace("Registered metric named {}", metricName); | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we clarify in the javadoc above that if the metric already exists, an exception would be thrown?