From 0e318fb84c07ce6841192b0dcf195aac6b0a2707 Mon Sep 17 00:00:00 2001 From: jack-berg <34418638+jack-berg@users.noreply.github.com> Date: Thu, 22 Jun 2023 16:14:24 -0500 Subject: [PATCH] AutoConfiguredOpenTelemetrySdkBuilder does not set GlobalOpenTelemetry by default (#5564) --- .../autoconfigure/AutoConfiguredOpenTelemetrySdk.java | 2 +- .../AutoConfiguredOpenTelemetrySdkBuilder.java | 8 +++++--- .../AutoConfiguredOpenTelemetrySdkTest.java | 11 ++++------- .../sdk/autoconfigure/OrderedSpiTest.java | 6 +----- .../ConditionalResourceProviderTest.java | 4 +--- .../AutoConfiguredOpenTelemetrySdkTest.java | 5 +---- .../MetricExporterConfigurationTest.java | 1 - .../metric/viewconfig/ViewConfigCustomizerTest.java | 1 - 8 files changed, 13 insertions(+), 25 deletions(-) diff --git a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdk.java b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdk.java index a1a8c58fe9b..424368ccf8d 100644 --- a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdk.java +++ b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdk.java @@ -28,7 +28,7 @@ public abstract class AutoConfiguredOpenTelemetrySdk { *
This will automatically set the resulting SDK as the {@link GlobalOpenTelemetry} instance. */ public static AutoConfiguredOpenTelemetrySdk initialize() { - return builder().build(); + return builder().setResultAsGlobal().build(); } /** diff --git a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkBuilder.java b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkBuilder.java index 6770efdc38e..e67cae366b5 100644 --- a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkBuilder.java +++ b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkBuilder.java @@ -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; @@ -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. + * + *
By default, {@link GlobalOpenTelemetry} is not set. */ - public AutoConfiguredOpenTelemetrySdkBuilder setResultAsGlobal(boolean setResultAsGlobal) { - this.setResultAsGlobal = setResultAsGlobal; + public AutoConfiguredOpenTelemetrySdkBuilder setResultAsGlobal() { + this.setResultAsGlobal = true; return this; } diff --git a/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkTest.java b/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkTest.java index 9e4c6c39305..56c02d3822d 100644 --- a/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkTest.java +++ b/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkTest.java @@ -147,7 +147,6 @@ void resetGlobal() { GlobalEventEmitterProvider.resetForTest(); builder = AutoConfiguredOpenTelemetrySdk.builder() - .setResultAsGlobal(false) .addPropertiesSupplier(disableExportPropertySupplier()); } @@ -355,7 +354,7 @@ 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()); @@ -363,7 +362,7 @@ void builder_setResultAsGlobalFalse() { @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()) @@ -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(); @@ -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"); @@ -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"); diff --git a/sdk-extensions/autoconfigure/src/testAutoConfigureOrder/java/io/opentelemetry/sdk/autoconfigure/OrderedSpiTest.java b/sdk-extensions/autoconfigure/src/testAutoConfigureOrder/java/io/opentelemetry/sdk/autoconfigure/OrderedSpiTest.java index a110e1c96fc..5d55c2fc679 100644 --- a/sdk-extensions/autoconfigure/src/testAutoConfigureOrder/java/io/opentelemetry/sdk/autoconfigure/OrderedSpiTest.java +++ b/sdk-extensions/autoconfigure/src/testAutoConfigureOrder/java/io/opentelemetry/sdk/autoconfigure/OrderedSpiTest.java @@ -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( diff --git a/sdk-extensions/autoconfigure/src/testConditionalResourceProvider/java/io/opentelemetry/sdk/autoconfigure/ConditionalResourceProviderTest.java b/sdk-extensions/autoconfigure/src/testConditionalResourceProvider/java/io/opentelemetry/sdk/autoconfigure/ConditionalResourceProviderTest.java index dd2a2cd47ef..07ee4654d4f 100644 --- a/sdk-extensions/autoconfigure/src/testConditionalResourceProvider/java/io/opentelemetry/sdk/autoconfigure/ConditionalResourceProviderTest.java +++ b/sdk-extensions/autoconfigure/src/testConditionalResourceProvider/java/io/opentelemetry/sdk/autoconfigure/ConditionalResourceProviderTest.java @@ -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")); @@ -27,7 +26,6 @@ void shouldConditionallyProvideResourceAttributes_skipBasedOnPreviousResource() void shouldConditionallyProvideResourceAttributes_skipBasedOnConfig() { AutoConfiguredOpenTelemetrySdk sdk = AutoConfiguredOpenTelemetrySdk.builder() - .setResultAsGlobal(false) .addPropertiesSupplier(() -> singletonMap("skip-first-resource-provider", "true")) .build(); diff --git a/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkTest.java b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkTest.java index c501d77991f..6bf5c74ccc5 100644 --- a/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkTest.java +++ b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkTest.java @@ -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); } } diff --git a/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/MetricExporterConfigurationTest.java b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/MetricExporterConfigurationTest.java index 5f6a7721459..2383c38b1f8 100644 --- a/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/MetricExporterConfigurationTest.java +++ b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/MetricExporterConfigurationTest.java @@ -72,7 +72,6 @@ void autoConfiguredOpenTelemetrySdk_PrometheusOnClasspath() { try (OpenTelemetrySdk sdk = AutoConfiguredOpenTelemetrySdk.builder() - .setResultAsGlobal(false) .setConfig(DefaultConfigProperties.createForTest(config)) .build() .getOpenTelemetrySdk()) { diff --git a/sdk-extensions/incubator/src/test/java/io/opentelemetry/sdk/extension/incubator/metric/viewconfig/ViewConfigCustomizerTest.java b/sdk-extensions/incubator/src/test/java/io/opentelemetry/sdk/extension/incubator/metric/viewconfig/ViewConfigCustomizerTest.java index f9344f7327a..25950b2b898 100644 --- a/sdk-extensions/incubator/src/test/java/io/opentelemetry/sdk/extension/incubator/metric/viewconfig/ViewConfigCustomizerTest.java +++ b/sdk-extensions/incubator/src/test/java/io/opentelemetry/sdk/extension/incubator/metric/viewconfig/ViewConfigCustomizerTest.java @@ -28,7 +28,6 @@ class ViewConfigCustomizerTest { void customizeMeterProvider_Spi() { InMemoryMetricReader reader = InMemoryMetricReader.create(); AutoConfiguredOpenTelemetrySdk.builder() - .setResultAsGlobal(false) .addPropertiesSupplier( () -> ImmutableMap.of(