Skip to content

Commit

Permalink
Load OTelSDK config from environment variables and system properties. F…
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrille-leclerc committed Aug 26, 2024
1 parent 682ccd2 commit ee61c7b
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
import io.opentelemetry.maven.semconv.MavenOtelSemanticAttributes;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.resources.Resource;
import java.io.Closeable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
Expand All @@ -36,6 +40,10 @@ public final class OpenTelemetrySdkService implements Closeable {

private final OpenTelemetrySdk openTelemetrySdk;

private Resource resource;

private ConfigProperties configProperties;

private final Tracer tracer;

private final boolean mojosInstrumentationEnabled;
Expand All @@ -47,26 +55,51 @@ public OpenTelemetrySdkService() {
"OpenTelemetry: Initialize OpenTelemetrySdkService v{}...",
MavenOtelSemanticAttributes.TELEMETRY_DISTRO_VERSION_VALUE);

// Change default of "otel.[traces,metrics,logs].exporter" from "otlp" to "none"
// The impacts are
// * If no otel exporter settings are passed, then the Maven extension will not export
// rather than exporting on OTLP GRPC to http://localhost:4317
// * If OTEL_EXPORTER_OTLP_ENDPOINT is defined but OTEL_[TRACES,METRICS,LOGS]_EXPORTER,
// is not, then don't export
Map<String, String> properties = new HashMap<>();
properties.put("otel.traces.exporter", "none");
properties.put("otel.metrics.exporter", "none");
properties.put("otel.logs.exporter", "none");

AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk =
AutoConfiguredOpenTelemetrySdk.builder()
.setServiceClassLoader(getClass().getClassLoader())
.addPropertiesSupplier(() -> properties)
.addPropertiesCustomizer(configProperties -> {
// Change default of "otel.[traces,metrics,logs].exporter" from "otlp" to "none"
if (configProperties.getString("otel.exporter.otlp.endpoint") == null) {
Map<String, String> properties = new HashMap<>();
if (configProperties.getString("otel.exporter.otlp.traces.endpoint") == null) {
properties.put("otel.traces.exporter", "none");
}
if (configProperties.getString("otel.exporter.otlp.metrics.endpoint") == null) {
properties.put("otel.metrics.exporter", "none");
}
if (configProperties.getString("otel.exporter.otlp.logs.endpoint") == null) {
properties.put("otel.logs.exporter", "none");
}
return properties;
} else {
return Collections.emptyMap();
}
})
.addPropertiesCustomizer(config -> {
// keep a reference to the computed config properties for future use in the extension
this.configProperties = config;
return Collections.emptyMap();
})
.addResourceCustomizer((res, configProperties) -> {
// keep a reference to the computed Resource for future use in the extension
this.resource = Resource.builder().putAll(res).build();
return this.resource;
})
.disableShutdownHook()
.build();

if (this.resource == null) {
this.resource = Resource.empty();
}
if (this.configProperties == null) {
this.configProperties = DefaultConfigProperties.createFromMap(Collections.emptyMap());
}

this.openTelemetrySdk = autoConfiguredOpenTelemetrySdk.getOpenTelemetrySdk();

logger.debug("OpenTelemetry: OpenTelemetrySdkService initialized, resource:{}", resource);

Boolean mojoSpansEnabled = getBooleanConfig("otel.instrumentation.maven.mojo.enabled");
this.mojosInstrumentationEnabled = mojoSpansEnabled == null || mojoSpansEnabled;

Expand Down Expand Up @@ -97,6 +130,14 @@ public Tracer getTracer() {
return this.tracer;
}

public Resource getResource() {
return resource;
}

public ConfigProperties getConfigProperties() {
return configProperties;
}

/** Returns the {@link ContextPropagators} for this {@link OpenTelemetry}. */
public ContextPropagators getPropagators() {
return this.openTelemetrySdk.getPropagators();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,92 @@

package io.opentelemetry.maven;

import org.junit.jupiter.api.Disabled;
import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static org.assertj.core.api.Assertions.assertThat;

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

public class OpenTelemetrySdkServiceTest {

/** Verify default `service.name` */
/** Verify default config */
@Test
@Disabled
public void testDefaultConfiguration() {
testConfiguration("maven");
System.clearProperty("otel.exporter.otlp.endpoint");
System.clearProperty("otel.service.name");
System.clearProperty("otel.resource.attributes");
try (OpenTelemetrySdkService openTelemetrySdkService = new OpenTelemetrySdkService()) {

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

ConfigProperties configProperties = openTelemetrySdkService.getConfigProperties();
assertThat(configProperties.getString("otel.exporter.otlp.endpoint")).isNull();
assertThat(configProperties.getString("otel.traces.exporter")).isEqualTo("none");
assertThat(configProperties.getString("otel.metrics.exporter")).isEqualTo("none");
assertThat(configProperties.getString("otel.logs.exporter")).isEqualTo("none");
}
}

/** Verify overwritten `service.name` */
/** Verify overwritten `service.name`,`key1` and `key2` */
@Test
@Disabled
public void testOverwrittenConfiguration() {
public void testOverwrittenResourceAttributes() {
System.setProperty("otel.service.name", "my-maven");
try {
testConfiguration("my-maven");
System.setProperty("otel.resource.attributes", "key1=val1,key2=val2");

try (OpenTelemetrySdkService openTelemetrySdkService = new OpenTelemetrySdkService()) {

Resource resource = openTelemetrySdkService.getResource();
assertThat(resource.getAttribute(stringKey("service.name"))).isEqualTo("my-maven");
assertThat(resource.getAttribute(stringKey("key1"))).isEqualTo("val1");
assertThat(resource.getAttribute(stringKey("key2"))).isEqualTo("val2");

} finally {
System.clearProperty("otel.service.name");
System.clearProperty("otel.resource.attributes");
}
}

/** Verify overwritten `"otel.exporter.otlp.endpoint" */
@Test
public void testOverwrittenExporterConfiguration_1() {
System.setProperty("otel.exporter.otlp.endpoint", "http://example.com:4318");

try (OpenTelemetrySdkService openTelemetrySdkService = new OpenTelemetrySdkService()) {


ConfigProperties configProperties = openTelemetrySdkService.getConfigProperties();
assertThat(configProperties.getString("otel.exporter.otlp.endpoint")).isEqualTo("http://example.com:4318");
assertThat(configProperties.getString("otel.traces.exporter")).isNull();
assertThat(configProperties.getString("otel.metrics.exporter")).isNull();
assertThat(configProperties.getString("otel.logs.exporter")).isNull();

} finally {
System.clearProperty("otel.service.name");
System.clearProperty("otel.exporter.otlp.endpoint");
}
}

void testConfiguration(String expectedServiceName) {
// OpenTelemetrySdkService openTelemetrySdkService = new OpenTelemetrySdkService();
// openTelemetrySdkService.initialize();
// try {
// Resource resource =
// openTelemetrySdkService.autoConfiguredOpenTelemetrySdk.getResource();
// assertThat(resource.getAttribute(ResourceAttributes.SERVICE_NAME))
// .isEqualTo(expectedServiceName);
// } finally {
// openTelemetrySdkService.dispose();
// GlobalOpenTelemetry.resetForTest();
// GlobalEventEmitterProvider.resetForTest();
// }
/** Verify overwritten `"otel.exporter.otlp.traces.endpoint" */
@Test
public void testOverwrittenExporterConfiguration_2() {
System.clearProperty("otel.exporter.otlp.endpoint");
System.setProperty("otel.exporter.otlp.traces.endpoint", "http://example.com:4318/v1/traces");
System.setProperty("otel.exporter.otlp.traces.protocol", "http/protobuf");

try (OpenTelemetrySdkService openTelemetrySdkService = new OpenTelemetrySdkService()) {

ConfigProperties configProperties = openTelemetrySdkService.getConfigProperties();
assertThat(configProperties.getString("otel.exporter.otlp.endpoint")).isNull();
assertThat(configProperties.getString("otel.exporter.otlp.traces.endpoint")).isEqualTo("http://example.com:4318/v1/traces");
assertThat(configProperties.getString("otel.traces.exporter")).isNull();
assertThat(configProperties.getString("otel.metrics.exporter")).isEqualTo("none");
assertThat(configProperties.getString("otel.logs.exporter")).isEqualTo("none");

} finally {
System.clearProperty("otel.exporter.otlp.endpoint");
System.clearProperty("otel.exporter.otlp.traces.endpoint");
System.clearProperty("otel.exporter.otlp.traces.protocol");
}
}
}

0 comments on commit ee61c7b

Please sign in to comment.