Versions Compared

Key

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

...

Please keep the discussion on the mailing list rather than commenting on the wiki (wiki discussions get unwieldy fast).

Motivation

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. But this is very brittle as some metrics reporters cause issues when instantiated multiple times. This is the case for example with CruiseControlMetricsReporter. 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 plugins in Kafka Connect. For example MirrorMaker2 creates its the source and checkpoint MirrorMaker2 connectors create their own Metrics object objects and has have logic to add the metric reporters from the configuration.

In this proposal, a "plugin" is an interface users can implement and that is instantiated by Kafka. For example, a class implementing org.apache.kafka.server.policy.CreateTopicPolicy is considered a plugin as it's instantiated by brokers. On the other hand a class implementing org.apache.kafka.clients.producer.Callback is not considered a plugin as it's instantiated in user logic.

...

Code Block
languagejava
titleMonitorable.java
package org.apache.kafka.common.metrics;

public interface Monitorable {

    /**
     * SetProvides thea PluginMetrics instance from the component that instantiates the plugin.
     * PluginsPluginMetrics can registerbe andused unregisterby metricsthe using the given PluginMetricsplugin to register and unregister metrics
     * at any point in their lifecycle prior to their close method being called.
     * Any metrics registered will be automatically removed when the plugin is closed.
     */
    void withPluginMetrics(PluginMetrics metrics);

}

...

The PluginMetrics interface has methods to add and remove metrics and sensors. Plugins will only be able to remove metrics and sensors they created. Metrics created via this class will have their group set to "plugins" and include tags that uniquely identify the plugin.

Code Block
languagejava
/**
 * Implementations are thread safe so plugins may use the PluginMetrics instance from multiple threads
 */
public interface PluginMetrics extends Closeable {public interface PluginMetrics extends Closeable {

    /**
     * Create a {@link MetricName} with the given name, description and tags. The group will be set to "plugins"
     * Tags to uniquely identify the plugins are automatically added to the provided tags
     *
     * @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
     */
    MetricName metricName(String name, String description, Map<String, String> tags);

    /**
     * Add a metric to monitor an object that implements {@link 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.
     */
    void addMetric(MetricName metricName, MetricValueProvider<?> metricValueProvider);

    /**
     * Remove a metric if it exists.
     *
     * @param metricName The name of the metric
     * @throws IllegalArgumentException if the plugin has not already created a metric with this name
     */
    void removeMetric(MetricName metricName);

    /**
     * Create a sensor with the given unique name. The name must only be unique for the plugin, so different plugins can use the same names.
     *
     * @param name The sensor name
     * @return The sensor
     * @throws IllegalArgumentException if a sensor with same name already exists for this plugin
     */
    Sensor sensor(String name);

    /**
     * Remove a sensor (if it exists) and its associated metrics.
     *
     * @param name The name of the sensor to be removed
     * @throws IllegalArgumentException if the plugin has not already created a sensor with this name
     */
    void removeSensor(String name);
}

The PluginMetrics interface implements Closeable, and the close() method deletes all the metrics the instance has created. It is automatically closed and when the associated instance is closed by the component that owns it.

...

When instantiating a class, if it implements Monitorable, the withPluginMetricsKafka components will wrap the instance in an internal container class. If the instance implements Monitorable, an PluginMetrics object will be created with the right tags and added to the container class and the withPluginMetrics() method will be called with that object. If the class is also Configurable, withPluginMetrics() 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. Tags will be added to metrics and sensors tags created via the PluginMetrics interface to unique identify each instance. When the component closes the plugin instance, the associated PluginMetrics will also be closed so all metrics registered by the plugin are deleted.

For all plugins apart from Connectors, Tasks, Converters, Transformations and Predicates, a tag containing the configuration name (config) and the plugin class name (class) will be added as tags. For example, metrics registered by a custom Serializer named MySerializer configured via key.serializer will have the following name: kafka.producer:type=plugins,client-id=producer-1,config=key.serializer,class=MySerializer
For configurations that accept a list of classes, for example interceptor.classes, if the same class is provided multiple times, their metrics may collide. This is deemed highly unlikely to occur as there are no use cases where providing multiple times the same class is useful.

For Connectors and Converters, the name of the connector will be added as a tag (connector). Tasks will add For example: kafka.connect:type=plugins,connector=my-sink
For Tasks, the name of the connector (connector) and the task id (task) will be added as tags. For example: kafka.connect:type=plugins,connector=my-sink,task=0
For Transformations and Predicates will have the connector name, the name of the connector (connector), the task id (task) and their alias (transformation/predicate) will be added as tags. For example:

...

kafka.connect:type=plugins,connector=my-sink,task=0

...

,predicate=my-predicate
For Converters, the name of the connector (connector), the task id (task), and their type (iskey, either set to true or false) will be added as tags. For example: kafka.connect:type=plugins,connector=my-sink,task=0,

...

iskey=true

KIP-608

This proposal supersedes KIP-608 which only aimed at providing this functionality to Authorizer plugins. KIP-608 was adopted in 2020 but was never implemented. This KIP proposes to alter the names of the metrics from KIP-608 to the following so all plugins use the same naming format:

...

The goal is allow this feature to be used by all plugins that are Closeable, AutoCloseable or have a close() methods apart from MetricsReporter since instances are created before the Metrics instance. Also all Connect connector plugins will be supported. The javadoc of all supported plugins will be updated to mention they are able to implement the Monitorable interface to define their own metrics.

...

The only Closeable interface Streams supports today is SerdesSerde. Users can instantiate their own Serdes instance so Streams does not control the lifecycle of instances. For that reason this proposal ignores Streams plugins.

...

If we decide that some of these plugins would benefit from being able to emit metrics, we could make the necessary API changes in the future to support them.

Example usage

For example if we can create a custom ProducerInterceptor that track how many records it intercepts:

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

    private Sensor sensor;

    public void withPluginMetrics(PluginMetrics metrics) {
        this.metrics = 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;
    }
}

...

This is a new feature so it has no impact on deprecationcompatibility. The only significant API change is for Converter that will now be Closeable. The default close() method should ensure that existing implementations still work.

...