55
66package io .opentelemetry .maven ;
77
8+ import com .google .common .annotations .VisibleForTesting ;
89import io .opentelemetry .api .OpenTelemetry ;
910import io .opentelemetry .api .trace .Tracer ;
1011import io .opentelemetry .context .propagation .ContextPropagators ;
1112import io .opentelemetry .maven .semconv .MavenOtelSemanticAttributes ;
1213import io .opentelemetry .sdk .OpenTelemetrySdk ;
1314import io .opentelemetry .sdk .autoconfigure .AutoConfiguredOpenTelemetrySdk ;
15+ import io .opentelemetry .sdk .autoconfigure .internal .AutoConfigureUtil ;
16+ import io .opentelemetry .sdk .autoconfigure .spi .ConfigProperties ;
17+ import io .opentelemetry .sdk .autoconfigure .spi .internal .DefaultConfigProperties ;
1418import io .opentelemetry .sdk .common .CompletableResultCode ;
19+ import io .opentelemetry .sdk .resources .Resource ;
1520import java .io .Closeable ;
21+ import java .util .Collections ;
1622import java .util .HashMap ;
17- import java .util .Locale ;
1823import java .util .Map ;
24+ import java .util .Optional ;
1925import java .util .concurrent .TimeUnit ;
20- import javax .annotation .Nullable ;
2126import javax .annotation .PreDestroy ;
2227import javax .inject .Named ;
2328import javax .inject .Singleton ;
@@ -36,6 +41,10 @@ public final class OpenTelemetrySdkService implements Closeable {
3641
3742 private final OpenTelemetrySdk openTelemetrySdk ;
3843
44+ @ VisibleForTesting final Resource resource ;
45+
46+ private final ConfigProperties configProperties ;
47+
3948 private final Tracer tracer ;
4049
4150 private final boolean mojosInstrumentationEnabled ;
@@ -47,32 +56,68 @@ public OpenTelemetrySdkService() {
4756 "OpenTelemetry: Initialize OpenTelemetrySdkService v{}..." ,
4857 MavenOtelSemanticAttributes .TELEMETRY_DISTRO_VERSION_VALUE );
4958
50- // Change default of "otel.[traces,metrics,logs].exporter" from "otlp" to "none"
51- // The impacts are
52- // * If no otel exporter settings are passed, then the Maven extension will not export
53- // rather than exporting on OTLP GRPC to http://localhost:4317
54- // * If OTEL_EXPORTER_OTLP_ENDPOINT is defined but OTEL_[TRACES,METRICS,LOGS]_EXPORTER,
55- // is not, then don't export
56- Map <String , String > properties = new HashMap <>();
57- properties .put ("otel.traces.exporter" , "none" );
58- properties .put ("otel.metrics.exporter" , "none" );
59- properties .put ("otel.logs.exporter" , "none" );
60-
6159 AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk =
6260 AutoConfiguredOpenTelemetrySdk .builder ()
6361 .setServiceClassLoader (getClass ().getClassLoader ())
64- .addPropertiesSupplier (() -> properties )
62+ .addPropertiesCustomizer (
63+ OpenTelemetrySdkService ::requireExplicitConfigOfTheOtlpExporter )
6564 .disableShutdownHook ()
6665 .build ();
6766
6867 this .openTelemetrySdk = autoConfiguredOpenTelemetrySdk .getOpenTelemetrySdk ();
68+ this .configProperties =
69+ Optional .ofNullable (AutoConfigureUtil .getConfig (autoConfiguredOpenTelemetrySdk ))
70+ .orElseGet (() -> DefaultConfigProperties .createFromMap (Collections .emptyMap ()));
71+
72+ this .resource = AutoConfigureUtil2 .getResource (autoConfiguredOpenTelemetrySdk );
73+ // Display resource attributes in debug logs for troubleshooting when traces are not found in
74+ // the observability backend, helping understand `service.name`, `service.namespace`, etc.
75+ logger .debug ("OpenTelemetry: OpenTelemetrySdkService initialized, resource:{}" , resource );
6976
70- Boolean mojoSpansEnabled = getBooleanConfig ( "otel.instrumentation.maven.mojo.enabled" );
71- this . mojosInstrumentationEnabled = mojoSpansEnabled == null || mojoSpansEnabled ;
77+ this . mojosInstrumentationEnabled =
78+ configProperties . getBoolean ( "otel.instrumentation.maven.mojo.enabled" , true ) ;
7279
7380 this .tracer = openTelemetrySdk .getTracer ("io.opentelemetry.contrib.maven" , VERSION );
7481 }
7582
83+ /**
84+ * The OTel SDK by default sends data to the OTLP gRPC endpoint localhost:4317 if no exporter and
85+ * no OTLP exporter endpoint are defined. This is not suited for a build tool for which we want
86+ * the OTel SDK to be disabled by default.
87+ *
88+ * <p>Change the OTel SDL behavior: if none of the exporter and the OTLP exporter endpoint are
89+ * defined, explicitly disable the exporter setting "{@code
90+ * otel.[traces,metrics,logs].exporter=none}"
91+ *
92+ * @return The properties to be returned by {@link
93+ * io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder#addPropertiesCustomizer(java.util.function.Function)}
94+ */
95+ static Map <String , String > requireExplicitConfigOfTheOtlpExporter (
96+ ConfigProperties configProperties ) {
97+
98+ Map <String , String > properties = new HashMap <>();
99+ if (configProperties .getString ("otel.exporter.otlp.endpoint" ) != null ) {
100+ logger .debug ("OpenTelemetry: OTLP exporter endpoint is explicitly configured" );
101+ return properties ;
102+ }
103+ String [] signalTypes = {"traces" , "metrics" , "logs" };
104+ for (String signalType : signalTypes ) {
105+ boolean isExporterImplicitlyConfiguredToOtlp =
106+ configProperties .getString ("otel." + signalType + ".exporter" ) == null ;
107+ boolean isOtlpExporterEndpointSpecified =
108+ configProperties .getString ("otel.exporter.otlp." + signalType + ".endpoint" ) != null ;
109+
110+ if (isExporterImplicitlyConfiguredToOtlp && !isOtlpExporterEndpointSpecified ) {
111+ logger .debug (
112+ "OpenTelemetry: Disabling default OTLP exporter endpoint for signal {} exporter" ,
113+ signalType );
114+ properties .put ("otel." + signalType + ".exporter" , "none" );
115+ }
116+ }
117+
118+ return properties ;
119+ }
120+
76121 @ PreDestroy
77122 @ Override
78123 public synchronized void close () {
@@ -97,6 +142,10 @@ public Tracer getTracer() {
97142 return this .tracer ;
98143 }
99144
145+ public ConfigProperties getConfigProperties () {
146+ return configProperties ;
147+ }
148+
100149 /** Returns the {@link ContextPropagators} for this {@link OpenTelemetry}. */
101150 public ContextPropagators getPropagators () {
102151 return this .openTelemetrySdk .getPropagators ();
@@ -105,17 +154,4 @@ public ContextPropagators getPropagators() {
105154 public boolean isMojosInstrumentationEnabled () {
106155 return mojosInstrumentationEnabled ;
107156 }
108-
109- @ Nullable
110- private static Boolean getBooleanConfig (String name ) {
111- String value = System .getProperty (name );
112- if (value != null ) {
113- return Boolean .parseBoolean (value );
114- }
115- value = System .getenv (name .toUpperCase (Locale .ROOT ).replace ('.' , '_' ));
116- if (value != null ) {
117- return Boolean .parseBoolean (value );
118- }
119- return null ;
120- }
121157}
0 commit comments