Skip to content

Commit

Permalink
Read normalized otel.config.file property (#6105)
Browse files Browse the repository at this point in the history
Co-authored-by: Trask Stalnaker <[email protected]>
  • Loading branch information
jack-berg and trask authored Jan 3, 2024
1 parent d45fb3f commit 17b8f43
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 =
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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(
Expand Down Expand Up @@ -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 =
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 17b8f43

Please sign in to comment.