diff --git a/maven-extension/src/main/java/io/opentelemetry/maven/OpenTelemetrySdkService.java b/maven-extension/src/main/java/io/opentelemetry/maven/OpenTelemetrySdkService.java index 2d0aa1014..81d088a6c 100644 --- a/maven-extension/src/main/java/io/opentelemetry/maven/OpenTelemetrySdkService.java +++ b/maven-extension/src/main/java/io/opentelemetry/maven/OpenTelemetrySdkService.java @@ -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; @@ -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 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; diff --git a/maven-extension/src/test/java/io/opentelemetry/maven/OpenTelemetrySdkServiceTest.java b/maven-extension/src/test/java/io/opentelemetry/maven/OpenTelemetrySdkServiceTest.java index 9a9700813..1d3fb9a1f 100644 --- a/maven-extension/src/test/java/io/opentelemetry/maven/OpenTelemetrySdkServiceTest.java +++ b/maven-extension/src/test/java/io/opentelemetry/maven/OpenTelemetrySdkServiceTest.java @@ -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(); @@ -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");