Skip to content

Commit 99c3d24

Browse files
authored
Added unit test for MatchedRuleToMetricSnapshotsConverter (#993)
Signed-off-by: dhoard <[email protected]>
1 parent db0cc44 commit 99c3d24

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

collector/src/main/java/io/prometheus/jmx/MatchedRule.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,32 @@ public boolean isMatched() {
9999
return !isUnmatched();
100100
}
101101

102+
@Override
103+
public String toString() {
104+
return "MatchedRule{"
105+
+ "name='"
106+
+ name
107+
+ '\''
108+
+ ", matchName='"
109+
+ matchName
110+
+ '\''
111+
+ ", type='"
112+
+ type
113+
+ '\''
114+
+ ", help='"
115+
+ help
116+
+ '\''
117+
+ ", labelNames="
118+
+ labelNames
119+
+ ", labelValues="
120+
+ labelValues
121+
+ ", value="
122+
+ value
123+
+ ", valueFactor="
124+
+ valueFactor
125+
+ '}';
126+
}
127+
102128
@Override
103129
public boolean equals(Object o) {
104130
if (this == o) return true;

collector/src/main/java/io/prometheus/jmx/MatchedRuleToMetricSnapshotsConverter.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package io.prometheus.jmx;
22

3+
import io.prometheus.jmx.logger.Logger;
4+
import io.prometheus.jmx.logger.LoggerFactory;
35
import io.prometheus.metrics.model.snapshots.*;
46
import java.util.*;
7+
import java.util.logging.Level;
58

69
public class MatchedRuleToMetricSnapshotsConverter {
710

11+
private static final Logger LOGGER =
12+
LoggerFactory.getLogger(MatchedRuleToMetricSnapshotsConverter.class);
13+
814
private static final String OBJECTNAME = "_objectname";
915

1016
public static MetricSnapshots convert(List<MatchedRule> matchedRules) {
11-
1217
Map<String, List<MatchedRule>> rulesByPrometheusMetricName = new HashMap<>();
1318

1419
for (MatchedRule matchedRule : matchedRules) {
@@ -18,6 +23,19 @@ public static MetricSnapshots convert(List<MatchedRule> matchedRules) {
1823
matchedRulesWithSameName.add(matchedRule);
1924
}
2025

26+
if (LOGGER.isLoggable(Level.FINE)) {
27+
rulesByPrometheusMetricName
28+
.values()
29+
.forEach(
30+
matchedRules1 ->
31+
matchedRules1.forEach(
32+
matchedRule ->
33+
LOGGER.log(
34+
Level.FINE,
35+
"matchedRule %s",
36+
matchedRule)));
37+
}
38+
2139
MetricSnapshots.Builder result = MetricSnapshots.builder();
2240
for (List<MatchedRule> rulesWithSameName : rulesByPrometheusMetricName.values()) {
2341
result.metricSnapshot(convertRulesWithSameName(rulesWithSameName));
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package io.prometheus.jmx;
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.MetricMetadata;
7+
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
8+
import java.util.ArrayList;
9+
import java.util.Collections;
10+
import java.util.List;
11+
import org.junit.Test;
12+
13+
public class MatchedRuleToMetricSnapshotsConverterTest {
14+
15+
@Test
16+
public void testMatchedRuleAggregation() {
17+
List<MatchedRule> matchedRules = new ArrayList<>();
18+
19+
matchedRules.add(
20+
new MatchedRule(
21+
"jvm_memory_committed_bytes",
22+
"java.lang<type=Memory><HeapMemoryUsage>committed: 16252928",
23+
"UNKNOWN",
24+
"java.lang.management.MemoryUsage"
25+
+ " java.lang:name=null,type=Memory,attribute=committed",
26+
of("area"),
27+
of("heap"),
28+
1.6252928E7,
29+
1.0));
30+
31+
matchedRules.add(
32+
new MatchedRule(
33+
"jvm_memory_committed_bytes",
34+
"java.lang<type=Memory><NonHeapMemoryUsage>committed: 17170432",
35+
"UNKNOWN",
36+
"java.lang.management.MemoryUsage"
37+
+ " java.lang:name=null,type=Memory,attribute=committed",
38+
of("area"),
39+
of("nonheap"),
40+
2.1757952E7,
41+
1.0));
42+
43+
MetricSnapshots metricSnapshots =
44+
MatchedRuleToMetricSnapshotsConverter.convert(matchedRules);
45+
46+
assertThat(metricSnapshots).hasSize(1);
47+
48+
metricSnapshots.forEach(
49+
metricSnapshot -> {
50+
MetricMetadata metricMetadata = metricSnapshot.getMetadata();
51+
52+
assertThat(metricMetadata.getName()).isEqualTo("jvm_memory_committed_bytes");
53+
assertThat(metricMetadata.getPrometheusName())
54+
.isEqualTo("jvm_memory_committed_bytes");
55+
56+
List<? extends DataPointSnapshot> dataPointSnapshots =
57+
metricSnapshot.getDataPoints();
58+
assertThat(dataPointSnapshots).hasSize(2);
59+
});
60+
}
61+
62+
private static List<String> of(String... strings) {
63+
List<String> list = new ArrayList<>();
64+
Collections.addAll(list, strings);
65+
return list;
66+
}
67+
}

0 commit comments

Comments
 (0)