chia7712 commented on code in PR #20493:
URL: https://github.com/apache/kafka/pull/20493#discussion_r2340463734


##########
clients/src/main/java/org/apache/kafka/common/utils/AppInfoParser.java:
##########
@@ -81,31 +82,44 @@ public static synchronized void unregisterAppInfo(String 
prefix, String id, Metr
             if (server.isRegistered(name))
                 server.unregisterMBean(name);
 
-            unregisterMetrics(metrics);
+            unregisterMetrics(metrics, id);
         } catch (JMException e) {
             log.warn("Error unregistering AppInfo mbean", e);
         } finally {
             log.info("App info {} for {} unregistered", prefix, id);
         }
     }
 
-    private static MetricName metricName(Metrics metrics, String name) {
-        return metrics.metricName(name, "app-info", "Metric indicating " + 
name);
+    private static MetricName metricName(Metrics metrics, String name, 
Map<String, String> tags) {
+        return metrics.metricName(name, "app-info", "Metric indicating " + 
name, tags);
     }
 
-    private static void registerMetrics(Metrics metrics, AppInfo appInfo) {
-        if (metrics != null) {
-            metrics.addMetric(metricName(metrics, "version"), (Gauge<String>) 
(config, now) -> appInfo.getVersion());
-            metrics.addMetric(metricName(metrics, "commit-id"), 
(Gauge<String>) (config, now) -> appInfo.getCommitId());
-            metrics.addMetric(metricName(metrics, "start-time-ms"), 
(Gauge<Long>) (config, now) -> appInfo.getStartTimeMs());
+    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());
+        // Mirror Maker/Worker doesn't set client-id tag into the metrics 
config, so we need to set it here.

Review Comment:
   Should we fix the both MM or worker? 



##########
clients/src/main/java/org/apache/kafka/common/utils/AppInfoParser.java:
##########
@@ -81,31 +82,44 @@ public static synchronized void unregisterAppInfo(String 
prefix, String id, Metr
             if (server.isRegistered(name))
                 server.unregisterMBean(name);
 
-            unregisterMetrics(metrics);
+            unregisterMetrics(metrics, id);
         } catch (JMException e) {
             log.warn("Error unregistering AppInfo mbean", e);
         } finally {
             log.info("App info {} for {} unregistered", prefix, id);
         }
     }
 
-    private static MetricName metricName(Metrics metrics, String name) {
-        return metrics.metricName(name, "app-info", "Metric indicating " + 
name);
+    private static MetricName metricName(Metrics metrics, String name, 
Map<String, String> tags) {
+        return metrics.metricName(name, "app-info", "Metric indicating " + 
name, tags);
     }
 
-    private static void registerMetrics(Metrics metrics, AppInfo appInfo) {
-        if (metrics != null) {
-            metrics.addMetric(metricName(metrics, "version"), (Gauge<String>) 
(config, now) -> appInfo.getVersion());
-            metrics.addMetric(metricName(metrics, "commit-id"), 
(Gauge<String>) (config, now) -> appInfo.getCommitId());
-            metrics.addMetric(metricName(metrics, "start-time-ms"), 
(Gauge<Long>) (config, now) -> appInfo.getStartTimeMs());
+    private static void registerMetrics(Metrics metrics, AppInfo appInfo, 
String clientId) {

Review Comment:
   Could you please add a unit test to ensure that the client ID is not added 
repeatedly?



-- 
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