-
Notifications
You must be signed in to change notification settings - Fork 923
Open
Description
When using OpenTelemetryExtension with JUnit 5 @Nested test classes, the SDK is closed prematurely after the first nested class completes, causing subsequent nested classes to have non-functional metrics/spans/logs.
Root Cause
The afterAll() callback in OpenTelemetryExtension calls openTelemetry.close():
@Override
public void afterAll(ExtensionContext context) {
GlobalOpenTelemetry.resetForTest();
openTelemetry.close();
}In JUnit 5, @AfterAll (and thus afterAll() extension callbacks) is called after each nested class completes, not just once at the end of the outer test class. This means:
- First
@Nestedclass runs →afterAll()→openTelemetry.close()called - Second
@Nestedclass runs → SDK is already closed, metrics are not recorded - Subsequent
@Nestedclasses → Same issue
Reproduction
@DisplayName("My Test")
public class MyTest {
@RegisterExtension
static final OpenTelemetryExtension otelTesting = OpenTelemetryExtension.create();
@Nested
@DisplayName("First nested class")
class FirstNestedTests {
@Test
void test1() {
// This test works - metrics are recorded
var meter = otelTesting.getOpenTelemetry().getMeter("test");
var counter = meter.counterBuilder("test.counter").build();
counter.add(1);
assertThat(otelTesting.getMetrics()).isNotEmpty(); // PASSES
}
}
@Nested
@DisplayName("Second nested class")
class SecondNestedTests {
@Test
void test2() {
// This test fails - SDK was closed after FirstNestedTests
var meter = otelTesting.getOpenTelemetry().getMeter("test");
var counter = meter.counterBuilder("test.counter").build();
counter.add(1);
assertThat(otelTesting.getMetrics()).isNotEmpty(); // FAILS - metrics are empty
}
}
}Workaround
Currently the only workaround is to use @TestClassOrder with @Order annotations to ensure test classes that depend on metrics run first:
@TestClassOrder(ClassOrderer.OrderAnnotation.class)
public class MyTest {
@Nested
@Order(1) // Run first before SDK is closed
class MetricsTests { ... }
@Nested
@Order(2)
class OtherTests { ... }
}This is pretty clunky though. It might also work to recreate the extension in each nested test, but that kind of defeats the purpose of nesting.
Environment
- OpenTelemetry SDK version: 1.56.0
- JUnit 5 version: 5.x / 6.x
- Java version: 11+
Metadata
Metadata
Assignees
Labels
No labels