5252 */
5353public abstract class InstrumentationTestRunner {
5454
55- private final TestInstrumenters testInstrumenters ;
55+ private final OpenTelemetry openTelemetry ;
56+ // Lazy initialized so that test runners can load without triggering Instrumenter construction
57+ // in the OpenTelemetry API bridging tests where some of the newer OpenTelemetry APIs used by
58+ // Instrumenter are absent.
59+ @ Nullable private TestInstrumenters testInstrumenters ;
5660 protected Map <InstrumentationScopeInfo , Map <String , MetricData >> metricsByScope = new HashMap <>();
5761 protected Set <InstrumentationScopeInfo > instrumentationScopes = new HashSet <>();
5862
@@ -65,7 +69,7 @@ public abstract class InstrumentationTestRunner {
6569 tracesByScope = new HashMap <>();
6670
6771 protected InstrumentationTestRunner (OpenTelemetry openTelemetry ) {
68- testInstrumenters = new TestInstrumenters ( openTelemetry ) ;
72+ this . openTelemetry = openTelemetry ;
6973 }
7074
7175 public abstract void beforeTestClass ();
@@ -285,7 +289,7 @@ public final <E extends Exception> void runWithSpan(String spanName, ThrowingRun
285289 */
286290 public final <T , E extends Throwable > T runWithSpan (
287291 String spanName , ThrowingSupplier <T , E > callback ) throws E {
288- return testInstrumenters .runWithSpan (spanName , callback );
292+ return getTestInstrumenters () .runWithSpan (spanName , callback );
289293 }
290294
291295 /**
@@ -308,7 +312,7 @@ public final <E extends Throwable> void runWithHttpClientSpan(
308312 */
309313 public final <T , E extends Throwable > T runWithHttpClientSpan (
310314 String spanName , ThrowingSupplier <T , E > callback ) throws E {
311- return testInstrumenters .runWithHttpClientSpan (spanName , callback );
315+ return getTestInstrumenters () .runWithHttpClientSpan (spanName , callback );
312316 }
313317
314318 /**
@@ -330,13 +334,20 @@ public final <E extends Throwable> void runWithHttpServerSpan(ThrowingRunnable<E
330334 */
331335 public final <T , E extends Throwable > T runWithHttpServerSpan (ThrowingSupplier <T , E > callback )
332336 throws E {
333- return testInstrumenters .runWithHttpServerSpan (callback );
337+ return getTestInstrumenters () .runWithHttpServerSpan (callback );
334338 }
335339
336340 /** Runs the provided {@code callback} inside the scope of a non-recording span. */
337341 public final <T , E extends Throwable > T runWithNonRecordingSpan (ThrowingSupplier <T , E > callback )
338342 throws E {
339- return testInstrumenters .runWithNonRecordingSpan (callback );
343+ return getTestInstrumenters ().runWithNonRecordingSpan (callback );
344+ }
345+
346+ private TestInstrumenters getTestInstrumenters () {
347+ if (testInstrumenters == null ) {
348+ testInstrumenters = new TestInstrumenters (openTelemetry );
349+ }
350+ return testInstrumenters ;
340351 }
341352
342353 private static void awaitUntilAsserted (Runnable runnable ) {
0 commit comments