Skip to content

Commit 89181a9

Browse files
committed
test
1 parent 1d5245f commit 89181a9

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -577,12 +577,23 @@ private void maybeRegisterShutdownHook(OpenTelemetrySdk openTelemetrySdk) {
577577
Runtime.getRuntime().addShutdownHook(shutdownHook(openTelemetrySdk));
578578
}
579579

580-
@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
581580
private <T> T maybeRunWithGlobalOpenTelemetryLock(Supplier<T> supplier) {
582581
if (setResultAsGlobal) {
583582
return supplier.get();
584583
}
585584

585+
Object mutex = getGlobalOpenTelemetryLock();
586+
if (mutex == null) {
587+
return supplier.get();
588+
}
589+
synchronized (mutex) {
590+
return supplier.get();
591+
}
592+
}
593+
594+
// Visible for testing
595+
@Nullable
596+
static Object getGlobalOpenTelemetryLock() {
586597
Object mutex = null;
587598
try {
588599
Field mutexField = GlobalOpenTelemetry.class.getDeclaredField("mutex");
@@ -596,13 +607,7 @@ private <T> T maybeRunWithGlobalOpenTelemetryLock(Supplier<T> supplier) {
596607
} catch (Exception exception) {
597608
logger.log(Level.WARNING, "Could not acquire Global OpenTelemetry mutex", exception);
598609
}
599-
600-
if (mutex == null) {
601-
return supplier.get();
602-
}
603-
synchronized (mutex) {
604-
return supplier.get();
605-
}
610+
return mutex;
606611
}
607612

608613
private void maybeSetAsGlobal(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.sdk.autoconfigure;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
import org.junit.jupiter.api.Test;
11+
12+
class AutoConfiguredOpenTelemetrySdkBuilderTest {
13+
14+
@Test
15+
void getGlobalOpenTelemetryLock_findsLock() {
16+
assertThat(AutoConfiguredOpenTelemetrySdkBuilder.getGlobalOpenTelemetryLock()).isNotNull();
17+
}
18+
}

0 commit comments

Comments
 (0)