Versions Compared

Key

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

...

The stock metrics reporter implementation – JmxReporter already gets passed some additional context via the JMX prefix (e.g. kafka.server kafka.streams, kafa.admin.client, etc.) at instantiation. However, this information is not available to additional metrics reporters configured at startup.

...

Code Block
/**
 * MetricsContext encapsulates additional metadata about metrics exposed via a
 * {@link org.apache.kafka.common.metrics.MetricsReporter}
 * 
 * The metadata map provides following information:
 * - a <code>_namespace</node> field indicating the component exposing metrics
 *   e.g. kafka.server, kafka.consumer
 *   {@link JmxReporter} uses this as prefix for mbean names
 *
 * - for clients and streams libraries: any freeform fields passed in via
 *   client properties in the form of `metrics.context.<key>=<value>
 *
 * - for kafka brokers: kafka.broker.id, kafka.cluster.id
 * - for connect workers: connect.kafka.cluster.id, connect.group.id
 */
@InterfaceStability.Evolving
public interface MetricsContext {

    /* predefined fields */
    String NAMESPACE = "_namespace"; // metrics namespace, formerly jmx prefix
    
    /**
     * Returns metadata fields
     */
    public Map<String, String> metadata();
}

Connect, Streams and Client configuration changes

A new configuration prefix will be introduced for connect, streams and client configs, so applications can pass context metadata to embedded client libraries.

metrics.context.<key>=<value>

Connect and streams will pass on their context configuration to the clients they instantiate. Similar to other client configurations, explicit client config overrides would take precedence over values passed through from the main config.

The configured key value pairs are then passed by the clients to the configured metrics reporters via the MetricsContext.

For instance, configuring a client with metrics.context.foo.bar=baz would add the field foo.bar mapped to the value baz in the MetricsContext metadata the client metrics reporter.

JmxReporter changes

The JmxReporter(String prefix) constructor will be deprecated in favor of the default constructor.

The prefix will be passed to the reporter via the metrics context instead. An internal metric context field _namespace will hold the existing jmx prefix values.

By convention, pre-defined metric context fields that are guaranteed to be present would be prefixed with an underscore.

Proposed Changes

Remove the special status of JmxReporter

Existing usages of JmxReporter(String prefix) will be removed and replaced with the default constructor, in line with what we do for metric reporter plugins defined via configuration.

The JmxReporter will give precedence to the _namespace field from the MetricsContext as JMX prefix over the prefix field passed in via the deprecated constructor.

All components and libraries currently instantiating metrics reporters will define the necessary metric context and add the _namespace

...

 field to the metrics context.

Client and Streams libraries

In addition to the above, clients and streams libraries will read in additional metrics.context.<field>=<value> entries passed in via configurationand add them to the metric context before calling contextChange(MetricsContext metricsContext).

Kafka Brokers

Kafka brokers will pass in fully resolved fields kafka.broker.id and kafka.cluster.id to the metric context.

Connect Workers

Connect workers will pass in connect.kafka.cluster.id to indicate the Kafka cluster used in distributed mode, as well as connect.group.id to indicate the group id used for worker coordination, and also propagate this information to the its clients.

Compatibility, Deprecation, and Migration Plan

contextChange(MetricsContext metricsContext) will be added as a default method to MetricsReporter to support backward compatibility with existing implementations. No changes are required for existing metric reporters since we are not changing any other behavior.

Rejected Alternatives

  • Add additional metadata to the tags defined in MetricsConfig
    MetricsConfig tags are tightly coupled to MetricNameTemplate objects, and require metric names to provide placeholders for all the tags, which make it difficult to add new tags without changing the metric names.

  • Add additional metadata to the ClusterResource class
    Metrics reporters could already get some Kafka cluster metadata information (e.g, Kafka cluster id) by implementing the ClusterResourceListener interface introduced in KIP-78. We considered adding additional context metadata to this class, but ultimately rejected that idea. ClusterResource is tightly coupled to a Kafka cluster. Adding metadata of other components to ClusterResource would be ill-suited and cause for confusion. The ClusterResourceListener.onUpdate(ClusterResource)method also has well-defined semantics for broker and client metadata updates, which would make implementing MetricsReporters more difficult.

  • Pass in additional context via the Configurable/Reconfigurable interface
    As noted, some context metadata such as broker.id is currently available via configuration, but relies on workarounds to manipulate the config passed to metrics reporters when they are defined dynamically. While it might be possible to use reconfigure(...) instead to pass context values when they are known, this would require metric reporters to know all possible context keys in advance to return in reconfigurableConfigs. As a result, one would have to make code changes to reporter plugins with every release just to support new fields added to the context. That would be akin to asking metrics reporter to know what metric tags are exposed in advance, which seemed convoluted.