Skip to content

Commit

Permalink
Load OTel SDK config from environment variables and system properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrille-leclerc committed Aug 27, 2024
1 parent ee61c7b commit ecb469b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import javax.annotation.PreDestroy;
Expand Down Expand Up @@ -59,16 +60,26 @@ public OpenTelemetrySdkService() {
AutoConfiguredOpenTelemetrySdk.builder()
.setServiceClassLoader(getClass().getClassLoader())
.addPropertiesCustomizer(configProperties -> {
// Change default of "otel.[traces,metrics,logs].exporter" from "otlp" to "none"
// The OTel SDK by default sends data to the OTLP gRPC endpoint at localhost:4317.
// Change this behavior to disable by default the OTel SDK in the Maven extension so
// that it must be explicitly enabled by the user.
// To change this default behavior, we set "otel.[traces,metrics,logs].exporter" to
// "none" if the endpoint has not been specified
if (configProperties.getString("otel.exporter.otlp.endpoint") == null) {
Map<String, String> properties = new HashMap<>();
if (configProperties.getString("otel.exporter.otlp.traces.endpoint") == null) {
if (Objects.equals("otlp",
configProperties.getString("otel.traces.exporter", "otlp"))
&& configProperties.getString("otel.exporter.otlp.traces.endpoint") == null) {
properties.put("otel.traces.exporter", "none");
}
if (configProperties.getString("otel.exporter.otlp.metrics.endpoint") == null) {
if (Objects.equals("otlp",
configProperties.getString("otel.metrics.exporter", "otlp"))
&& configProperties.getString("otel.exporter.otlp.metrics.endpoint") == null) {
properties.put("otel.metrics.exporter", "none");
}
if (configProperties.getString("otel.exporter.otlp.logs.endpoint") == null) {
if (Objects.equals("otlp",
configProperties.getString("otel.logs.exporter", "otlp"))
&& configProperties.getString("otel.exporter.otlp.logs.endpoint") == null) {
properties.put("otel.logs.exporter", "none");
}
return properties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,29 @@ public void testOverwrittenResourceAttributes() {
System.setProperty("otel.service.name", "my-maven");
System.setProperty("otel.resource.attributes", "key1=val1,key2=val2");

try (OpenTelemetrySdkService openTelemetrySdkService = new OpenTelemetrySdkService()) {
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");
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");
System.clearProperty("otel.service.name");
System.clearProperty("otel.resource.attributes");
}
}

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

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.exporter.otlp.endpoint")).isEqualTo(
"http://example.com:4317");
assertThat(configProperties.getString("otel.traces.exporter")).isNull();
assertThat(configProperties.getString("otel.metrics.exporter")).isNull();
assertThat(configProperties.getString("otel.logs.exporter")).isNull();
Expand All @@ -71,18 +71,44 @@ public void testOverwrittenExporterConfiguration_1() {
}
}

/** Verify overwritten `"otel.exporter.otlp.traces.endpoint" */
/** Verify defining `otel.exporter.otlp.traces.endpoint` works */
@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");
System.clearProperty("otel.traces.exporter");
System.setProperty("otel.exporter.otlp.traces.endpoint", "http://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(
"http://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", "http://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("http://example.com:4318/v1/traces");
assertThat(
configProperties.getString("otel.exporter.otlp.traces.endpoint")).isEqualTo(
"http://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");
Expand Down

0 comments on commit ecb469b

Please sign in to comment.