apoorvmittal10 commented on code in PR #14575:
URL: https://github.com/apache/kafka/pull/14575#discussion_r1364651878


##########
clients/src/main/java/org/apache/kafka/common/telemetry/collector/MetricsCollector.java:
##########
@@ -0,0 +1,87 @@
+/*
+ * 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.telemetry.collector;
+
+import org.apache.kafka.common.annotation.InterfaceStability;
+import org.apache.kafka.common.telemetry.emitter.Emitter;
+
+/**
+ * A {@code MetricsCollector} is responsible for scraping a source of metrics 
and forwarding
+ * them to the given {@link Emitter}. For example, a given collector might be 
used to collect
+ * system metrics, Kafka metrics, JVM metrics, or other metrics that are to be 
captured, exposed,
+ * and/or forwarded.
+ *
+ * <p/>
+ *
+ * In general, a {@code MetricsCollector} implementation is closely managed by 
another entity
+ * (that entity is colloquially referred to as the "telemetry reporter") that 
will be in
+ * charge of its lifecycle via the {@link #start()} and {@link #stop()} 
methods. The telemetry
+ * reporter should ensure that the {@link #start()} method is invoked <i>once 
and only once</i>
+ * before calls to {@link #collect(Emitter)} are made. Implementations of 
{@code MetricsCollector}
+ * should allow for the corner-case that {@link #stop()} is called before 
{@link #start()},
+ * which might happen in the case of error on startup of the telemetry 
reporter.
+ *
+ * <p/>
+ *
+ * Regarding threading, the {@link #start()} and {@link #stop()} methods may 
be called from
+ * different threads and so proper care should be taken by implementations of 
the
+ * {@code MetricsCollector} interface to be thread-safe. However, the 
telemetry reporter must
+ * ensure that the {@link #collect(Emitter)} method should only be invoked in 
a synchronous
+ * manner.
+ *
+ * @see Emitter
+ */
[email protected]
+public interface MetricsCollector {
+
+    /**
+     * The {@code collect} method is called by the telemetry reporter to 
retrieve the value
+     * of its desired set of metrics, and then forward those on to the provided
+     * {@link Emitter}. The implementation may choose to collect all the 
metrics before forwarding
+     * them to the {@code emitter}, or they may be forwarded as they are 
collected.
+     *
+     * <p/>
+     *
+     * In general, the implementation should try not to presume the 
characteristics of the
+     * {@link Emitter} so as to keep a loose coupling.
+     *
+     * @param emitter {@link Emitter} to which the metric values will be 
passed once collected
+     */
+    void collect(Emitter emitter);
+
+    /**
+     * Allows the {@code MetricsCollector} implementation to initialize 
itself. This method should
+     * be invoked by the telemetry reporter before calls to {@link 
#collect(Emitter)} are made. The
+     * telemetry reporter should not invoke this method more than once.
+     */
+    default void start() {
+        // Do nothing...
+    }
+
+    /**
+     * Allows the {@code MetricsCollector} implementation to stop itself and 
dispose of any resources.
+     * This method should ideally be invoked only once by the telemetry 
reporter.
+     *
+     * <p/>
+     *
+     * Calls to {@link #collect(Emitter)} once this method has been invoked 
should be expected to
+     * fail by the telemetry reporter; it should take caution to handle that 
case.

Review Comment:
   No you didn't write it, I copied it from some other place ;) I ll log an 
error and will stop proceeding, do you think we should capture that here in 
docs or should not write this line itself?



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to