Skip to content

Commit c4fb100

Browse files
authored
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]>
1 parent 3067f15 commit c4fb100

File tree

2 files changed

+8
-36
lines changed

2 files changed

+8
-36
lines changed

clients/src/main/java/org/apache/kafka/common/utils/AppInfoParser.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import org.apache.kafka.common.MetricName;
2020
import org.apache.kafka.common.metrics.Gauge;
21-
import org.apache.kafka.common.metrics.MetricConfig;
2221
import org.apache.kafka.common.metrics.Metrics;
2322

2423
import org.slf4j.Logger;
@@ -96,9 +95,9 @@ private static MetricName metricName(Metrics metrics, String name) {
9695

9796
private static void registerMetrics(Metrics metrics, AppInfo appInfo) {
9897
if (metrics != null) {
99-
metrics.addMetric(metricName(metrics, "version"), new ImmutableValue<>(appInfo.getVersion()));
100-
metrics.addMetric(metricName(metrics, "commit-id"), new ImmutableValue<>(appInfo.getCommitId()));
101-
metrics.addMetric(metricName(metrics, "start-time-ms"), new ImmutableValue<>(appInfo.getStartTimeMs()));
98+
metrics.addMetric(metricName(metrics, "version"), (Gauge<String>) (config, now) -> appInfo.getVersion());
99+
metrics.addMetric(metricName(metrics, "commit-id"), (Gauge<String>) (config, now) -> appInfo.getCommitId());
100+
metrics.addMetric(metricName(metrics, "start-time-ms"), (Gauge<Long>) (config, now) -> appInfo.getStartTimeMs());
102101
}
103102
}
104103

@@ -143,17 +142,4 @@ public Long getStartTimeMs() {
143142
}
144143

145144
}
146-
147-
static class ImmutableValue<T> implements Gauge<T> {
148-
private final T value;
149-
150-
public ImmutableValue(T value) {
151-
this.value = value;
152-
}
153-
154-
@Override
155-
public T value(MetricConfig config, long now) {
156-
return value;
157-
}
158-
}
159145
}

tools/src/test/java/org/apache/kafka/tools/PushHttpMetricsReporterTest.java

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.apache.kafka.common.config.ConfigException;
2121
import org.apache.kafka.common.metrics.Gauge;
2222
import org.apache.kafka.common.metrics.KafkaMetric;
23-
import org.apache.kafka.common.metrics.MetricConfig;
2423
import org.apache.kafka.common.utils.MockTime;
2524
import org.apache.kafka.common.utils.Time;
2625

@@ -186,35 +185,35 @@ public void testMetricValues() throws Exception {
186185
KafkaMetric metric1 = new KafkaMetric(
187186
new Object(),
188187
new MetricName("name1", "group1", "desc1", Map.of("key1", "value1")),
189-
new ImmutableValue<>(1.0),
188+
(Gauge<Double>) (config, now) -> 1.0,
190189
null,
191190
time
192191
);
193192
KafkaMetric newMetric1 = new KafkaMetric(
194193
new Object(),
195194
new MetricName("name1", "group1", "desc1", Map.of("key1", "value1")),
196-
new ImmutableValue<>(-1.0),
195+
(Gauge<Double>) (config, now) -> -1.0,
197196
null,
198197
time
199198
);
200199
KafkaMetric metric2 = new KafkaMetric(
201200
new Object(),
202201
new MetricName("name2", "group2", "desc2", Map.of("key2", "value2")),
203-
new ImmutableValue<>(2.0),
202+
(Gauge<Double>) (config, now) -> 2.0,
204203
null,
205204
time
206205
);
207206
KafkaMetric metric3 = new KafkaMetric(
208207
new Object(),
209208
new MetricName("name3", "group3", "desc3", Map.of("key3", "value3")),
210-
new ImmutableValue<>(3.0),
209+
(Gauge<Double>) (config, now) -> 3.0,
211210
null,
212211
time
213212
);
214213
KafkaMetric metric4 = new KafkaMetric(
215214
new Object(),
216215
new MetricName("name4", "group4", "desc4", Map.of("key4", "value4")),
217-
new ImmutableValue<>("value4"),
216+
(Gauge<String>) (config, now) -> "value4",
218217
null,
219218
time
220219
);
@@ -333,17 +332,4 @@ private void verifyResponse() throws IOException {
333332
verify(httpOut).flush();
334333
verify(httpOut).close();
335334
}
336-
337-
static class ImmutableValue<T> implements Gauge<T> {
338-
private final T value;
339-
340-
public ImmutableValue(T value) {
341-
this.value = value;
342-
}
343-
344-
@Override
345-
public T value(MetricConfig config, long now) {
346-
return value;
347-
}
348-
}
349335
}

0 commit comments

Comments
 (0)