Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,18 @@ private ConfigProperties computeConfigProperties() {
}

// Visible for testing
@SuppressWarnings("SystemOut")
Thread shutdownHook(OpenTelemetrySdk sdk) {
return new Thread(sdk::close);
return new Thread(
() -> {
try {
sdk.close();
} catch (Throwable e) {
// https://github.com/open-telemetry/opentelemetry-java/issues/6827
// logging deps might not be on the classpath at this point
System.out.printf("%s Flush failed during shutdown: %s%n", Level.WARNING, e);
}
});
}

private static <I, O1, O2> BiFunction<I, ConfigProperties, O2> mergeCustomizer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
Expand Down Expand Up @@ -445,6 +446,22 @@ void builder_callAutoConfigureListeners() {
verify(listener).afterAutoConfigure(sdk);
}

@Test
void builder_catchesException() throws InterruptedException {
OpenTelemetrySdk sdk = mock(OpenTelemetrySdk.class);
doThrow(NoClassDefFoundError.class).when(sdk).close();

try {
Thread thread = builder.shutdownHook(sdk);
thread.start();
thread.join();
} catch (NoClassDefFoundError e) {
fail("shutdownHook threw unexpected NoClassDefFoundError", e);
}

verify(sdk).close();
}

private static Supplier<Map<String, String>> disableExportPropertySupplier() {
Map<String, String> props = new HashMap<>();
props.put("otel.metrics.exporter", "none");
Expand Down
Loading