Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger committed Feb 3, 2025
1 parent d8708df commit b19f64b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

package io.opentelemetry.instrumentation.testing;

import static io.opentelemetry.instrumentation.testing.internal.AwaitUtil.awaitUntilAsserted;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static org.awaitility.Awaitility.await;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.testing.util.TelemetryDataUtil;
Expand All @@ -18,6 +18,7 @@
import io.opentelemetry.sdk.testing.assertj.TraceAssert;
import io.opentelemetry.sdk.testing.assertj.TracesAssert;
import io.opentelemetry.sdk.trace.data.SpanData;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -29,6 +30,8 @@
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.assertj.core.api.ListAssert;
import org.awaitility.core.ConditionFactory;
import org.awaitility.core.ConditionTimeoutException;

/**
* This interface defines a common set of operations for interaction with OpenTelemetry SDK and
Expand Down Expand Up @@ -171,6 +174,13 @@ public final void waitAndAssertMetrics(
});
}

public final List<LogRecordData> waitForLogRecords(int numberOfLogRecords) {
awaitUntilAsserted(
() -> assertThat(getExportedLogRecords().size()).isEqualTo(numberOfLogRecords),
await().timeout(Duration.ofSeconds(20)));
return getExportedLogRecords();
}

private List<MetricData> instrumentationMetrics(String instrumentationName) {
return getExportedMetrics().stream()
.filter(m -> m.getInstrumentationScopeInfo().getName().equals(instrumentationName))
Expand Down Expand Up @@ -250,4 +260,29 @@ public final <T, E extends Throwable> T runWithNonRecordingSpan(ThrowingSupplier
throws E {
return testInstrumenters.runWithNonRecordingSpan(callback);
}

private static void awaitUntilAsserted(Runnable runnable) {
awaitUntilAsserted(runnable, await());
}

private static void awaitUntilAsserted(Runnable runnable, ConditionFactory conditionFactory) {
try {
conditionFactory.untilAsserted(runnable::run);
} catch (Throwable t) {
// awaitility is doing a jmx call that is not implemented in GraalVM:
// call:
// https://github.com/awaitility/awaitility/blob/fbe16add874b4260dd240108304d5c0be84eabc8/awaitility/src/main/java/org/awaitility/core/ConditionAwaiter.java#L157
// see https://github.com/oracle/graal/issues/6101 (spring boot graal native image)
if (t.getClass().getName().equals("com.oracle.svm.core.jdk.UnsupportedFeatureError")
|| t instanceof ConditionTimeoutException) {
// Don't throw this failure since the stack is the awaitility thread, causing confusion.
// Instead, just assert one more time on the test thread, which will fail with a better
// stack trace - that is on the same thread as the test.
// TODO: There is probably a better way to do this.
runnable.run();
} else {
throw t;
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

package io.opentelemetry.instrumentation.testing.junit;

import static io.opentelemetry.instrumentation.testing.internal.AwaitUtil.awaitUntilAsserted;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static org.awaitility.Awaitility.await;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.context.ContextStorage;
Expand All @@ -23,7 +21,6 @@
import io.opentelemetry.sdk.testing.assertj.MetricAssert;
import io.opentelemetry.sdk.testing.assertj.TraceAssert;
import io.opentelemetry.sdk.trace.data.SpanData;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
Expand Down Expand Up @@ -126,10 +123,7 @@ public List<List<SpanData>> waitForTraces(int numberOfTraces) {
* This waits up to 20 seconds, then times out.
*/
public List<LogRecordData> waitForLogRecords(int numberOfLogRecords) {
awaitUntilAsserted(
() -> assertThat(testRunner.getExportedLogRecords().size()).isEqualTo(numberOfLogRecords),
await().timeout(Duration.ofSeconds(20)));
return testRunner.getExportedLogRecords();
return testRunner.waitForLogRecords(numberOfLogRecords);
}

@SafeVarargs
Expand Down

0 comments on commit b19f64b

Please sign in to comment.