Skip to content

Commit acbbbaf

Browse files
committed
Remove getOrSet, restore isSet
1 parent cacf0e0 commit acbbbaf

File tree

3 files changed

+22
-68
lines changed

3 files changed

+22
-68
lines changed

api/all/src/main/java/io/opentelemetry/api/GlobalOpenTelemetry.java

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,20 @@
4646
* not the OpenTelemetry javaagent is installed.
4747
* </ul>
4848
*
49-
* <p>Applications with custom instrumentation should call {@link #getOrSet(Supplier)} once during
50-
* initialization, and pass the resulting instance around manually (or with dependency injection) to
51-
* install custom instrumentation. This results in the following behavior:
49+
* <p>Applications with custom instrumentation should call {@link #isSet()} once during
50+
* initialization to access the javaagent instance or initialize (e.g. {@code isSet() ?
51+
* GlobalOpenTelemetry.get() : initializeSdk()}), and pass the resulting instance around manually
52+
* (or with dependency injection) to install custom instrumentation. This results in the following
53+
* behavior:
5254
*
5355
* <ul>
5456
* <li>If the OpenTelemetry javaagent is installed, custom instrumentation will use the {@link
5557
* OpenTelemetry} it installs.
56-
* <li>If the OpenTelemetry javaagent is not installed, custom instrumentation will use the {@link
57-
* OpenTelemetry} instance returned by {@link Supplier} passed to {@link #getOrSet(Supplier)}.
58+
* <li>If the OpenTelemetry javaagent is not installed, custom instrumentation will use an {@link
59+
* OpenTelemetry} instance initialized by the application.
5860
* </ul>
59-
*
60-
* @see TracerProvider
61-
* @see ContextPropagators
6261
*/
63-
// We intentionally assign to be use for error reporting.
62+
// We intentionally assign for error reporting.
6463
@SuppressWarnings("StaticAssignmentOfThrowable")
6564
public final class GlobalOpenTelemetry {
6665

@@ -102,26 +101,14 @@ public static OpenTelemetry getOrNoop() {
102101
}
103102

104103
/**
105-
* Returns the registered global {@link OpenTelemetry} if set, or else calls {@link
106-
* #set(Supplier)} with the {@code supplier}.
107-
*
108-
* <p>NOTE: if the global instance is set, the response is obfuscated to prevent callers from
109-
* casting to SDK implementation instances and inappropriately accessing non-instrumentation APIs.
110-
*
111-
* <p>NOTE: This does not result in the {@link #set(OpenTelemetry)} side effects of {@link
112-
* #get()}.
104+
* Returns {@code true} if {@link GlobalOpenTelemetry} is set, otherwise {@code false}.
113105
*
114106
* <p>Application custom instrumentation should use this method during initialization. See class
115107
* javadoc for more details.
116108
*/
117-
public static OpenTelemetry getOrSet(Supplier<OpenTelemetry> supplier) {
109+
public static boolean isSet() {
118110
synchronized (mutex) {
119-
if (globalOpenTelemetry == null) {
120-
OpenTelemetry openTelemetry = supplier.get();
121-
set(openTelemetry);
122-
return openTelemetry;
123-
}
124-
return globalOpenTelemetry;
111+
return globalOpenTelemetry != null;
125112
}
126113
}
127114

@@ -133,11 +120,11 @@ public static OpenTelemetry getOrSet(Supplier<OpenTelemetry> supplier) {
133120
* <p>NOTE: all returned instanced are obfuscated to prevent callers from casting to SDK
134121
* implementation instances and inappropriately accessing non-instrumentation APIs.
135122
*
136-
* <p>Native instrumentations should use {@link #getOrNoop()} instad. See class javadoc for more
123+
* <p>Native instrumentations should use {@link #getOrNoop()} instead. See class javadoc for more
137124
* details.
138125
*
139-
* <p>Application custom instrumentation should use {@link #getOrSet(Supplier)} instead. See class
140-
* javadoc for more details.
126+
* <p>Application custom instrumentation should use {@link #isSet()} and only call this if the
127+
* response is {@code true}. See class javadoc for more details.
141128
*
142129
* <p>If the global instance has not been set, and {@code
143130
* io.opentelemetry:opentelemetry-sdk-extension-autoconfigure} is present, and {@value

api/testing-internal/src/main/java/io/opentelemetry/api/testing/internal/AbstractOpenTelemetryTest.java

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
import io.opentelemetry.api.metrics.MeterProvider;
1515
import io.opentelemetry.api.trace.TracerProvider;
1616
import io.opentelemetry.context.propagation.ContextPropagators;
17-
import java.util.concurrent.atomic.AtomicInteger;
18-
import java.util.function.Supplier;
1917
import org.junit.jupiter.api.AfterEach;
2018
import org.junit.jupiter.api.BeforeAll;
2119
import org.junit.jupiter.api.Test;
@@ -88,9 +86,9 @@ void independentNonGlobalPropagators() {
8886

8987
@Test
9088
void setThenSet() {
91-
assertThat(GlobalOpenTelemetry.getOrNoop()).isSameAs(OpenTelemetry.noop());
89+
assertThat(GlobalOpenTelemetry.isSet()).isFalse();
9290
setOpenTelemetry();
93-
assertThat(GlobalOpenTelemetry.getOrNoop()).isNotSameAs(OpenTelemetry.noop());
91+
assertThat(GlobalOpenTelemetry.isSet()).isTrue();
9492
assertThatThrownBy(() -> GlobalOpenTelemetry.set(getOpenTelemetry()))
9593
.isInstanceOf(IllegalStateException.class)
9694
.hasMessageContaining("GlobalOpenTelemetry.set has already been called")
@@ -99,7 +97,9 @@ void setThenSet() {
9997

10098
@Test
10199
void getThenSet() {
100+
assertThat(GlobalOpenTelemetry.isSet()).isFalse();
102101
assertThat(GlobalOpenTelemetry.getOrNoop()).isSameAs(OpenTelemetry.noop());
102+
103103
// Calling GlobalOpenTelemetry.get() has the side affect of setting GlobalOpenTelemetry to an
104104
// (obfuscated) noop.
105105
// Call GlobalOpenTelemetry.get() using a utility method so we can later assert it was
@@ -111,7 +111,10 @@ void getThenSet() {
111111
.isEqualTo("io.opentelemetry.api.GlobalOpenTelemetry$ObfuscatedOpenTelemetry"))
112112
.extracting("delegate")
113113
.isSameAs(OpenTelemetry.noop());
114+
115+
assertThat(GlobalOpenTelemetry.isSet()).isTrue();
114116
assertThat(GlobalOpenTelemetry.getOrNoop()).isNotSameAs(OpenTelemetry.noop());
117+
115118
assertThatThrownBy(() -> GlobalOpenTelemetry.set(getOpenTelemetry()))
116119
.isInstanceOf(IllegalStateException.class)
117120
.hasMessageContaining("GlobalOpenTelemetry.set has already been called")
@@ -131,42 +134,6 @@ void getOrNoop() {
131134
assertThat(GlobalOpenTelemetry.getOrNoop()).isNotSameAs(OpenTelemetry.noop());
132135
}
133136

134-
@Test
135-
void getOrSet_NotPreviouslySet() {
136-
AtomicInteger supplierCallCount = new AtomicInteger();
137-
Supplier<OpenTelemetry> supplier =
138-
() -> {
139-
supplierCallCount.incrementAndGet();
140-
return OpenTelemetry.noop();
141-
};
142-
143-
assertThat(GlobalOpenTelemetry.getOrSet(supplier)).isSameAs(OpenTelemetry.noop());
144-
assertThat(supplierCallCount.get()).isEqualTo(1);
145-
146-
// The second time getOrSet is called, we get an obfuscated instance
147-
assertThat(GlobalOpenTelemetry.getOrSet(supplier).getClass().getName())
148-
.isEqualTo("io.opentelemetry.api.GlobalOpenTelemetry$ObfuscatedOpenTelemetry");
149-
assertThat(supplierCallCount.get()).isEqualTo(1);
150-
}
151-
152-
@Test
153-
void getOrSet_PreviouslySet() {
154-
setOpenTelemetry();
155-
156-
AtomicInteger supplierCallCount = new AtomicInteger();
157-
Supplier<OpenTelemetry> supplier =
158-
() -> {
159-
supplierCallCount.incrementAndGet();
160-
return OpenTelemetry.noop();
161-
};
162-
163-
assertThat(GlobalOpenTelemetry.getOrSet(supplier).getClass().getName())
164-
.isEqualTo("io.opentelemetry.api.GlobalOpenTelemetry$ObfuscatedOpenTelemetry");
165-
assertThat(GlobalOpenTelemetry.getOrSet(supplier).getClass().getName())
166-
.isEqualTo("io.opentelemetry.api.GlobalOpenTelemetry$ObfuscatedOpenTelemetry");
167-
assertThat(supplierCallCount.get()).isEqualTo(0);
168-
}
169-
170137
@Test
171138
void toString_noop_Valid() {
172139
assertThat(getOpenTelemetry().toString())

docs/apidiffs/current_vs_latest/opentelemetry-api.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ Comparing source compatibility of opentelemetry-api-1.57.0-SNAPSHOT.jar against
22
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.api.GlobalOpenTelemetry (not serializable)
33
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
44
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.api.OpenTelemetry getOrNoop()
5-
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.api.OpenTelemetry getOrSet(java.util.function.Supplier<io.opentelemetry.api.OpenTelemetry>)
5+
+++ NEW METHOD: PUBLIC(+) STATIC(+) boolean isSet()

0 commit comments

Comments
 (0)