Skip to content

Commit 7ea2dd9

Browse files
fix: change exception logging when running in maven (#7336)
1 parent ed1e201 commit 7ea2dd9

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkBuilder.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,8 +649,18 @@ private ConfigProperties computeConfigProperties() {
649649
}
650650

651651
// Visible for testing
652+
@SuppressWarnings("SystemOut")
652653
Thread shutdownHook(OpenTelemetrySdk sdk) {
653-
return new Thread(sdk::close);
654+
return new Thread(
655+
() -> {
656+
try {
657+
sdk.close();
658+
} catch (Throwable e) {
659+
// https://github.com/open-telemetry/opentelemetry-java/issues/6827
660+
// logging deps might not be on the classpath at this point
661+
System.out.printf("%s Flush failed during shutdown: %s%n", Level.WARNING, e);
662+
}
663+
});
654664
}
655665

656666
private static <I, O1, O2> BiFunction<I, ConfigProperties, O2> mergeCustomizer(

sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import static org.assertj.core.api.Assertions.assertThat;
1111
import static org.assertj.core.api.Assertions.assertThatCode;
1212
import static org.assertj.core.api.Assertions.assertThatThrownBy;
13+
import static org.junit.jupiter.api.Assertions.fail;
1314
import static org.mockito.ArgumentMatchers.any;
1415
import static org.mockito.Mockito.doReturn;
1516
import static org.mockito.Mockito.doThrow;
@@ -445,6 +446,22 @@ void builder_callAutoConfigureListeners() {
445446
verify(listener).afterAutoConfigure(sdk);
446447
}
447448

449+
@Test
450+
void builder_catchesException() throws InterruptedException {
451+
OpenTelemetrySdk sdk = mock(OpenTelemetrySdk.class);
452+
doThrow(NoClassDefFoundError.class).when(sdk).close();
453+
454+
try {
455+
Thread thread = builder.shutdownHook(sdk);
456+
thread.start();
457+
thread.join();
458+
} catch (NoClassDefFoundError e) {
459+
fail("shutdownHook threw unexpected NoClassDefFoundError", e);
460+
}
461+
462+
verify(sdk).close();
463+
}
464+
448465
private static Supplier<Map<String, String>> disableExportPropertySupplier() {
449466
Map<String, String> props = new HashMap<>();
450467
props.put("otel.metrics.exporter", "none");

0 commit comments

Comments
 (0)