From 17b8f4355da0932e25a66fffd03f01ad8ea95f5e Mon Sep 17 00:00:00 2001 From: jack-berg <34418638+jack-berg@users.noreply.github.com> Date: Wed, 3 Jan 2024 11:50:38 -0600 Subject: [PATCH] Read normalized otel.config.file property (#6105) Co-authored-by: Trask Stalnaker --- .../AutoConfiguredOpenTelemetrySdkBuilder.java | 3 ++- .../autoconfigure/FileConfigurationTest.java | 11 ++++++++++- .../autoconfigure/FileConfigurationTest.java | 18 +++++++++++++----- 3 files changed, 25 insertions(+), 7 deletions(-) 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 2d10524e276..05935556aba 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 @@ -479,10 +479,11 @@ public AutoConfiguredOpenTelemetrySdk build() { @Nullable private static AutoConfiguredOpenTelemetrySdk maybeConfigureFromFile(ConfigProperties config) { - String configurationFile = config.getString("OTEL_CONFIG_FILE"); + String configurationFile = config.getString("otel.config.file"); if (configurationFile == null || configurationFile.isEmpty()) { return null; } + logger.fine("Autoconfiguring from configuration file: " + configurationFile); FileInputStream fis; try { fis = new FileInputStream(configurationFile); diff --git a/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/FileConfigurationTest.java b/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/FileConfigurationTest.java index e6aa1759d32..418dd263625 100644 --- a/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/FileConfigurationTest.java +++ b/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/FileConfigurationTest.java @@ -7,6 +7,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; +import io.github.netmikey.logunit.api.LogCapturer; import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException; import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties; @@ -16,10 +17,17 @@ import java.nio.file.Path; import java.util.Collections; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.api.io.TempDir; +import org.slf4j.event.Level; class FileConfigurationTest { + @RegisterExtension + static final LogCapturer logCapturer = + LogCapturer.create() + .captureForLogger(AutoConfiguredOpenTelemetrySdkBuilder.class.getName(), Level.TRACE); + @Test void configFile(@TempDir Path tempDir) throws IOException { String yaml = @@ -36,11 +44,12 @@ void configFile(@TempDir Path tempDir) throws IOException { Files.write(path, yaml.getBytes(StandardCharsets.UTF_8)); ConfigProperties config = DefaultConfigProperties.createFromMap( - Collections.singletonMap("OTEL_CONFIG_FILE", path.toString())); + Collections.singletonMap("otel.config.file", path.toString())); assertThatThrownBy(() -> AutoConfiguredOpenTelemetrySdk.builder().setConfig(config).build()) .isInstanceOf(ConfigurationException.class) .hasMessage( "Error configuring from file. Is opentelemetry-sdk-extension-incubator on the classpath?"); + logCapturer.assertContains("Autoconfiguring from configuration file: " + path); } } diff --git a/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/FileConfigurationTest.java b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/FileConfigurationTest.java index dd11494958f..ea726ca3472 100644 --- a/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/FileConfigurationTest.java +++ b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/FileConfigurationTest.java @@ -15,6 +15,7 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import io.github.netmikey.logunit.api.LogCapturer; import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator; @@ -41,11 +42,17 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.api.io.TempDir; +import org.slf4j.event.Level; class FileConfigurationTest { @RegisterExtension private static final CleanupExtension cleanup = new CleanupExtension(); + @RegisterExtension + static final LogCapturer logCapturer = + LogCapturer.create() + .captureForLogger(AutoConfiguredOpenTelemetrySdkBuilder.class.getName(), Level.TRACE); + @TempDir private Path tempDir; private Path configFilePath; @@ -71,7 +78,7 @@ void setup() throws IOException { void configFile_Valid() { ConfigProperties config = DefaultConfigProperties.createFromMap( - Collections.singletonMap("OTEL_CONFIG_FILE", configFilePath.toString())); + Collections.singletonMap("otel.config.file", configFilePath.toString())); OpenTelemetrySdk expectedSdk = OpenTelemetrySdk.builder() .setTracerProvider( @@ -102,13 +109,14 @@ void configFile_Valid() { assertThat(autoConfiguredOpenTelemetrySdk.getResource()).isEqualTo(Resource.getDefault()); verify(builder, times(1)).shutdownHook(autoConfiguredOpenTelemetrySdk.getOpenTelemetrySdk()); assertThat(Runtime.getRuntime().removeShutdownHook(thread)).isTrue(); + logCapturer.assertContains("Autoconfiguring from configuration file: " + configFilePath); } @Test void configFile_NoShutdownHook() { ConfigProperties config = DefaultConfigProperties.createFromMap( - Collections.singletonMap("OTEL_CONFIG_FILE", configFilePath.toString())); + Collections.singletonMap("otel.config.file", configFilePath.toString())); AutoConfiguredOpenTelemetrySdkBuilder builder = spy(AutoConfiguredOpenTelemetrySdk.builder()); AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk = @@ -123,7 +131,7 @@ void configFile_setResultAsGlobalFalse() { GlobalOpenTelemetry.set(OpenTelemetry.noop()); ConfigProperties config = DefaultConfigProperties.createFromMap( - Collections.singletonMap("OTEL_CONFIG_FILE", configFilePath.toString())); + Collections.singletonMap("otel.config.file", configFilePath.toString())); AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk = AutoConfiguredOpenTelemetrySdk.builder().setConfig(config).build(); @@ -139,7 +147,7 @@ void configFile_setResultAsGlobalFalse() { void configFile_setResultAsGlobalTrue() { ConfigProperties config = DefaultConfigProperties.createFromMap( - Collections.singletonMap("OTEL_CONFIG_FILE", configFilePath.toString())); + Collections.singletonMap("otel.config.file", configFilePath.toString())); AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk = AutoConfiguredOpenTelemetrySdk.builder().setConfig(config).setResultAsGlobal().build(); @@ -169,7 +177,7 @@ void configFile_Error(@TempDir Path tempDir) throws IOException { Files.write(path, yaml.getBytes(StandardCharsets.UTF_8)); ConfigProperties config = DefaultConfigProperties.createFromMap( - Collections.singletonMap("OTEL_CONFIG_FILE", path.toString())); + Collections.singletonMap("otel.config.file", path.toString())); assertThatThrownBy(() -> AutoConfiguredOpenTelemetrySdk.builder().setConfig(config).build()) .isInstanceOf(ConfigurationException.class)