Skip to content

Commit f749213

Browse files
committed
OpenTelemetry - Avoid checking metrics not existing on Semeru
Also improves assertions and error messages.
1 parent a69eac4 commit f749213

File tree

2 files changed

+89
-80
lines changed
  • extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/metrics
  • integration-tests/opentelemetry/src/test/java/io/quarkus/it/opentelemetry

2 files changed

+89
-80
lines changed

extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/metrics/JvmMetricsTest.java

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import static io.opentelemetry.sdk.metrics.data.MetricDataType.*;
44
import static java.util.concurrent.TimeUnit.SECONDS;
5-
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
5+
import static org.assertj.core.api.Assertions.assertThat;
66
import static org.hamcrest.Matchers.is;
7-
import static org.junit.jupiter.api.Assertions.assertEquals;
87

8+
import java.util.HashSet;
99
import java.util.List;
1010
import java.util.Set;
1111
import java.util.stream.Collectors;
@@ -25,6 +25,7 @@
2525
import io.quarkus.opentelemetry.deployment.common.exporter.InMemoryMetricExporter;
2626
import io.quarkus.opentelemetry.deployment.common.exporter.InMemoryMetricExporterProvider;
2727
import io.quarkus.test.QuarkusUnitTest;
28+
import io.quarkus.test.common.JdkUtil;
2829
import io.restassured.RestAssured;
2930

3031
/**
@@ -48,36 +49,42 @@ public class JvmMetricsTest extends BaseJvmMetricsTest {
4849

4950
@Test
5051
void allMetrics() throws InterruptedException {
51-
Set<MetricToAssert> allMetrics = Set.of(
52-
new JvmMetricsTest.MetricToAssert("http.server.request.duration", "Duration of HTTP server requests.", "s",
53-
HISTOGRAM), // just because we generate load with HTTP
54-
new JvmMetricsTest.MetricToAssert("jvm.memory.committed", "Measure of memory committed.", "By", LONG_SUM),
55-
new JvmMetricsTest.MetricToAssert("jvm.memory.used", "Measure of memory used.", "By", LONG_SUM),
56-
new JvmMetricsTest.MetricToAssert("jvm.memory.limit", "Measure of max obtainable memory.", "By", LONG_SUM),
57-
new JvmMetricsTest.MetricToAssert("jvm.memory.used_after_last_gc",
58-
"Measure of memory used, as measured after the most recent garbage collection event on this pool.",
59-
"By", LONG_SUM),
60-
new JvmMetricsTest.MetricToAssert("jvm.gc.duration", "Duration of JVM garbage collection actions.", "s",
61-
HISTOGRAM),
62-
new JvmMetricsTest.MetricToAssert("jvm.class.count", "Number of classes currently loaded.", "{class}",
63-
LONG_SUM),
64-
new JvmMetricsTest.MetricToAssert("jvm.class.loaded", "Number of classes loaded since JVM start.", "{class}",
65-
LONG_SUM),
66-
new JvmMetricsTest.MetricToAssert("jvm.class.unloaded", "Number of classes unloaded since JVM start.",
67-
"{class}", LONG_SUM),
68-
new JvmMetricsTest.MetricToAssert("jvm.cpu.count",
69-
"Number of processors available to the Java virtual machine.", "{cpu}", LONG_SUM),
70-
new JvmMetricsTest.MetricToAssert("jvm.cpu.limit", "", "1", LONG_SUM),
71-
new JvmMetricsTest.MetricToAssert("jvm.cpu.time", "CPU time used by the process as reported by the JVM.", "s",
72-
DOUBLE_SUM),
73-
new JvmMetricsTest.MetricToAssert("jvm.cpu.recent_utilization",
74-
"Recent CPU utilization for the process as reported by the JVM.", "1", DOUBLE_GAUGE),
75-
new JvmMetricsTest.MetricToAssert("jvm.cpu.longlock", "Long lock times", "s", HISTOGRAM),
76-
new JvmMetricsTest.MetricToAssert("jvm.cpu.context_switch", "", "Hz", DOUBLE_SUM),
77-
new JvmMetricsTest.MetricToAssert("jvm.network.io", "Network read/write bytes.", "By", HISTOGRAM), //
78-
new JvmMetricsTest.MetricToAssert("jvm.network.time", "Network read/write duration.", "s", HISTOGRAM), //
79-
new JvmMetricsTest.MetricToAssert("jvm.thread.count", "Number of executing platform threads.", "{thread}",
80-
LONG_SUM));
52+
Set<MetricToAssert> allMetrics = new HashSet<>();
53+
54+
allMetrics.add(new MetricToAssert("http.server.request.duration", "Duration of HTTP server requests.", "s", HISTOGRAM)); // just because we generate load with HTTP
55+
allMetrics.add(new MetricToAssert("jvm.memory.committed", "Measure of memory committed.", "By", LONG_SUM));
56+
allMetrics.add(new MetricToAssert("jvm.memory.used", "Measure of memory used.", "By", LONG_SUM));
57+
// Not on native
58+
allMetrics.add(new MetricToAssert("jvm.memory.limit", "Measure of max obtainable memory.", "By", LONG_SUM));
59+
allMetrics.add(new MetricToAssert("jvm.memory.used_after_last_gc",
60+
"Measure of memory used, as measured after the most recent garbage collection event on this pool.",
61+
"By", LONG_SUM));
62+
// not on native
63+
allMetrics.add(new MetricToAssert("jvm.gc.duration", "Duration of JVM garbage collection actions.", "s", HISTOGRAM));
64+
allMetrics.add(new MetricToAssert("jvm.class.count", "Number of classes currently loaded.", "{class}", LONG_SUM));
65+
allMetrics
66+
.add(new MetricToAssert("jvm.class.loaded", "Number of classes loaded since JVM start.", "{class}", LONG_SUM));
67+
allMetrics.add(
68+
new MetricToAssert("jvm.class.unloaded", "Number of classes unloaded since JVM start.", "{class}", LONG_SUM));
69+
allMetrics.add(new MetricToAssert("jvm.cpu.count", "Number of processors available to the Java virtual machine.",
70+
"{cpu}", LONG_SUM));
71+
// jvm.system.cpu.utilization instead, on native
72+
allMetrics.add(
73+
new MetricToAssert("jvm.cpu.time", "CPU time used by the process as reported by the JVM.", "s", DOUBLE_SUM));
74+
allMetrics.add(new MetricToAssert("jvm.cpu.recent_utilization",
75+
"Recent CPU utilization for the process as reported by the JVM.", "1", DOUBLE_GAUGE));
76+
allMetrics.add(new MetricToAssert("jvm.thread.count", "Number of executing platform threads.", "{thread}", LONG_SUM));
77+
78+
// not supported on Semeru
79+
if (!JdkUtil.isSemeru()) {
80+
allMetrics.add(new MetricToAssert("jvm.cpu.limit", "", "1", LONG_SUM));
81+
allMetrics.add(new MetricToAssert("jvm.cpu.longlock", "Long lock times", "s", HISTOGRAM));
82+
allMetrics.add(new MetricToAssert("jvm.cpu.context_switch", "", "Hz", DOUBLE_SUM));
83+
84+
// not on native
85+
allMetrics.add(new MetricToAssert("jvm.network.io", "Network read/write bytes.", "By", HISTOGRAM));
86+
allMetrics.add(new MetricToAssert("jvm.network.time", "Network read/write duration.", "s", HISTOGRAM));
87+
}
8188

8289
// Force GC to run
8390
System.gc();
@@ -89,14 +96,11 @@ void allMetrics() throws InterruptedException {
8996
.body(is("hello"));
9097

9198
Awaitility.await().atMost(10, SECONDS)
92-
.untilAsserted(() -> assertEquals(allMetrics.size(),
93-
metricExporter.getFinishedMetricItems().stream()
94-
.map(MetricData::getName)
95-
.collect(Collectors.toSet())
96-
.size(),
97-
"Found: " + metricExporter.getFinishedMetricItems().stream()
99+
.untilAsserted(
100+
() -> assertThat(metricExporter.getFinishedMetricItems().stream()
98101
.map(MetricData::getName)
99-
.collect(Collectors.toSet())));
102+
.collect(Collectors.toSet())).containsExactlyInAnyOrderElementsOf(
103+
allMetrics.stream().map(MetricToAssert::name).toList()));
100104

101105
List<MetricData> finishedMetricItems = metricExporter.getFinishedMetricItems();
102106

integration-tests/opentelemetry/src/test/java/io/quarkus/it/opentelemetry/MetricsTest.java

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
import static io.restassured.RestAssured.given;
66
import static java.net.HttpURLConnection.HTTP_OK;
77
import static java.util.concurrent.TimeUnit.SECONDS;
8-
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
8+
import static org.assertj.core.api.Assertions.assertThat;
99
import static org.awaitility.Awaitility.await;
1010
import static org.junit.jupiter.api.Assertions.assertEquals;
1111

12+
import java.util.HashSet;
1213
import java.util.List;
1314
import java.util.Map;
1415
import java.util.Set;
@@ -19,6 +20,7 @@
1920
import org.junit.jupiter.api.Test;
2021

2122
import io.opentelemetry.sdk.metrics.data.MetricDataType;
23+
import io.quarkus.test.common.JdkUtil;
2224
import io.quarkus.test.junit.QuarkusTest;
2325
import io.restassured.common.mapper.TypeRef;
2426

@@ -98,18 +100,17 @@ void testAllJvmMetrics() {
98100

99101
await().atMost(10, SECONDS).untilAsserted(() -> {
100102
Set<String> allMetricNames = getAllMetricNames("jvm.");
101-
assertThat(allMetricNames.size())
102-
.withFailMessage("The jvm metrics are " + allMetricNames)
103-
.isGreaterThanOrEqualTo(allMetrics.size());
103+
assertThat(allMetricNames)
104+
.containsAll(allMetrics.stream().map(MetricToAssert::name).toList());
104105
});
105106

106107
allMetrics.forEach(metricToAssert -> {
107108

108109
// metric is there and has at least 1 reading
109110
await().atMost(10, SECONDS)
110-
.untilAsserted(() -> assertThat(getMetrics(metricToAssert.name()).size())
111-
.withFailMessage("The metric " + metricToAssert.name())
112-
.isGreaterThan(0));
111+
.untilAsserted(() -> assertThat(getMetrics(metricToAssert.name()))
112+
.withFailMessage("The metric " + metricToAssert.name() + " is not defined")
113+
.hasSizeGreaterThan(0));
113114

114115
// skip assertions from flaky metrics
115116
if (!metricToAssert.name().equals("jvm.memory.used_after_last_gc") &&
@@ -164,40 +165,44 @@ record MetricToAssert(String name, String description, String metricUnit, Metric
164165
}
165166

166167
protected Set<MetricToAssert> getJvmMetricsToAssert() {
167-
return Set.of(
168-
// new MetricToAssert("http.server.request.duration", "Duration of HTTP server requests.", "s",
169-
// HISTOGRAM), // just because we generate load with HTTP
170-
new MetricToAssert("jvm.memory.committed", "Measure of memory committed.", "By", LONG_SUM),
171-
new MetricToAssert("jvm.memory.used", "Measure of memory used.", "By", LONG_SUM),
172-
// Not on native
173-
new MetricToAssert("jvm.memory.limit", "Measure of max obtainable memory.", "By", LONG_SUM),
174-
new MetricToAssert("jvm.memory.used_after_last_gc",
175-
"Measure of memory used, as measured after the most recent garbage collection event on this pool.",
176-
"By", LONG_SUM),
177-
// not on native
178-
new MetricToAssert("jvm.gc.duration", "Duration of JVM garbage collection actions.", "s",
179-
HISTOGRAM),
180-
new MetricToAssert("jvm.class.count", "Number of classes currently loaded.", "{class}",
181-
LONG_SUM),
182-
new MetricToAssert("jvm.class.loaded", "Number of classes loaded since JVM start.", "{class}",
183-
LONG_SUM),
184-
new MetricToAssert("jvm.class.unloaded", "Number of classes unloaded since JVM start.",
185-
"{class}", LONG_SUM),
186-
new MetricToAssert("jvm.cpu.count",
187-
"Number of processors available to the Java virtual machine.", "{cpu}", LONG_SUM),
188-
new MetricToAssert("jvm.cpu.limit", "", "1", LONG_SUM),
189-
//jvm.system.cpu.utilization instead, on native
190-
new MetricToAssert("jvm.cpu.time", "CPU time used by the process as reported by the JVM.", "s",
191-
DOUBLE_SUM),
192-
new MetricToAssert("jvm.cpu.recent_utilization",
193-
"Recent CPU utilization for the process as reported by the JVM.", "1", DOUBLE_GAUGE),
194-
new MetricToAssert("jvm.cpu.longlock", "Long lock times", "s", HISTOGRAM),
195-
new MetricToAssert("jvm.cpu.context_switch", "", "Hz", DOUBLE_SUM),
196-
// not on native
197-
new MetricToAssert("jvm.network.io", "Network read/write bytes.", "By", HISTOGRAM),
198-
new MetricToAssert("jvm.network.time", "Network read/write duration.", "s", HISTOGRAM),
199-
new MetricToAssert("jvm.thread.count", "Number of executing platform threads.", "{thread}",
200-
LONG_SUM));
168+
Set<MetricToAssert> jvmMetrics = new HashSet<>();
169+
170+
// metrics.add(new MetricToAssert("http.server.request.duration", "Duration of HTTP server requests.", "s", HISTOGRAM)); // just because we generate load with HTTP
171+
jvmMetrics.add(new MetricToAssert("jvm.memory.committed", "Measure of memory committed.", "By", LONG_SUM));
172+
jvmMetrics.add(new MetricToAssert("jvm.memory.used", "Measure of memory used.", "By", LONG_SUM));
173+
// Not on native
174+
jvmMetrics.add(new MetricToAssert("jvm.memory.limit", "Measure of max obtainable memory.", "By", LONG_SUM));
175+
jvmMetrics.add(new MetricToAssert("jvm.memory.used_after_last_gc",
176+
"Measure of memory used, as measured after the most recent garbage collection event on this pool.",
177+
"By", LONG_SUM));
178+
// not on native
179+
jvmMetrics.add(new MetricToAssert("jvm.gc.duration", "Duration of JVM garbage collection actions.", "s", HISTOGRAM));
180+
jvmMetrics.add(new MetricToAssert("jvm.class.count", "Number of classes currently loaded.", "{class}", LONG_SUM));
181+
jvmMetrics
182+
.add(new MetricToAssert("jvm.class.loaded", "Number of classes loaded since JVM start.", "{class}", LONG_SUM));
183+
jvmMetrics.add(
184+
new MetricToAssert("jvm.class.unloaded", "Number of classes unloaded since JVM start.", "{class}", LONG_SUM));
185+
jvmMetrics.add(new MetricToAssert("jvm.cpu.count", "Number of processors available to the Java virtual machine.",
186+
"{cpu}", LONG_SUM));
187+
// jvm.system.cpu.utilization instead, on native
188+
jvmMetrics.add(
189+
new MetricToAssert("jvm.cpu.time", "CPU time used by the process as reported by the JVM.", "s", DOUBLE_SUM));
190+
jvmMetrics.add(new MetricToAssert("jvm.cpu.recent_utilization",
191+
"Recent CPU utilization for the process as reported by the JVM.", "1", DOUBLE_GAUGE));
192+
jvmMetrics.add(new MetricToAssert("jvm.thread.count", "Number of executing platform threads.", "{thread}", LONG_SUM));
193+
194+
// not supported on Semeru
195+
if (!JdkUtil.isSemeru()) {
196+
jvmMetrics.add(new MetricToAssert("jvm.cpu.limit", "", "1", LONG_SUM));
197+
jvmMetrics.add(new MetricToAssert("jvm.cpu.longlock", "Long lock times", "s", HISTOGRAM));
198+
jvmMetrics.add(new MetricToAssert("jvm.cpu.context_switch", "", "Hz", DOUBLE_SUM));
199+
200+
// not on native
201+
jvmMetrics.add(new MetricToAssert("jvm.network.io", "Network read/write bytes.", "By", HISTOGRAM));
202+
jvmMetrics.add(new MetricToAssert("jvm.network.time", "Network read/write duration.", "s", HISTOGRAM));
203+
}
204+
205+
return jvmMetrics;
201206
}
202207

203208
private Double getLastReading(MetricToAssert metricToAssert) {

0 commit comments

Comments
 (0)