gharris1727 commented on code in PR #17511:
URL: https://github.com/apache/kafka/pull/17511#discussion_r1847407838


##########
clients/src/main/java/org/apache/kafka/common/metrics/PluginMetrics.java:
##########
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.common.metrics;
+
+import org.apache.kafka.common.MetricName;
+
+import java.util.Map;
+
+/**
+ * This allows plugins to register metrics and sensors.
+ * Any metrics registered by the plugin is automatically removed when the 
plugin is closed.

Review Comment:
   ```suggestion
    * Any metrics registered by the plugin are automatically removed when the 
plugin is closed.
   ```



##########
clients/src/main/java/org/apache/kafka/common/internals/Plugin.java:
##########
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.common.internals;
+
+import org.apache.kafka.common.metrics.Metrics;
+import org.apache.kafka.common.metrics.Monitorable;
+import org.apache.kafka.common.metrics.internals.PluginMetricsImpl;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.function.Supplier;
+
+public class Plugin<T> implements Supplier<T>, AutoCloseable {
+
+    private final T instance;
+    private final Optional<PluginMetricsImpl> pluginMetrics;
+
+    private Plugin(T instance, PluginMetricsImpl pluginMetrics) {
+        this.instance = instance;
+        this.pluginMetrics = Optional.ofNullable(pluginMetrics);
+    }
+
+    public static <T> Plugin<T> wrapInstance(T instance, Metrics metrics, 
String key) {
+        return wrapInstance(instance, metrics, () -> tags(key, instance));
+    }
+
+    private static <T> Map<String, String> tags(String key, T instance) {
+        Map<String, String> tags = new LinkedHashMap<>();
+        tags.put("config", key);
+        tags.put("class", instance.getClass().getSimpleName());
+        return tags;
+    }
+
+    public static <T> List<Plugin<T>> wrapInstances(List<T> instances, Metrics 
metrics, String key) {
+        List<Plugin<T>> plugins = new ArrayList<>();
+        for (T instance : instances) {
+            plugins.add(wrapInstance(instance, metrics, key));
+        }
+        return plugins;
+    }
+
+    public static <T> Plugin<T> wrapInstance(T instance, Metrics metrics, 
Supplier<Map<String, String>> tagsSupplier) {
+        PluginMetricsImpl pluginMetrics = null;
+        if (instance instanceof Monitorable && metrics != null) {
+            pluginMetrics = new PluginMetricsImpl(metrics, tagsSupplier.get());
+            ((Monitorable) instance).withPluginMetrics(pluginMetrics);
+        }
+        return new Plugin<>(instance, pluginMetrics);
+    }
+
+    @Override
+    public T get() {
+        return instance;
+    }
+
+    @Override
+    public void close() throws Exception {

Review Comment:
   The close order here has some trade-offs.
   
   If we close metrics first, the plugin close() method can't ever emit 
metrics. I can think of some useful examples that we should permit:
   * is a close operation ongoing
   * how many records were processed during the instance lifetime
   * how long was the plugin open
   * how long did operation X take during closing
   
   Also if there are background threads within a plugin, those threads may want 
to emit metrics concurrent with the close() call. The close() call may leave 
that background thread running, intentionally or accidentally. I think that 
once the close() call completes, we can throw away any further metrics from the 
plugin, making all of the calls no-ops, or maybe throw exceptions if we want to 
be harsh.
   
   If we close the plugin first, the MetricValueProvider interface may have an 
undefined value during and after closing. If the plugin needs to be open in 
order to provide real data, they MetricValueProvider instance may throw an 
exception, or provide junk or stale data.
   
   I think we should close the plugin first, and mention that the 
MetricValueProviders may be called at any time, including after the metric is 
removed or the plugin is closed.
   
   



##########
clients/src/main/java/org/apache/kafka/common/internals/Plugin.java:
##########
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.common.internals;
+
+import org.apache.kafka.common.metrics.Metrics;
+import org.apache.kafka.common.metrics.Monitorable;
+import org.apache.kafka.common.metrics.internals.PluginMetricsImpl;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.function.Supplier;
+
+public class Plugin<T> implements Supplier<T>, AutoCloseable {
+
+    private final T instance;
+    private final Optional<PluginMetricsImpl> pluginMetrics;
+
+    private Plugin(T instance, PluginMetricsImpl pluginMetrics) {
+        this.instance = instance;
+        this.pluginMetrics = Optional.ofNullable(pluginMetrics);
+    }
+
+    public static <T> Plugin<T> wrapInstance(T instance, Metrics metrics, 
String key) {
+        return wrapInstance(instance, metrics, () -> tags(key, instance));
+    }
+
+    private static <T> Map<String, String> tags(String key, T instance) {
+        Map<String, String> tags = new LinkedHashMap<>();
+        tags.put("config", key);
+        tags.put("class", instance.getClass().getSimpleName());
+        return tags;
+    }
+
+    public static <T> List<Plugin<T>> wrapInstances(List<T> instances, Metrics 
metrics, String key) {
+        List<Plugin<T>> plugins = new ArrayList<>();
+        for (T instance : instances) {
+            plugins.add(wrapInstance(instance, metrics, key));
+        }
+        return plugins;
+    }
+
+    public static <T> Plugin<T> wrapInstance(T instance, Metrics metrics, 
Supplier<Map<String, String>> tagsSupplier) {
+        PluginMetricsImpl pluginMetrics = null;
+        if (instance instanceof Monitorable && metrics != null) {
+            pluginMetrics = new PluginMetricsImpl(metrics, tagsSupplier.get());
+            ((Monitorable) instance).withPluginMetrics(pluginMetrics);
+        }
+        return new Plugin<>(instance, pluginMetrics);
+    }
+
+    @Override
+    public T get() {
+        return instance;
+    }
+
+    @Override
+    public void close() throws Exception {
+        if (pluginMetrics.isPresent()) pluginMetrics.get().close();

Review Comment:
   Is there a way for the plugin metrics to throw on close? If so, the instance 
itself wouldn't get closed, leaking it.
   
   I think propagating the exception from the actual instance close is a good 
idea.



##########
clients/src/main/java/org/apache/kafka/common/metrics/internals/PluginMetricsImpl.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.common.metrics.internals;
+
+import org.apache.kafka.common.MetricName;
+import org.apache.kafka.common.metrics.MetricValueProvider;
+import org.apache.kafka.common.metrics.Metrics;
+import org.apache.kafka.common.metrics.PluginMetrics;
+import org.apache.kafka.common.metrics.Sensor;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+
+public class PluginMetricsImpl implements PluginMetrics, Closeable {
+
+    private static final String GROUP = "plugins";
+
+    private final Metrics metrics;
+    private final Map<String, String> tags;
+    private final Set<MetricName> metricNames = new HashSet<>();
+    private final Set<String> sensors = new HashSet<>();
+
+    public PluginMetricsImpl(Metrics metrics, Map<String, String> tags) {
+        this.metrics = metrics;
+        this.tags = tags;
+    }
+
+    @Override
+    public MetricName metricName(String name, String description, Map<String, 
String> tags) {
+        for (String tagName : tags.keySet()) {
+            if (this.tags.containsKey(tagName)) {
+                throw new IllegalArgumentException("Cannot use " + tagName + " 
as a tag name");
+            }
+        }
+        Map<String, String> metricsTags = new LinkedHashMap<>(this.tags);
+        metricsTags.putAll(tags);
+        return metrics.metricName(name, GROUP, description, metricsTags);
+    }
+
+    @Override
+    public void addMetric(MetricName metricName, MetricValueProvider<?> 
metricValueProvider) {
+        if (metricNames.contains(metricName)) {
+            throw new IllegalArgumentException("Metric " + metricName + " 
already exists");
+        }
+        metrics.addMetric(metricName, metricValueProvider);
+        metricNames.add(metricName);
+    }
+
+    @Override
+    public void removeMetric(MetricName metricName) {
+        if (metricNames.contains(metricName)) {
+            metrics.removeMetric(metricName);
+            metricNames.remove(metricName);
+        } else {
+            throw new IllegalArgumentException("Unknown metric " + metricName);
+        }
+    }
+
+    @Override
+    public Sensor addSensor(String name) {
+        if (sensors.contains(name)) {
+            throw new IllegalArgumentException("Sensor " + name + " already 
exists");
+        }
+        Sensor sensor = metrics.sensor(name);
+        sensors.add(name);
+        return sensor;
+    }
+
+    @Override
+    public void removeSensor(String name) {
+        if (sensors.contains(name)) {
+            metrics.removeSensor(name);
+            sensors.remove(name);
+        } else {
+            throw new IllegalArgumentException("Unknown sensor " + name);
+        }
+    }
+
+    @Override
+    public void close() throws IOException {

Review Comment:
   What if additional metrics are added or removed after close() is called?



-- 
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

Reply via email to