Skip to content

Commit fa217e5

Browse files
authored
[jmx-scraper] fix JVM units + simplify tests (#1593)
1 parent 10efe16 commit fa217e5

File tree

6 files changed

+79
-95
lines changed

6 files changed

+79
-95
lines changed

jmx-metrics/src/integrationTest/java/io/opentelemetry/contrib/jmxmetrics/target_systems/JvmTargetSystemIntegrationTest.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ void endToEnd() {
3131
"Metaspace",
3232
"Par Survivor Space");
3333
waitAndAssertMetrics(
34-
metric -> assertGauge(metric, "jvm.classes.loaded", "number of loaded classes", "1"),
34+
metric -> assertGauge(metric, "jvm.classes.loaded", "number of loaded classes", "{class}"),
3535
metric ->
3636
assertTypedSum(
3737
metric,
3838
"jvm.gc.collections.count",
3939
"total number of collections that have occurred",
40-
"1",
40+
"{collection}",
4141
Arrays.asList("ConcurrentMarkSweep", "ParNew")),
4242
metric ->
4343
assertTypedSum(
@@ -46,27 +46,27 @@ void endToEnd() {
4646
"the approximate accumulated collection elapsed time in milliseconds",
4747
"ms",
4848
Arrays.asList("ConcurrentMarkSweep", "ParNew")),
49-
metric -> assertGauge(metric, "jvm.memory.heap.committed", "current heap usage", "by"),
50-
metric -> assertGauge(metric, "jvm.memory.heap.init", "current heap usage", "by"),
51-
metric -> assertGauge(metric, "jvm.memory.heap.max", "current heap usage", "by"),
52-
metric -> assertGauge(metric, "jvm.memory.heap.used", "current heap usage", "by"),
49+
metric -> assertGauge(metric, "jvm.memory.heap.committed", "current heap usage", "By"),
50+
metric -> assertGauge(metric, "jvm.memory.heap.init", "current heap usage", "By"),
51+
metric -> assertGauge(metric, "jvm.memory.heap.max", "current heap usage", "By"),
52+
metric -> assertGauge(metric, "jvm.memory.heap.used", "current heap usage", "By"),
5353
metric ->
54-
assertGauge(metric, "jvm.memory.nonheap.committed", "current non-heap usage", "by"),
55-
metric -> assertGauge(metric, "jvm.memory.nonheap.init", "current non-heap usage", "by"),
56-
metric -> assertGauge(metric, "jvm.memory.nonheap.max", "current non-heap usage", "by"),
57-
metric -> assertGauge(metric, "jvm.memory.nonheap.used", "current non-heap usage", "by"),
54+
assertGauge(metric, "jvm.memory.nonheap.committed", "current non-heap usage", "By"),
55+
metric -> assertGauge(metric, "jvm.memory.nonheap.init", "current non-heap usage", "By"),
56+
metric -> assertGauge(metric, "jvm.memory.nonheap.max", "current non-heap usage", "By"),
57+
metric -> assertGauge(metric, "jvm.memory.nonheap.used", "current non-heap usage", "By"),
5858
metric ->
5959
assertTypedGauge(
60-
metric, "jvm.memory.pool.committed", "current memory pool usage", "by", gcLabels),
60+
metric, "jvm.memory.pool.committed", "current memory pool usage", "By", gcLabels),
6161
metric ->
6262
assertTypedGauge(
63-
metric, "jvm.memory.pool.init", "current memory pool usage", "by", gcLabels),
63+
metric, "jvm.memory.pool.init", "current memory pool usage", "By", gcLabels),
6464
metric ->
6565
assertTypedGauge(
66-
metric, "jvm.memory.pool.max", "current memory pool usage", "by", gcLabels),
66+
metric, "jvm.memory.pool.max", "current memory pool usage", "By", gcLabels),
6767
metric ->
6868
assertTypedGauge(
69-
metric, "jvm.memory.pool.used", "current memory pool usage", "by", gcLabels),
70-
metric -> assertGauge(metric, "jvm.threads.count", "number of threads", "1"));
69+
metric, "jvm.memory.pool.used", "current memory pool usage", "By", gcLabels),
70+
metric -> assertGauge(metric, "jvm.threads.count", "number of threads", "{thread}"));
7171
}
7272
}

jmx-metrics/src/integrationTest/java/io/opentelemetry/contrib/jmxmetrics/target_systems/KafkaIntegrationTest.java

+16-15
Original file line numberDiff line numberDiff line change
@@ -380,13 +380,14 @@ void endToEnd() {
380380
List<Consumer<Metric>> assertions = new ArrayList<>(kafkaBrokerAssertions());
381381
assertions.addAll(
382382
Arrays.asList(
383-
metric -> assertGauge(metric, "jvm.classes.loaded", "number of loaded classes", "1"),
383+
metric ->
384+
assertGauge(metric, "jvm.classes.loaded", "number of loaded classes", "{class}"),
384385
metric ->
385386
assertTypedSum(
386387
metric,
387388
"jvm.gc.collections.count",
388389
"total number of collections that have occurred",
389-
"1",
390+
"{collection}",
390391
Arrays.asList("G1 Young Generation", "G1 Old Generation")),
391392
metric ->
392393
assertTypedSum(
@@ -396,36 +397,36 @@ void endToEnd() {
396397
"ms",
397398
Arrays.asList("G1 Young Generation", "G1 Old Generation")),
398399
metric ->
399-
assertGauge(metric, "jvm.memory.heap.committed", "current heap usage", "by"),
400-
metric -> assertGauge(metric, "jvm.memory.heap.init", "current heap usage", "by"),
401-
metric -> assertGauge(metric, "jvm.memory.heap.max", "current heap usage", "by"),
402-
metric -> assertGauge(metric, "jvm.memory.heap.used", "current heap usage", "by"),
400+
assertGauge(metric, "jvm.memory.heap.committed", "current heap usage", "By"),
401+
metric -> assertGauge(metric, "jvm.memory.heap.init", "current heap usage", "By"),
402+
metric -> assertGauge(metric, "jvm.memory.heap.max", "current heap usage", "By"),
403+
metric -> assertGauge(metric, "jvm.memory.heap.used", "current heap usage", "By"),
403404
metric ->
404405
assertGauge(
405-
metric, "jvm.memory.nonheap.committed", "current non-heap usage", "by"),
406+
metric, "jvm.memory.nonheap.committed", "current non-heap usage", "By"),
406407
metric ->
407-
assertGauge(metric, "jvm.memory.nonheap.init", "current non-heap usage", "by"),
408+
assertGauge(metric, "jvm.memory.nonheap.init", "current non-heap usage", "By"),
408409
metric ->
409-
assertGauge(metric, "jvm.memory.nonheap.max", "current non-heap usage", "by"),
410+
assertGauge(metric, "jvm.memory.nonheap.max", "current non-heap usage", "By"),
410411
metric ->
411-
assertGauge(metric, "jvm.memory.nonheap.used", "current non-heap usage", "by"),
412+
assertGauge(metric, "jvm.memory.nonheap.used", "current non-heap usage", "By"),
412413
metric ->
413414
assertTypedGauge(
414415
metric,
415416
"jvm.memory.pool.committed",
416417
"current memory pool usage",
417-
"by",
418+
"By",
418419
gcLabels),
419420
metric ->
420421
assertTypedGauge(
421-
metric, "jvm.memory.pool.init", "current memory pool usage", "by", gcLabels),
422+
metric, "jvm.memory.pool.init", "current memory pool usage", "By", gcLabels),
422423
metric ->
423424
assertTypedGauge(
424-
metric, "jvm.memory.pool.max", "current memory pool usage", "by", gcLabels),
425+
metric, "jvm.memory.pool.max", "current memory pool usage", "By", gcLabels),
425426
metric ->
426427
assertTypedGauge(
427-
metric, "jvm.memory.pool.used", "current memory pool usage", "by", gcLabels),
428-
metric -> assertGauge(metric, "jvm.threads.count", "number of threads", "1")));
428+
metric, "jvm.memory.pool.used", "current memory pool usage", "By", gcLabels),
429+
metric -> assertGauge(metric, "jvm.threads.count", "number of threads", "{thread}")));
429430

430431
waitAndAssertMetrics(assertions);
431432
}

jmx-metrics/src/main/resources/target-systems/jvm.groovy

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
def classLoading = otel.mbean("java.lang:type=ClassLoading")
1818
otel.instrument(classLoading, "jvm.classes.loaded", "number of loaded classes",
19-
"1", "LoadedClassCount", otel.&longValueCallback)
19+
"{class}", "LoadedClassCount", otel.&longValueCallback)
2020

2121
def garbageCollector = otel.mbeans("java.lang:type=GarbageCollector,*")
2222
otel.instrument(garbageCollector, "jvm.gc.collections.count", "total number of collections that have occurred",
23-
"1", ["name" : { mbean -> mbean.name().getKeyProperty("name") }],
23+
"{collection}", ["name" : { mbean -> mbean.name().getKeyProperty("name") }],
2424
"CollectionCount", otel.&longCounterCallback)
2525
otel.instrument(garbageCollector, "jvm.gc.collections.elapsed",
2626
"the approximate accumulated collection elapsed time in milliseconds", "ms",
@@ -29,15 +29,15 @@ otel.instrument(garbageCollector, "jvm.gc.collections.elapsed",
2929

3030
def memory = otel.mbean("java.lang:type=Memory")
3131
otel.instrument(memory, "jvm.memory.heap", "current heap usage",
32-
"by", "HeapMemoryUsage", otel.&longValueCallback)
32+
"By", "HeapMemoryUsage", otel.&longValueCallback)
3333
otel.instrument(memory, "jvm.memory.nonheap", "current non-heap usage",
34-
"by", "NonHeapMemoryUsage", otel.&longValueCallback)
34+
"By", "NonHeapMemoryUsage", otel.&longValueCallback)
3535

3636
def memoryPool = otel.mbeans("java.lang:type=MemoryPool,*")
3737
otel.instrument(memoryPool, "jvm.memory.pool", "current memory pool usage",
38-
"by", ["name" : { mbean -> mbean.name().getKeyProperty("name") }],
38+
"By", ["name" : { mbean -> mbean.name().getKeyProperty("name") }],
3939
"Usage", otel.&longValueCallback)
4040

4141
def threading = otel.mbean("java.lang:type=Threading")
4242
otel.instrument(threading, "jvm.threads.count", "number of threads",
43-
"1", "ThreadCount", otel.&longValueCallback)
43+
"{thread}", "ThreadCount", otel.&longValueCallback)

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

-28
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@
1212
import io.opentelemetry.proto.metrics.v1.Metric;
1313
import io.opentelemetry.proto.metrics.v1.NumberDataPoint;
1414
import java.util.Arrays;
15-
import java.util.Collection;
16-
import java.util.HashSet;
1715
import java.util.List;
1816
import java.util.Map;
19-
import java.util.Set;
2017
import java.util.function.Consumer;
2118
import java.util.stream.Collectors;
2219
import org.assertj.core.api.AbstractAssert;
@@ -199,31 +196,6 @@ private MetricAssert checkDataPoints(Consumer<List<NumberDataPoint>> listConsume
199196
return this;
200197
}
201198

202-
// TODO: To be removed and calls will be replaced with hasDataPointsWithAttributes()
203-
@CanIgnoreReturnValue
204-
public MetricAssert hasTypedDataPoints(Collection<String> types) {
205-
return checkDataPoints(
206-
dataPoints -> {
207-
dataPointsCommonCheck(dataPoints);
208-
209-
Set<String> foundValues = new HashSet<>();
210-
for (NumberDataPoint dataPoint : dataPoints) {
211-
List<KeyValue> attributes = dataPoint.getAttributesList();
212-
213-
info.description(
214-
"expected exactly one 'name' attribute for typed data point in metric '%s'",
215-
actual.getName());
216-
iterables.assertHasSize(info, attributes, 1);
217-
218-
objects.assertEqual(info, attributes.get(0).getKey(), "name");
219-
foundValues.add(attributes.get(0).getValue().getStringValue());
220-
}
221-
info.description(
222-
"missing or unexpected type attribute for metric '%s'", actual.getName());
223-
iterables.assertContainsExactlyInAnyOrder(info, foundValues, types.toArray());
224-
});
225-
}
226-
227199
private void dataPointsCommonCheck(List<NumberDataPoint> dataPoints) {
228200
info.description("unable to retrieve data points from metric '%s'", actual.getName());
229201
objects.assertNotNull(info, dataPoints);

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

+37-26
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
package io.opentelemetry.contrib.jmxscraper.target_systems;
77

8+
import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attribute;
9+
import static io.opentelemetry.contrib.jmxscraper.assertions.DataPointAttributes.attributeGroup;
10+
811
import io.opentelemetry.contrib.jmxscraper.JmxScraperContainer;
912
import io.opentelemetry.contrib.jmxscraper.TestAppContainer;
13+
import io.opentelemetry.contrib.jmxscraper.assertions.AttributeMatcherGroup;
1014
import java.nio.file.Path;
11-
import java.util.Arrays;
12-
import java.util.List;
1315
import org.testcontainers.containers.GenericContainer;
1416
import org.testcontainers.containers.wait.strategy.Wait;
1517

@@ -34,33 +36,34 @@ protected JmxScraperContainer customizeScraperContainer(
3436
@Override
3537
protected MetricsVerifier createMetricsVerifier() {
3638
// those values depend on the JVM GC configured
37-
List<String> gcLabels =
38-
Arrays.asList(
39+
AttributeMatcherGroup[] memoryAttributes =
40+
nameAttributeMatchers(
3941
"Code Cache",
4042
"PS Eden Space",
4143
"PS Old Gen",
4244
"Metaspace",
4345
"Compressed Class Space",
4446
"PS Survivor Space");
45-
List<String> gcCollectionLabels = Arrays.asList("PS MarkSweep", "PS Scavenge");
47+
AttributeMatcherGroup[] gcAlgorithmAttributes =
48+
nameAttributeMatchers("PS MarkSweep", "PS Scavenge");
4649

4750
return MetricsVerifier.create()
4851
.add(
4952
"jvm.classes.loaded",
5053
metric ->
5154
metric
5255
.hasDescription("number of loaded classes")
53-
.hasUnit("1")
56+
.hasUnit("{class}")
5457
.isGauge()
5558
.hasDataPointsWithoutAttributes())
5659
.add(
5760
"jvm.gc.collections.count",
5861
metric ->
5962
metric
6063
.hasDescription("total number of collections that have occurred")
61-
.hasUnit("1")
64+
.hasUnit("{collection}")
6265
.isCounter()
63-
.hasTypedDataPoints(gcCollectionLabels))
66+
.hasDataPointsWithAttributes(gcAlgorithmAttributes))
6467
.add(
6568
"jvm.gc.collections.elapsed",
6669
metric ->
@@ -69,110 +72,118 @@ protected MetricsVerifier createMetricsVerifier() {
6972
"the approximate accumulated collection elapsed time in milliseconds")
7073
.hasUnit("ms")
7174
.isCounter()
72-
.hasTypedDataPoints(gcCollectionLabels))
75+
.hasDataPointsWithAttributes(gcAlgorithmAttributes))
7376
.add(
7477
"jvm.memory.heap.committed",
7578
metric ->
7679
metric
7780
.hasDescription("current heap usage")
78-
.hasUnit("by")
81+
.hasUnit("By")
7982
.isGauge()
8083
.hasDataPointsWithoutAttributes())
8184
.add(
8285
"jvm.memory.heap.init",
8386
metric ->
8487
metric
8588
.hasDescription("current heap usage")
86-
.hasUnit("by")
89+
.hasUnit("By")
8790
.isGauge()
8891
.hasDataPointsWithoutAttributes())
8992
.add(
9093
"jvm.memory.heap.max",
9194
metric ->
9295
metric
9396
.hasDescription("current heap usage")
94-
.hasUnit("by")
97+
.hasUnit("By")
9598
.isGauge()
9699
.hasDataPointsWithoutAttributes())
97100
.add(
98101
"jvm.memory.heap.used",
99102
metric ->
100103
metric
101104
.hasDescription("current heap usage")
102-
.hasUnit("by")
105+
.hasUnit("By")
103106
.isGauge()
104107
.hasDataPointsWithoutAttributes())
105108
.add(
106109
"jvm.memory.nonheap.committed",
107110
metric ->
108111
metric
109112
.hasDescription("current non-heap usage")
110-
.hasUnit("by")
113+
.hasUnit("By")
111114
.isGauge()
112115
.hasDataPointsWithoutAttributes())
113116
.add(
114117
"jvm.memory.nonheap.init",
115118
metric ->
116119
metric
117120
.hasDescription("current non-heap usage")
118-
.hasUnit("by")
121+
.hasUnit("By")
119122
.isGauge()
120123
.hasDataPointsWithoutAttributes())
121124
.add(
122125
"jvm.memory.nonheap.max",
123126
metric ->
124127
metric
125128
.hasDescription("current non-heap usage")
126-
.hasUnit("by")
129+
.hasUnit("By")
127130
.isGauge()
128131
.hasDataPointsWithoutAttributes())
129132
.add(
130133
"jvm.memory.nonheap.used",
131134
metric ->
132135
metric
133136
.hasDescription("current non-heap usage")
134-
.hasUnit("by")
137+
.hasUnit("By")
135138
.isGauge()
136139
.hasDataPointsWithoutAttributes())
137140
.add(
138141
"jvm.memory.pool.committed",
139142
metric ->
140143
metric
141144
.hasDescription("current memory pool usage")
142-
.hasUnit("by")
145+
.hasUnit("By")
143146
.isGauge()
144-
.hasTypedDataPoints(gcLabels))
147+
.hasDataPointsWithAttributes(memoryAttributes))
145148
.add(
146149
"jvm.memory.pool.init",
147150
metric ->
148151
metric
149152
.hasDescription("current memory pool usage")
150-
.hasUnit("by")
153+
.hasUnit("By")
151154
.isGauge()
152-
.hasTypedDataPoints(gcLabels))
155+
.hasDataPointsWithAttributes(memoryAttributes))
153156
.add(
154157
"jvm.memory.pool.max",
155158
metric ->
156159
metric
157160
.hasDescription("current memory pool usage")
158-
.hasUnit("by")
161+
.hasUnit("By")
159162
.isGauge()
160-
.hasTypedDataPoints(gcLabels))
163+
.hasDataPointsWithAttributes(memoryAttributes))
161164
.add(
162165
"jvm.memory.pool.used",
163166
metric ->
164167
metric
165168
.hasDescription("current memory pool usage")
166-
.hasUnit("by")
169+
.hasUnit("By")
167170
.isGauge()
168-
.hasTypedDataPoints(gcLabels))
171+
.hasDataPointsWithAttributes(memoryAttributes))
169172
.add(
170173
"jvm.threads.count",
171174
metric ->
172175
metric
173176
.hasDescription("number of threads")
174-
.hasUnit("1")
177+
.hasUnit("{thread}")
175178
.isGauge()
176179
.hasDataPointsWithoutAttributes());
177180
}
181+
182+
private static AttributeMatcherGroup[] nameAttributeMatchers(String... values) {
183+
AttributeMatcherGroup[] groups = new AttributeMatcherGroup[values.length];
184+
for (int i = 0; i < values.length; i++) {
185+
groups[i] = attributeGroup(attribute("name", values[i]));
186+
}
187+
return groups;
188+
}
178189
}

0 commit comments

Comments
 (0)