Current state: Approved
Discussion thread: here
Vote thread: here
JIRA:
All Kafka components register AppInfo metrics to track application details such as start time and commit-id etc. These metrics are valuable for monitoring and debugging. However, AppInfo does not include the client-id for Worker and MM2 clients, which is an important piece of information for custom metrics reporters. While most Kafka clients (consumer, producer, admin) already register their client-id in the metrics config, there are cases in MM2 and Worker where AppInfo is registered without the client-id.
The AppInfoParser class registers a JMX MBean with the provided client-id, but when adding metrics to the Metrics registry, the client-id is not included. This KIP proposes adding the client-id as a metric tag.
Currently MetricName for Worker and MM2 clients:
New MetricName for Worker and MM2 clients:
1) The new MBean will include a new tag, client-id
private static void registerMetrics(Metrics metrics, AppInfo appInfo, String clientId) {
if (metrics == null) return;
// Most Kafka clients (producer/consumer/admin) set the client-id tag in the metrics config.
// Although we don’t explicitly parse client-id here, these metrics are automatically tagged with client-id.
metrics.addMetric(metricName(metrics, "version", Map.of()), (Gauge<String>) (config, now) -> appInfo.getVersion());
metrics.addMetric(metricName(metrics, "commit-id", Map.of()), (Gauge<String>) (config, now) -> appInfo.getCommitId());
metrics.addMetric(metricName(metrics, "start-time-ms", Map.of()), (Gauge<Long>) (config, now) -> appInfo.getStartTimeMs());
// MirrorMaker/Worker doesn't set client-id tag into the metrics config, so we need to set it here.
if (!metrics.config().tags().containsKey("client-id") && clientId != null) {
metrics.addMetric(metricName(metrics, "version", Map.of("client-id", clientId)), (Gauge<String>) (config, now) -> appInfo.getVersion());
metrics.addMetric(metricName(metrics, "commit-id", Map.of("client-id", clientId)), (Gauge<String>) (config, now) -> appInfo.getCommitId());
metrics.addMetric(metricName(metrics, "start-time-ms", Map.of("client-id", clientId)), (Gauge<Long>) (config, now) -> appInfo.getStartTimeMs());
}
} |
2) When unregisters an MBean, also remove the new and deprecated MBean.
private static void unregisterMetrics(Metrics metrics, String clientId) {
if (metrics == null) return;
metrics.removeMetric(metricName(metrics, "version", Map.of()));
metrics.removeMetric(metricName(metrics, "commit-id", Map.of()));
metrics.removeMetric(metricName(metrics, "start-time-ms", Map.of()));
if (!metrics.config().tags().containsKey("client-id") && clientId != null) {
metrics.removeMetric(metricName(metrics, "version", Map.of("client-id", clientId)));
metrics.removeMetric(metricName(metrics, "commit-id", Map.of("client-id", clientId)));
metrics.removeMetric(metricName(metrics, "start-time-ms", Map.of("client-id", clientId)));
}
} |
For users currently relying on the metric name without client-id tag, it will remain available until Kafka 5.0. New users are encouraged to use the new metric name, which includes the client-id tag. This deprecation will be documented in upgrade.html.
In Kafka 5.0, the following metric name will be removed.
change unit test or Integration test to verify the new method.
Nevertheless, this approach is considered unnecessary, as it would introduce complexity without significant benefit.