66package io .opentelemetry .instrumentation .testing ;
77
88import static io .opentelemetry .sdk .testing .assertj .OpenTelemetryAssertions .assertThat ;
9- import static org .awaitility .Awaitility .await ;
109
1110import io .opentelemetry .api .OpenTelemetry ;
11+ import io .opentelemetry .instrumentation .testing .internal .AwaitUtil ;
1212import io .opentelemetry .instrumentation .testing .util .TelemetryDataUtil ;
1313import io .opentelemetry .instrumentation .testing .util .ThrowingRunnable ;
1414import io .opentelemetry .instrumentation .testing .util .ThrowingSupplier ;
2929import java .util .stream .Collectors ;
3030import javax .annotation .Nullable ;
3131import org .assertj .core .api .ListAssert ;
32- import org .awaitility .core .ConditionTimeoutException ;
3332
3433/**
3534 * This interface defines a common set of operations for interaction with OpenTelemetry SDK and
@@ -118,25 +117,8 @@ private <T extends Consumer<TraceAssert>> void waitAndAssertTraces(
118117 List <T > assertionsList = new ArrayList <>();
119118 assertions .forEach (assertionsList ::add );
120119
121- try {
122- await ()
123- .untilAsserted (() -> doAssertTraces (traceComparator , assertionsList , verifyScopeVersion ));
124- } catch (Throwable t ) {
125- // awaitility is doing a jmx call that is not implemented in GraalVM:
126- // call:
127- // https://github.com/awaitility/awaitility/blob/fbe16add874b4260dd240108304d5c0be84eabc8/awaitility/src/main/java/org/awaitility/core/ConditionAwaiter.java#L157
128- // see https://github.com/oracle/graal/issues/6101 (spring boot graal native image)
129- if (t .getClass ().getName ().equals ("com.oracle.svm.core.jdk.UnsupportedFeatureError" )
130- || t instanceof ConditionTimeoutException ) {
131- // Don't throw this failure since the stack is the awaitility thread, causing confusion.
132- // Instead, just assert one more time on the test thread, which will fail with a better
133- // stack trace.
134- // TODO: There is probably a better way to do this.
135- doAssertTraces (traceComparator , assertionsList , verifyScopeVersion );
136- } else {
137- throw t ;
138- }
139- }
120+ AwaitUtil .awaitUntilAsserted (
121+ () -> doAssertTraces (traceComparator , assertionsList , verifyScopeVersion ));
140122 }
141123
142124 private <T extends Consumer <TraceAssert >> void doAssertTraces (
@@ -159,31 +141,35 @@ private <T extends Consumer<TraceAssert>> void doAssertTraces(
159141 */
160142 public final void waitAndAssertMetrics (
161143 String instrumentationName , String metricName , Consumer <ListAssert <MetricData >> assertion ) {
162- await ()
163- .untilAsserted (
164- () ->
165- assertion .accept (
166- assertThat (getExportedMetrics ())
167- .filteredOn (
168- data ->
169- data .getInstrumentationScopeInfo ()
170- .getName ()
171- .equals (instrumentationName )
172- && data .getName ().equals (metricName ))));
144+
145+ AwaitUtil .awaitUntilAsserted (
146+ () ->
147+ assertion .accept (
148+ assertThat (getExportedMetrics ())
149+ .describedAs (
150+ "Metrics for instrumentation %s and metric name %s" ,
151+ instrumentationName , metricName )
152+ .filteredOn (
153+ data ->
154+ data .getInstrumentationScopeInfo ().getName ().equals (instrumentationName )
155+ && data .getName ().equals (metricName ))));
173156 }
174157
175158 @ SafeVarargs
176159 public final void waitAndAssertMetrics (
177160 String instrumentationName , Consumer <MetricAssert >... assertions ) {
178- await ()
179- .untilAsserted (
180- () -> {
181- Collection <MetricData > metrics = instrumentationMetrics (instrumentationName );
182- assertThat (metrics ).isNotEmpty ();
183- for (Consumer <MetricAssert > assertion : assertions ) {
184- assertThat (metrics ).anySatisfy (metric -> assertion .accept (assertThat (metric )));
185- }
186- });
161+ AwaitUtil .awaitUntilAsserted (
162+ () -> {
163+ Collection <MetricData > metrics = instrumentationMetrics (instrumentationName );
164+ assertThat (metrics ).isNotEmpty ();
165+ for (int i = 0 ; i < assertions .length ; i ++) {
166+ int index = i ;
167+ assertThat (metrics )
168+ .describedAs (
169+ "Metrics for instrumentation %s and assertion %d" , instrumentationName , index )
170+ .anySatisfy (metric -> assertions [index ].accept (assertThat (metric )));
171+ }
172+ });
187173 }
188174
189175 private List <MetricData > instrumentationMetrics (String instrumentationName ) {
0 commit comments