Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Kafka exposes many pluggable API for users to bring their custom plugins. For complex and critical plugins it's important to have metrics to monitor their behavior. Plugins wanting to emit metrics can use the Metrics class from the Kafka API but when creating a new Metrics instance it does not inherits the tags from the component it depends on (for example from a producer for a custom partitioner), or the registered metrics reporters. As most plugins are configurable, a workaround is to reimplement the metric reporters logic and in some case for tags too but that is cumbersome. Also by creating a separate Metrics instance, these metrics are separate from the client's and in case multiple clients are running in the same JVM, for example multiple producers, it can be hard to identify the specific client that is associated with some plugin metrics.

This issue also applies to connectors and tasks in Kafka Connect. For example MirrorMaker2 creates its own Metrics object and has logic to add the metric reporters from the configuration.

Public Interfaces

...

For client and server side plugins, I propose introducing a new interface: Monitorable. If a plugin implements this interface, the withMetricswithPluginMetrics() method will be called when the plugin is instantiated (after configure() if the plugin also implements Configurable). This will allow the plugin to adds its own metrics to the existing Metrics instance from the component (producer, consumer, etc) that instantiated it.

Code Block
languagejava
titleMonitorable.java

package org.apache.kafka.common.metrics;

public interface Monitorable {

    /**
     * Get the MetricsPluginMetrics instance from the client that instantiates the plugin.
     */
    void withMetricswithPluginMetrics(MetricsPluginMetrics Metricsmetrics);

}

Connectors/Tasks

For connectors and tasks, I propose adding a metrics() method to the SinkConnectorContext, SourceConnectorContext, SinkTaskContext and SourceTaskContext interfacesThe PluginMetrics class has methods to add and remove metrics and sensors. It will forward calls to the underlying Metrics instance. Plugins will only be able to remove metrics they created. Metrics created via this class will have their group set to the class name of the plugin.

Code Block
languagejava
public class PluginMetrics {

    /**
     * Create a PluginMetric instance for plugins to register metrics
     *
     * @param metrics The underlying Metrics repository to use for metrics
     * Retrieve the Metrics instance from the worker running this connector/task. Returns null if the runtime does not support this feature.
 */
default Metrics metrics() {
    return null; @param className The class name of the plugin
     */
    public PluginMetrics(Metrics metrics, String className) {}

    /**
     * Create a MetricName with the given name, description and tags. The plugin class name will be used as the metric group.
     *
     * @param name        The name of the metric
     * @param description A human-readable description to include in the metric
     * @param tags        additional key/value attributes of the metric
     */
    public MetricName metricName(String name, String description, Map<String, String> tags) {}

    /**
     * Add a 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.
     *
     * @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 synchronized void addMetric(MetricName metricName, MetricValueProvider<?> metricValueProvider) {}

    /**
     * Remove a metric if it exists and return it. Return null otherwise.
     *
     * @param metricName The name of the metric
     * @return the removed KafkaMetric or null if no such metric exists
     */
    public synchronized KafkaMetric removeMetric(MetricName metricName) {}

    /**
     * Get or create a sensor with the given unique name.
     *
     * @param name The sensor name
     * @return The sensor
     */
    public synchronized Sensor sensor(String name) {}

    /**
     * Remove a sensor (if it exists) and its associated metrics.
     *
     * @param name The name of the sensor to be removed
     */
    public synchronized void removeSensor(String name) {}

    /**
     * Delete all metrics and sensors registered by this plugin
     */
    void close() {}
}

Proposed Changes

When instantiating a class via the Utils.newInstance() helper methods, if it implements Monitorable and a Metrics object is available, withMetrics() a new PluginMetrics instance will be called with the current Metrics instancecreated and passed to the withPluginMetrics() method. It will be always called after configure().  Metrics registered by plugins will inherit the prefix/namespace from the current Metrics instance, these are: kafka.producer, kafka.consumer, kafka.connect, kafka.streams and kafka.server. Metrics reporters should not implement the Monitorable interface as they are created before the Metrics instance.


Example usage

For example if we create a custom ProducerInterceptor

Code Block
languagejava
public class MyInterceptor<K, V> implements ProducerInterceptor<K, V>, Monitorable {

    private Sensor sensor;

    public void setPluginMetrics(PluginMetrics metrics) {
        sensor = metrics.sensor("onSend");
        MetricName rate = metrics.metricName("rate", "Average number of calls per second.", Collections.emptyMap());
        MetricName total = metrics.metricName("total", "Total number of calls.", Collections.emptyMap());
        sensor.add(rate, new Rate());
        sensor.add(total, new CumulativeCount());
    }

    @Override
    public ProducerRecord<K, V> onSend(ProducerRecord<K, V> record) {
        sensor.record();
        return record;
    }
    
    ...
}

If the producer using this plugin has its client-id set to producer-1, the metrics created by this plugin will have the following name: kafka.producer:type=MyInterceptor,client-id=producer-1 and these attributes: rate and totalMirrorSourceConnector and MirrorCheckpointConnector currently register metrics using the kafka.connect.mirror prefix/namespace. If we update them with this proposal, their metrics will be renamed, for example from kafka.connect.mirror:type=MirrorSourceConnector to kafka.connect:type=MirrorSourceConnector. This KIP will not update these 2 connectors.

Compatibility, Deprecation, and Migration Plan

This is a new feature so it has no impact on deprecation and does not need a migration plan. Regarding compatibility, plugins and connectors that start using this feature will have to handle Metrics not being available to support older broker/client/Connect versions. For regular plugins Plugins they should be able to function without a call to withMetrics(), for connectors and tasks they should handle the metrics() method returning null when deployed on an older runtimeeven if their withPluginMetrics() method is not called.

Test Plan

This feature will be tested using unit and integration tests.

...

  • Create a dedicated Metrics instance for plugins: A dedicated instance could have its own prefix/namespace (for example kafka.consumer.plugins). This would allow grouping metrics from all plugins but it requires instantiating another Metrics instance and new metrics reporters.
  • Let plugins create their own Metrics instance: Instead of passing the Metrics instance to plugins we could pass all the values necessary (metrics reporters, configs, etc ...) to create and configure a Metrics instance. This is impractical as it requires passing a lot of values around and plugins still have to have logic to use them.Create a simpler PluginMetrics API: Instead of passing Metrics, a simpler API could be easier for plugins. It could prevent plugins calling close() or removeMetrics() but since plugins execute code, administrators should only use plugins they trust.
  • Provide the Metrics instance to Kafka Connect Connectors and Tasks via their context: Using a different mechanism that could introduce compatibility issues. Connectors and Tasks also