This is an automated email from the ASF dual-hosted git repository.

chia7712 pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/kafka.git


The following commit(s) were added to refs/heads/trunk by this push:
     new c4fb1008c48 MINOR: Use lambda expressions instead of ImmutableValue 
for Gauges (#20351)
c4fb1008c48 is described below

commit c4fb1008c4856c8cf9594269c86323753e6860ce
Author: Ming-Yen Chung <[email protected]>
AuthorDate: Thu Aug 14 20:35:21 2025 +0800

    MINOR: Use lambda expressions instead of ImmutableValue for Gauges (#20351)
    
    Refactor metric gauges instantiation to use lambda expressions instead
    of ImmutableValue.
    
    Reviewers: Ken Huang <[email protected]>, TengYao Chi
     <[email protected]>, Chia-Ping Tsai <[email protected]>
---
 .../apache/kafka/common/utils/AppInfoParser.java   | 20 +++---------------
 .../kafka/tools/PushHttpMetricsReporterTest.java   | 24 +++++-----------------
 2 files changed, 8 insertions(+), 36 deletions(-)

diff --git 
a/clients/src/main/java/org/apache/kafka/common/utils/AppInfoParser.java 
b/clients/src/main/java/org/apache/kafka/common/utils/AppInfoParser.java
index f9ebd82ea11..e23bda36d5f 100644
--- a/clients/src/main/java/org/apache/kafka/common/utils/AppInfoParser.java
+++ b/clients/src/main/java/org/apache/kafka/common/utils/AppInfoParser.java
@@ -18,7 +18,6 @@ package org.apache.kafka.common.utils;
 
 import org.apache.kafka.common.MetricName;
 import org.apache.kafka.common.metrics.Gauge;
-import org.apache.kafka.common.metrics.MetricConfig;
 import org.apache.kafka.common.metrics.Metrics;
 
 import org.slf4j.Logger;
@@ -96,9 +95,9 @@ public class AppInfoParser {
 
     private static void registerMetrics(Metrics metrics, AppInfo appInfo) {
         if (metrics != null) {
-            metrics.addMetric(metricName(metrics, "version"), new 
ImmutableValue<>(appInfo.getVersion()));
-            metrics.addMetric(metricName(metrics, "commit-id"), new 
ImmutableValue<>(appInfo.getCommitId()));
-            metrics.addMetric(metricName(metrics, "start-time-ms"), new 
ImmutableValue<>(appInfo.getStartTimeMs()));
+            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());
         }
     }
 
@@ -143,17 +142,4 @@ public class AppInfoParser {
         }
 
     }
-
-    static class ImmutableValue<T> implements Gauge<T> {
-        private final T value;
-
-        public ImmutableValue(T value) {
-            this.value = value;
-        }
-
-        @Override
-        public T value(MetricConfig config, long now) {
-            return value;
-        }
-    }
 }
diff --git 
a/tools/src/test/java/org/apache/kafka/tools/PushHttpMetricsReporterTest.java 
b/tools/src/test/java/org/apache/kafka/tools/PushHttpMetricsReporterTest.java
index 00215d6c7ee..bc7d91e6333 100644
--- 
a/tools/src/test/java/org/apache/kafka/tools/PushHttpMetricsReporterTest.java
+++ 
b/tools/src/test/java/org/apache/kafka/tools/PushHttpMetricsReporterTest.java
@@ -20,7 +20,6 @@ import org.apache.kafka.common.MetricName;
 import org.apache.kafka.common.config.ConfigException;
 import org.apache.kafka.common.metrics.Gauge;
 import org.apache.kafka.common.metrics.KafkaMetric;
-import org.apache.kafka.common.metrics.MetricConfig;
 import org.apache.kafka.common.utils.MockTime;
 import org.apache.kafka.common.utils.Time;
 
@@ -186,35 +185,35 @@ public class PushHttpMetricsReporterTest {
         KafkaMetric metric1 = new KafkaMetric(
                 new Object(),
                 new MetricName("name1", "group1", "desc1", Map.of("key1", 
"value1")),
-                new ImmutableValue<>(1.0),
+                (Gauge<Double>) (config, now) -> 1.0,
                 null,
                 time
         );
         KafkaMetric newMetric1 = new KafkaMetric(
                 new Object(),
                 new MetricName("name1", "group1", "desc1", Map.of("key1", 
"value1")),
-                new ImmutableValue<>(-1.0),
+                (Gauge<Double>) (config, now) -> -1.0,
                 null,
                 time
         );
         KafkaMetric metric2 = new KafkaMetric(
                 new Object(),
                 new MetricName("name2", "group2", "desc2", Map.of("key2", 
"value2")),
-                new ImmutableValue<>(2.0),
+                (Gauge<Double>) (config, now) -> 2.0,
                 null,
                 time
         );
         KafkaMetric metric3 = new KafkaMetric(
                 new Object(),
                 new MetricName("name3", "group3", "desc3", Map.of("key3", 
"value3")),
-                new ImmutableValue<>(3.0),
+                (Gauge<Double>) (config, now) -> 3.0,
                 null,
                 time
         );
         KafkaMetric metric4 = new KafkaMetric(
             new Object(),
             new MetricName("name4", "group4", "desc4", Map.of("key4", 
"value4")),
-            new ImmutableValue<>("value4"),
+            (Gauge<String>) (config, now) -> "value4",
             null,
             time
         );
@@ -333,17 +332,4 @@ public class PushHttpMetricsReporterTest {
         verify(httpOut).flush();
         verify(httpOut).close();
     }
-
-    static class ImmutableValue<T> implements Gauge<T> {
-        private final T value;
-
-        public ImmutableValue(T value) {
-            this.value = value;
-        }
-
-        @Override
-        public T value(MetricConfig config, long now) {
-            return value;
-        }
-    }
 }

Reply via email to