Skip to content

Commit 5ec92f4

Browse files
iliaxiliax
andauthored
BE: Metrics: Fix JMX metrics collection (2) (#1285)
Co-authored-by: iliax <ilya.kuramshin@almatech.dev>
1 parent 43bd3b0 commit 5ec92f4

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

api/src/main/java/io/kafbat/ui/service/metrics/RawMetric.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import java.math.BigDecimal;
77
import java.util.Arrays;
88
import java.util.Collection;
9+
import java.util.HashMap;
910
import java.util.LinkedHashMap;
1011
import java.util.Map;
12+
import java.util.Set;
1113
import java.util.stream.Stream;
1214

1315
public interface RawMetric {
@@ -26,24 +28,27 @@ static RawMetric create(String name, Map<String, String> labels, BigDecimal valu
2628

2729
static Stream<MetricSnapshot> groupIntoSnapshot(Collection<RawMetric> rawMetrics) {
2830
Map<String, Gauge> map = new LinkedHashMap<>();
31+
Map<String, String[]> gaugeLabels = new HashMap<>();
2932
for (RawMetric m : rawMetrics) {
3033
var lbls = m.labels().keySet()
3134
.stream()
3235
.map(PrometheusNaming::sanitizeLabelName)
3336
.toArray(String[]::new);
34-
var lblVals = Arrays.stream(lbls)
37+
var lblVals = m.labels().keySet()
38+
.stream()
3539
.map(l -> m.labels().get(l))
3640
.toArray(String[]::new);
3741
var sanitizedName = PrometheusNaming.sanitizeMetricName(m.name());
3842
var gauge = map.computeIfAbsent(
39-
sanitizedName,
40-
n -> Gauge.builder()
41-
.name(sanitizedName)
42-
.help(sanitizedName)
43-
.labelNames(lbls)
44-
.build()
43+
sanitizedName, n -> {
44+
gaugeLabels.put(n, lbls);
45+
return Gauge.builder().name(n).help(n).labelNames(lbls).build();
46+
}
4547
);
46-
gauge.labelValues(lblVals).set(m.value().doubleValue());
48+
if (Arrays.equals(lbls, gaugeLabels.get(sanitizedName))) {
49+
//using labels of first registered gauge, if not fit - skipping
50+
gauge.labelValues(lblVals).set(m.value().doubleValue());
51+
}
4752
}
4853
return map.values().stream().map(Gauge::collect);
4954
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.kafbat.ui.service.metrics;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import io.prometheus.metrics.model.snapshots.DataPointSnapshot;
6+
import io.prometheus.metrics.model.snapshots.Labels;
7+
import io.prometheus.metrics.model.snapshots.MetricSnapshot;
8+
import java.math.BigDecimal;
9+
import java.util.List;
10+
import java.util.Map;
11+
import org.junit.jupiter.api.Test;
12+
13+
class RawMetricTest {
14+
15+
@Test
16+
void groupIntoSnapshotChoosesFirstGaugeLabels() {
17+
List<MetricSnapshot> list = RawMetric.groupIntoSnapshot(
18+
List.of(
19+
RawMetric.create("name", Map.of("l1", "v1"), BigDecimal.ONE),
20+
RawMetric.create("name", Map.of("l1", "v11"), BigDecimal.TWO),
21+
RawMetric.create("name", Map.of("l1", "v1", "l2", "v2"), BigDecimal.TEN)
22+
)
23+
).toList();
24+
25+
assertThat(list)
26+
.hasSize(1)
27+
.element(0)
28+
.satisfies(snap ->
29+
assertThat(snap.getDataPoints())
30+
.map(DataPointSnapshot::getLabels)
31+
.containsExactly(Labels.of("l1", "v1"), Labels.of("l1", "v11"))
32+
);
33+
}
34+
35+
}

0 commit comments

Comments
 (0)