Skip to content

Commit a670561

Browse files
committed
refactor jvm metrics
1 parent 2c65318 commit a670561

File tree

3 files changed

+157
-50
lines changed

3 files changed

+157
-50
lines changed

jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/assertions/MetricAssert.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public MetricAssert hasDescription(String description) {
7575

7676
info.description("unexpected description for metric '%s'", actual.getName());
7777
objects.assertEqual(info, actual.getDescription(), description);
78-
strictCheck("description", /* expectedValue= */false, descriptionChecked);
78+
strictCheck("description", /* expectedValue= */ false, descriptionChecked);
7979
descriptionChecked = true;
8080
return this;
8181
}
@@ -92,7 +92,7 @@ public MetricAssert hasUnit(String unit) {
9292

9393
info.description("unexpected unit for metric '%s'", actual.getName());
9494
objects.assertEqual(info, actual.getUnit(), unit);
95-
strictCheck("unit", /* expectedValue= */false, unitChecked);
95+
strictCheck("unit", /* expectedValue= */ false, unitChecked);
9696
unitChecked = true;
9797
return this;
9898
}
@@ -108,7 +108,7 @@ public MetricAssert isGauge() {
108108

109109
info.description("gauge expected for metric '%s'", actual.getName());
110110
objects.assertEqual(info, actual.hasGauge(), true);
111-
strictCheck("type", /* expectedValue= */false, typeChecked);
111+
strictCheck("type", /* expectedValue= */ false, typeChecked);
112112
typeChecked = true;
113113
return this;
114114
}
@@ -135,7 +135,7 @@ private MetricAssert hasSum(boolean monotonic) {
135135
public MetricAssert isCounter() {
136136
// counters have a monotonic sum as their value can't decrease
137137
hasSum(true);
138-
strictCheck("type", /* expectedValue= */false, typeChecked);
138+
strictCheck("type", /* expectedValue= */ false, typeChecked);
139139
typeChecked = true;
140140
return this;
141141
}
@@ -144,7 +144,7 @@ public MetricAssert isCounter() {
144144
public MetricAssert isUpDownCounter() {
145145
// up down counters are non-monotonic as their value can increase & decrease
146146
hasSum(false);
147-
strictCheck("type", /* expectedValue= */false, typeChecked);
147+
strictCheck("type", /* expectedValue= */ false, typeChecked);
148148
typeChecked = true;
149149
return this;
150150
}
@@ -182,14 +182,13 @@ private MetricAssert checkDataPoints(Consumer<List<NumberDataPoint>> listConsume
182182
info.description("at least one set of data points expected for metric '%s'", actual.getName());
183183
integers.assertGreaterThan(info, count, 0);
184184

185-
strictCheck("data point attributes", /* expectedValue= */false, dataPointAttributesChecked);
185+
strictCheck("data point attributes", /* expectedValue= */ false, dataPointAttributesChecked);
186186
dataPointAttributesChecked = true;
187187
return this;
188188
}
189189

190190
@CanIgnoreReturnValue
191191
public MetricAssert hasTypedDataPoints(Collection<String> types) {
192-
// TODO: we could replace this with 'hasDataPointsAttributes'
193192
return checkDataPoints(
194193
dataPoints -> {
195194
dataPointsCommonCheck(dataPoints);

jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/JvmIntegrationTest.java

Lines changed: 142 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,147 @@ protected MetricsVerifier createMetricsVerifier() {
4444
"PS Survivor Space");
4545
List<String> gcCollectionLabels = Arrays.asList("PS MarkSweep", "PS Scavenge");
4646

47-
MetricsVerifier metricsVerifier =
48-
MetricsVerifier.create()
49-
.assertGauge("jvm.classes.loaded", "number of loaded classes", "1")
50-
.assertTypedCounter(
51-
"jvm.gc.collections.count",
52-
"total number of collections that have occurred",
53-
"1",
54-
gcCollectionLabels)
55-
.add(
56-
"jvm.gc.collections.elapsed",
57-
metric ->
58-
metric
59-
.hasDescription(
60-
"the approximate accumulated collection elapsed time in milliseconds")
61-
.hasUnit("ms")
62-
.isCounter()
63-
.hasTypedDataPoints(gcCollectionLabels))
64-
.assertGauge("jvm.memory.heap.committed", "current heap usage", "by")
65-
.assertGauge("jvm.memory.heap.init", "current heap usage", "by")
66-
.assertGauge("jvm.memory.heap.max", "current heap usage", "by")
67-
.assertGauge("jvm.memory.heap.used", "current heap usage", "by")
68-
.assertGauge("jvm.memory.nonheap.committed", "current non-heap usage", "by")
69-
.assertGauge("jvm.memory.nonheap.init", "current non-heap usage", "by")
70-
.assertGauge("jvm.memory.nonheap.max", "current non-heap usage", "by")
71-
.assertGauge("jvm.memory.nonheap.used", "current non-heap usage", "by")
72-
.assertTypedGauge(
73-
"jvm.memory.pool.committed", "current memory pool usage", "by", gcLabels)
74-
.assertTypedGauge("jvm.memory.pool.init", "current memory pool usage", "by", gcLabels)
75-
.assertTypedGauge("jvm.memory.pool.max", "current memory pool usage", "by", gcLabels)
76-
.assertTypedGauge("jvm.memory.pool.used", "current memory pool usage", "by", gcLabels)
77-
.assertGauge("jvm.threads.count", "number of threads", "1");
78-
79-
return metricsVerifier;
47+
return MetricsVerifier.create()
48+
.add(
49+
"jvm.classes.loaded",
50+
metric ->
51+
metric
52+
.hasDescription("number of loaded classes")
53+
.hasUnit("1")
54+
.isGauge()
55+
.hasDataPointsWithoutAttributes())
56+
.add(
57+
"jvm.gc.collections.count",
58+
metric ->
59+
metric
60+
.hasDescription("total number of collections that have occurred")
61+
.hasUnit("1")
62+
.isCounter()
63+
.hasTypedDataPoints(gcCollectionLabels))
64+
.add(
65+
"jvm.gc.collections.elapsed",
66+
metric ->
67+
metric
68+
.hasDescription(
69+
"the approximate accumulated collection elapsed time in milliseconds")
70+
.hasUnit("ms")
71+
.isCounter()
72+
.hasTypedDataPoints(gcCollectionLabels))
73+
.add(
74+
"jvm.memory.heap.committed",
75+
metric ->
76+
metric
77+
.hasDescription("current heap usage")
78+
.hasUnit("by")
79+
.isGauge()
80+
.hasDataPointsWithoutAttributes())
81+
.add(
82+
"jvm.memory.heap.init",
83+
metric ->
84+
metric
85+
.hasDescription("current heap usage")
86+
.hasUnit("by")
87+
.isGauge()
88+
.hasDataPointsWithoutAttributes())
89+
.add(
90+
"jvm.memory.heap.max",
91+
metric ->
92+
metric
93+
.hasDescription("current heap usage")
94+
.hasUnit("by")
95+
.isGauge()
96+
.hasDataPointsWithoutAttributes())
97+
.add(
98+
"jvm.memory.heap.used",
99+
metric ->
100+
metric
101+
.hasDescription("current heap usage")
102+
.hasUnit("by")
103+
.isGauge()
104+
.hasDataPointsWithoutAttributes())
105+
.add(
106+
"jvm.memory.nonheap.committed",
107+
metric ->
108+
metric
109+
.hasDescription("current non-heap usage")
110+
.hasUnit("by")
111+
.isGauge()
112+
.hasDataPointsWithoutAttributes())
113+
.add(
114+
"jvm.memory.nonheap.init",
115+
metric ->
116+
metric
117+
.hasDescription("current non-heap usage")
118+
.hasUnit("by")
119+
.isGauge()
120+
.hasDataPointsWithoutAttributes())
121+
.add(
122+
"jvm.memory.nonheap.max",
123+
metric ->
124+
metric
125+
.hasDescription("current non-heap usage")
126+
.hasUnit("by")
127+
.isGauge()
128+
.hasDataPointsWithoutAttributes())
129+
.add(
130+
"jvm.memory.nonheap.used",
131+
metric ->
132+
metric
133+
.hasDescription("current non-heap usage")
134+
.hasUnit("by")
135+
.isGauge()
136+
.hasDataPointsWithoutAttributes())
137+
.add(
138+
"jvm.memory.pool.committed",
139+
metric ->
140+
metric
141+
.hasDescription("current memory pool usage")
142+
.hasUnit("by")
143+
.isGauge()
144+
.hasTypedDataPoints(gcLabels))
145+
.add(
146+
"jvm.memory.pool.init",
147+
metric ->
148+
metric
149+
.hasDescription("current memory pool usage")
150+
.hasUnit("by")
151+
.isGauge()
152+
.hasTypedDataPoints(gcLabels))
153+
.add(
154+
"jvm.memory.pool.max",
155+
metric ->
156+
metric
157+
.hasDescription("current memory pool usage")
158+
.hasUnit("by")
159+
.isGauge()
160+
.hasTypedDataPoints(gcLabels))
161+
.add(
162+
"jvm.memory.pool.used",
163+
metric ->
164+
metric
165+
.hasDescription("current memory pool usage")
166+
.hasUnit("by")
167+
.isGauge()
168+
.hasTypedDataPoints(gcLabels))
169+
.add(
170+
"jvm.threads.count",
171+
metric ->
172+
metric
173+
.hasDescription("number of threads")
174+
.hasUnit("1")
175+
.isGauge()
176+
.hasDataPointsWithoutAttributes());
80177
}
178+
179+
/*
180+
List<String> gcLabels =
181+
Arrays.asList(
182+
"Code Cache",
183+
"PS Eden Space",
184+
"PS Old Gen",
185+
"Metaspace",
186+
"Compressed Class Space",
187+
"PS Survivor Space");
188+
List<String> gcCollectionLabels = Arrays.asList("PS MarkSweep", "PS Scavenge");
189+
*/
81190
}

jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/MetricsVerifier.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ public MetricsVerifier assertGauge(String metricName, String description, String
7070
.hasDataPointsWithoutAttributes());
7171
}
7272

73+
@CanIgnoreReturnValue
74+
public MetricsVerifier assertTypedGauge(
75+
String metricName, String description, String unit, List<String> types) {
76+
return add(
77+
metricName,
78+
metric ->
79+
metric.hasDescription(description).hasUnit(unit).isGauge().hasTypedDataPoints(types));
80+
}
81+
7382
// TODO: can now be inlined
7483
@CanIgnoreReturnValue
7584
public MetricsVerifier assertCounter(String metricName, String description, String unit) {
@@ -166,16 +175,6 @@ public MetricsVerifier assertTypedCounter(
166175
metric.hasDescription(description).hasUnit(unit).isCounter().hasTypedDataPoints(types));
167176
}
168177

169-
// TODO: can be inlined
170-
@CanIgnoreReturnValue
171-
public MetricsVerifier assertTypedGauge(
172-
String metricName, String description, String unit, List<String> types) {
173-
return add(
174-
metricName,
175-
metric ->
176-
metric.hasDescription(description).hasUnit(unit).isGauge().hasTypedDataPoints(types));
177-
}
178-
179178
public void verify(List<Metric> metrics) {
180179
verifyAllExpectedMetricsWereReceived(metrics);
181180

0 commit comments

Comments
 (0)