Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load OTel SDK config from environment variables and system properties.… #1434

Prev Previous commit
Better tests and docs
cyrille-leclerc committed Dec 6, 2024
commit e5cb5827b004035d40e7a22cb0023e70392c26c3
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@

package io.opentelemetry.maven;

import com.google.common.annotations.VisibleForTesting;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.propagation.ContextPropagators;
@@ -40,7 +41,7 @@ public final class OpenTelemetrySdkService implements Closeable {

private final OpenTelemetrySdk openTelemetrySdk;

private final Resource resource;
@VisibleForTesting final Resource resource;

private final ConfigProperties configProperties;

@@ -67,10 +68,10 @@ public OpenTelemetrySdkService() {
this.configProperties =
Optional.ofNullable(AutoConfigureUtil.getConfig(autoConfiguredOpenTelemetrySdk))
.orElseGet(() -> DefaultConfigProperties.createFromMap(Collections.emptyMap()));
this.resource =
Optional.ofNullable(AutoConfigureUtil2.getResource(autoConfiguredOpenTelemetrySdk))
.orElseGet(Resource::getDefault);

this.resource = AutoConfigureUtil2.getResource(autoConfiguredOpenTelemetrySdk);
// Display resource attributes in debug logs for troubleshooting when traces are not found in
// the observability backend, helping understand `service.name`, `service.namespace`, etc.
logger.debug("OpenTelemetry: OpenTelemetrySdkService initialized, resource:{}", resource);

this.mojosInstrumentationEnabled =
@@ -141,10 +142,6 @@ public Tracer getTracer() {
return this.tracer;
}

public Resource getResource() {
return resource;
}

public ConfigProperties getConfigProperties() {
return configProperties;
}
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@

import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.resources.Resource;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;

/**
@@ -26,7 +27,7 @@ public void testDefaultConfiguration() {
System.clearProperty("otel.resource.attributes");
try (OpenTelemetrySdkService openTelemetrySdkService = new OpenTelemetrySdkService()) {

Resource resource = openTelemetrySdkService.getResource();
Resource resource = openTelemetrySdkService.resource;
assertThat(resource.getAttribute(stringKey("service.name"))).isEqualTo("maven");

ConfigProperties configProperties = openTelemetrySdkService.getConfigProperties();
@@ -45,7 +46,7 @@ public void testOverwrittenResourceAttributes() {

try (OpenTelemetrySdkService openTelemetrySdkService = new OpenTelemetrySdkService()) {

Resource resource = openTelemetrySdkService.getResource();
Resource resource = openTelemetrySdkService.resource;
assertThat(resource.getAttribute(stringKey("service.name"))).isEqualTo("my-maven");
assertThat(resource.getAttribute(stringKey("key1"))).isEqualTo("val1");
assertThat(resource.getAttribute(stringKey("key2"))).isEqualTo("val2");
@@ -122,4 +123,14 @@ public void testOverwrittenExporterConfiguration_3() {
System.clearProperty("otel.exporter.otlp.traces.protocol");
}
}

@AfterAll
static void afterAll() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SylvainJuge please review

System.clearProperty("otel.exporter.otlp.endpoint");
System.clearProperty("otel.exporter.otlp.traces.endpoint");
System.clearProperty("otel.exporter.otlp.traces.protocol");
System.clearProperty("otel.resource.attributes");
System.clearProperty("otel.service.name");
System.clearProperty("otel.traces.exporter");
}
}