Skip to content

Commit

Permalink
AutoConfiguredOpenTelemetrySdkBuilder does not set GlobalOpenTelemetr…
Browse files Browse the repository at this point in the history
…y by default (#5564)
  • Loading branch information
jack-berg authored Jun 22, 2023
1 parent cb6d713 commit 0e318fb
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public abstract class AutoConfiguredOpenTelemetrySdk {
* <p>This will automatically set the resulting SDK as the {@link GlobalOpenTelemetry} instance.
*/
public static AutoConfiguredOpenTelemetrySdk initialize() {
return builder().build();
return builder().setResultAsGlobal().build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public final class AutoConfiguredOpenTelemetrySdkBuilder implements AutoConfigur

private boolean registerShutdownHook = true;

private boolean setResultAsGlobal = true;
private boolean setResultAsGlobal = false;

private boolean customized;

Expand Down Expand Up @@ -297,9 +297,11 @@ public AutoConfiguredOpenTelemetrySdkBuilder disableShutdownHook() {
/**
* Sets whether the configured {@link OpenTelemetrySdk} should be set as the application's
* {@linkplain io.opentelemetry.api.GlobalOpenTelemetry global} instance.
*
* <p>By default, {@link GlobalOpenTelemetry} is not set.
*/
public AutoConfiguredOpenTelemetrySdkBuilder setResultAsGlobal(boolean setResultAsGlobal) {
this.setResultAsGlobal = setResultAsGlobal;
public AutoConfiguredOpenTelemetrySdkBuilder setResultAsGlobal() {
this.setResultAsGlobal = true;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ void resetGlobal() {
GlobalEventEmitterProvider.resetForTest();
builder =
AutoConfiguredOpenTelemetrySdk.builder()
.setResultAsGlobal(false)
.addPropertiesSupplier(disableExportPropertySupplier());
}

Expand Down Expand Up @@ -355,15 +354,15 @@ void builder_addLoggerProviderCustomizer() {
void builder_setResultAsGlobalFalse() {
GlobalOpenTelemetry.set(OpenTelemetry.noop());

OpenTelemetrySdk openTelemetry = builder.setResultAsGlobal(false).build().getOpenTelemetrySdk();
OpenTelemetrySdk openTelemetry = builder.build().getOpenTelemetrySdk();

assertThat(GlobalOpenTelemetry.get()).extracting("delegate").isNotSameAs(openTelemetry);
assertThat(GlobalEventEmitterProvider.get()).isNotSameAs(openTelemetry.getSdkLoggerProvider());
}

@Test
void builder_setResultAsGlobalTrue() {
OpenTelemetrySdk openTelemetry = builder.setResultAsGlobal(true).build().getOpenTelemetrySdk();
OpenTelemetrySdk openTelemetry = builder.setResultAsGlobal().build().getOpenTelemetrySdk();

assertThat(GlobalOpenTelemetry.get()).extracting("delegate").isSameAs(openTelemetry);
assertThat(GlobalEventEmitterProvider.get())
Expand All @@ -378,7 +377,7 @@ void builder_registersShutdownHook() {
Thread thread = new Thread();
doReturn(thread).when(builder).shutdownHook(any());

OpenTelemetrySdk sdk = builder.setResultAsGlobal(false).build().getOpenTelemetrySdk();
OpenTelemetrySdk sdk = builder.build().getOpenTelemetrySdk();

verify(builder, times(1)).shutdownHook(sdk);
assertThat(Runtime.getRuntime().removeShutdownHook(thread)).isTrue();
Expand Down Expand Up @@ -443,8 +442,7 @@ void tracerProviderCustomizer() {
(resource, config) -> resource.merge(Resource.builder().put("cow", "moo").build()))
.addPropertiesSupplier(() -> singletonMap("otel.metrics.exporter", "none"))
.addPropertiesSupplier(() -> singletonMap("otel.traces.exporter", "none"))
.addPropertiesSupplier(() -> singletonMap("otel.logs.exporter", "none"))
.setResultAsGlobal(false);
.addPropertiesSupplier(() -> singletonMap("otel.logs.exporter", "none"));

AutoConfiguredOpenTelemetrySdk autoConfigured = autoConfiguration.build();
assertThat(autoConfigured.getResource().getAttribute(stringKey("cow"))).isEqualTo("moo");
Expand Down Expand Up @@ -515,7 +513,6 @@ void configurationError_ClosesResources() {
.addPropertiesSupplier(() -> singletonMap("otel.traces.exporter", "none"))
.addPropertiesSupplier(() -> singletonMap("otel.logs.exporter", "none"))
.addPropertiesSupplier(() -> singletonMap("otel.propagators", "foo"))
.setResultAsGlobal(false)
.build())
.isInstanceOf(ConfigurationException.class)
.hasMessageContaining("Unrecognized value for otel.propagators");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ class OrderedSpiTest {

@Test
void shouldLoadSpiImplementationsInOrder() {
AutoConfiguredOpenTelemetrySdk sdk =
AutoConfiguredOpenTelemetrySdk.builder()
.setResultAsGlobal(false)
.disableShutdownHook()
.build();
AutoConfiguredOpenTelemetrySdk sdk = AutoConfiguredOpenTelemetrySdk.builder().build();

assertThat(sdk.getResource().getAttributes().asMap())
.contains(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ class ConditionalResourceProviderTest {

@Test
void shouldConditionallyProvideResourceAttributes_skipBasedOnPreviousResource() {
AutoConfiguredOpenTelemetrySdk sdk =
AutoConfiguredOpenTelemetrySdk.builder().setResultAsGlobal(false).build();
AutoConfiguredOpenTelemetrySdk sdk = AutoConfiguredOpenTelemetrySdk.builder().build();

assertThat(sdk.getResource().getAttributes().asMap())
.contains(entry(ResourceAttributes.SERVICE_NAME, "test-service"));
Expand All @@ -27,7 +26,6 @@ void shouldConditionallyProvideResourceAttributes_skipBasedOnPreviousResource()
void shouldConditionallyProvideResourceAttributes_skipBasedOnConfig() {
AutoConfiguredOpenTelemetrySdk sdk =
AutoConfiguredOpenTelemetrySdk.builder()
.setResultAsGlobal(false)
.addPropertiesSupplier(() -> singletonMap("skip-first-resource-provider", "true"))
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ void initializeAndGet() {
@Test
void initializeAndGet_noGlobal() {
try (OpenTelemetrySdk sdk =
AutoConfiguredOpenTelemetrySdk.builder()
.setResultAsGlobal(false)
.build()
.getOpenTelemetrySdk()) {
AutoConfiguredOpenTelemetrySdk.builder().build().getOpenTelemetrySdk()) {
assertThat(GlobalOpenTelemetry.get()).isNotSameAs(sdk);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ void autoConfiguredOpenTelemetrySdk_PrometheusOnClasspath() {

try (OpenTelemetrySdk sdk =
AutoConfiguredOpenTelemetrySdk.builder()
.setResultAsGlobal(false)
.setConfig(DefaultConfigProperties.createForTest(config))
.build()
.getOpenTelemetrySdk()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class ViewConfigCustomizerTest {
void customizeMeterProvider_Spi() {
InMemoryMetricReader reader = InMemoryMetricReader.create();
AutoConfiguredOpenTelemetrySdk.builder()
.setResultAsGlobal(false)
.addPropertiesSupplier(
() ->
ImmutableMap.of(
Expand Down

0 comments on commit 0e318fb

Please sign in to comment.