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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,32 +55,90 @@ 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");
this.resource = Resource.empty();
cyrille-leclerc marked this conversation as resolved.
Show resolved Hide resolved
this.configProperties = DefaultConfigProperties.createFromMap(Collections.emptyMap());

AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk =
AutoConfiguredOpenTelemetrySdk.builder()
.setServiceClassLoader(getClass().getClassLoader())
.addPropertiesSupplier(() -> properties)
.addPropertiesCustomizer(
OpenTelemetrySdkService::requireExplicitConfigOfTheOtlpExporter)
cyrille-leclerc marked this conversation as resolved.
Show resolved Hide resolved
.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();

this.openTelemetrySdk = autoConfiguredOpenTelemetrySdk.getOpenTelemetrySdk();

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

// TODO should we replace `getBooleanConfig(name)` by `configProperties.getBoolean(name)`?
Copy link
Contributor

Choose a reason for hiding this comment

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

[minor] can we answer this TODO directly into this PR or are there still unknowns there to be answered before being able to do this change ?

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

this.tracer = openTelemetrySdk.getTracer("io.opentelemetry.contrib.maven", VERSION);
}

/**
* The OTel SDK by default sends data to the OTLP gRPC endpoint localhost:4317 if no exporter and
* no OTLP exporter endpoint are defined. This is not suited for a build tool for which we want
* the OTel SDK to be disabled by default.
*
* <p>Change the OTel SDL behavior: if none of the exporter and the OTLP exporter endpoint are
* defined, explicitly disable the exporter setting "{@code
* otel.[traces,metrics,logs].exporter=none}"
*
* @return The properties to be returned by {@link
* io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder#addPropertiesCustomizer(java.util.function.Function)}
*/
static Map<String, String> requireExplicitConfigOfTheOtlpExporter(
ConfigProperties configProperties) {

Map<String, String> properties = new HashMap<>();
if (configProperties.getString("otel.exporter.otlp.endpoint") == null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

[minor style] given most of the code is within this if statement, then inverting the condition and doing an early return with the debug statement avoids a bit of nesting by doing the "quick path" first.

for (SignalType signalType : SignalType.values()) {
boolean isExporterImplicitlyConfiguredToOtlp =
configProperties.getString("otel." + signalType.value + ".exporter") == null;
boolean isOtlpExporterEndpointSpecified =
configProperties.getString("otel.exporter.otlp." + signalType.value + ".endpoint")
!= null;

if (isExporterImplicitlyConfiguredToOtlp && !isOtlpExporterEndpointSpecified) {
logger.debug(
"OpenTelemetry: Disabling default OTLP exporter endpoint for signal {} exporter",
signalType.value);
properties.put("otel." + signalType.value + ".exporter", "none");
}
}
} else {
logger.debug("OpenTelemetry: OTLP exporter endpoint is explicitly configured");
}
return properties;
}

enum SignalType {
Copy link
Contributor

Choose a reason for hiding this comment

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

[minor] could be made private, also this might be further simplified to an Arrays.asList("traces","metrics","logs").

TRACES("traces"),
METRICS("metrics"),
LOGS("logs");

private final String value;

SignalType(String value) {
this.value = value;
}
}

@PreDestroy
@Override
public synchronized void close() {
Expand All @@ -97,6 +163,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,121 @@

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;

/**
* Note: if otel-java-contrib bumps to Java 11+, we could use junit-pioneer's
* {@code @SetSystemProperty} and {@code @ClearSystemProperty} but no bump is planned for now.
*/
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");
cyrille-leclerc marked this conversation as resolved.
Show resolved Hide resolved
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");
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be a bit better to not rely on global state (here in system properties) as we have to take care of the cleanup here.

What I think could be slightly better is to add an argument to the OpenTelemetrySdkService constructor to control which system properties are present or not if we can. While this slightly alters the design for test purpose it could be worth it here as it allows to get rid of any early cleanup or finally block. Also with such a change there can't be any side effect if system properties are set on the JVM that execute the tests which usually require us to ensure properties are not set in some cases.

The AutoConfiguredOpenTelemetrySdkBuilder.setConfigPropertiesCustomizer allows to override the config properties, but it's package-private so using AutoConfigureUtil.setConfigPropertiesCustomizer is needed. While it's an internal API doing this only for tests should be fine.


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 defining `otel.exporter.otlp.endpoint` works */
@Test
public void testOverwrittenExporterConfiguration_1() {
System.setProperty("otel.exporter.otlp.endpoint", "https://example.com:4317");

try (OpenTelemetrySdkService openTelemetrySdkService = new OpenTelemetrySdkService()) {

ConfigProperties configProperties = openTelemetrySdkService.getConfigProperties();
assertThat(configProperties.getString("otel.exporter.otlp.endpoint"))
.isEqualTo("https://example.com:4317");
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.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 defining `otel.exporter.otlp.traces.endpoint` works */
@Test
public void testOverwrittenExporterConfiguration_2() {
System.clearProperty("otel.exporter.otlp.endpoint");
System.clearProperty("otel.traces.exporter");
System.setProperty("otel.exporter.otlp.traces.endpoint", "https://example.com:4317/");

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("https://example.com:4317/");
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.traces.exporter");
System.clearProperty("otel.exporter.otlp.traces.endpoint");
}
}

/** Verify defining `otel.exporter.otlp.traces.endpoint` and `otel.traces.exporter` works */
@Test
public void testOverwrittenExporterConfiguration_3() {
System.clearProperty("otel.exporter.otlp.endpoint");
System.setProperty("otel.traces.exporter", "otlp");
System.setProperty("otel.exporter.otlp.traces.endpoint", "https://example.com:4317/");

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("https://example.com:4317/");
assertThat(configProperties.getString("otel.traces.exporter")).isEqualTo("otlp");
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");
}
}
}
Loading