Skip to content

Commit 0bb062e

Browse files
authored
fix: Improve reliability of metrics tests (#1763)
CI sometimes fails with errors like "http_server_requests_seconds not found" in the reported metrics. This looks like a race between the Quarkus metrics producer and the tests asking for these metrics. This change adds a time-limited retry loop until the expected metrics are available, before proceeding with other assertions. Note: in normal cases the loop finishes fast because the metrics are available. The two-minute timeout would apply only when the expected metrics fail to be produced at all.
1 parent 39189cd commit 0bb062e

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

quarkus/service/src/test/java/org/apache/polaris/service/quarkus/metrics/MetricsTestBase.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
import io.micrometer.core.instrument.MeterRegistry;
2626
import jakarta.inject.Inject;
2727
import jakarta.ws.rs.core.Response;
28+
import java.time.Duration;
2829
import java.util.Map;
30+
import java.util.concurrent.atomic.AtomicReference;
2931
import org.apache.polaris.service.quarkus.test.PolarisIntegrationTestFixture;
3032
import org.apache.polaris.service.quarkus.test.PolarisIntegrationTestHelper;
3133
import org.apache.polaris.service.quarkus.test.TestEnvironment;
@@ -41,6 +43,7 @@
4143
import org.junit.jupiter.api.extension.ExtendWith;
4244
import org.junit.jupiter.params.ParameterizedTest;
4345
import org.junit.jupiter.params.provider.ValueSource;
46+
import org.testcontainers.shaded.org.awaitility.Awaitility;
4447

4548
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
4649
@ExtendWith(TestEnvironmentExtension.class)
@@ -69,12 +72,26 @@ public void clearMetrics() {
6972
registry.clear();
7073
}
7174

75+
private Map<String, MetricFamily> fetchMetrics(String endpoint) {
76+
AtomicReference<Map<String, MetricFamily>> value = new AtomicReference<>();
77+
Awaitility.await()
78+
.atMost(Duration.ofMinutes(2))
79+
.untilAsserted(
80+
() -> {
81+
value.set(
82+
TestMetricsUtil.fetchMetrics(
83+
fixture.client, testEnv.baseManagementUri(), endpoint));
84+
assertThat(value.get()).containsKey(API_METRIC_NAME);
85+
assertThat(value.get()).containsKey(HTTP_METRIC_NAME);
86+
});
87+
return value.get();
88+
}
89+
7290
@ParameterizedTest
7391
@ValueSource(strings = {"%s/metrics", "%s/q/metrics"})
7492
public void testMetricsEmittedOnSuccessfulRequest(String endpoint) {
7593
sendSuccessfulRequest();
76-
Map<String, MetricFamily> allMetrics =
77-
TestMetricsUtil.fetchMetrics(fixture.client, testEnv.baseManagementUri(), endpoint);
94+
Map<String, MetricFamily> allMetrics = fetchMetrics(endpoint);
7895
assertThat(allMetrics).containsKey(API_METRIC_NAME);
7996
assertThat(allMetrics.get(API_METRIC_NAME).getMetrics())
8097
.satisfiesOnlyOnce(
@@ -125,8 +142,7 @@ public void testMetricsEmittedOnSuccessfulRequest(String endpoint) {
125142
@ValueSource(strings = {"%s/metrics", "%s/q/metrics"})
126143
public void testMetricsEmittedOnFailedRequest(String endpoint) {
127144
sendFailingRequest();
128-
Map<String, MetricFamily> allMetrics =
129-
TestMetricsUtil.fetchMetrics(fixture.client, testEnv.baseManagementUri(), endpoint);
145+
Map<String, MetricFamily> allMetrics = fetchMetrics(endpoint);
130146
assertThat(allMetrics).containsKey(API_METRIC_NAME);
131147
assertThat(allMetrics.get(API_METRIC_NAME).getMetrics())
132148
.satisfiesOnlyOnce(

0 commit comments

Comments
 (0)