|
10 | 10 | import io.opentelemetry.context.propagation.ContextPropagators; |
11 | 11 | import io.opentelemetry.maven.semconv.MavenOtelSemanticAttributes; |
12 | 12 | import io.opentelemetry.sdk.OpenTelemetrySdk; |
13 | | -import io.opentelemetry.sdk.autoconfigure.OpenTelemetrySdkAutoConfiguration; |
| 13 | +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; |
| 14 | +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder; |
14 | 15 | import io.opentelemetry.sdk.common.CompletableResultCode; |
| 16 | +import io.opentelemetry.sdk.resources.Resource; |
15 | 17 | import io.opentelemetry.sdk.trace.export.SpanExporter; |
16 | 18 | import io.opentelemetry.semconv.resource.attributes.ResourceAttributes; |
17 | | -import java.util.Map; |
| 19 | +import java.util.Collections; |
18 | 20 | import java.util.concurrent.TimeUnit; |
19 | 21 | import org.apache.maven.rtinfo.RuntimeInformation; |
20 | 22 | import org.codehaus.plexus.component.annotations.Component; |
|
25 | 27 | import org.slf4j.Logger; |
26 | 28 | import org.slf4j.LoggerFactory; |
27 | 29 |
|
28 | | -/** |
29 | | - * Service to configure the {@link OpenTelemetry} instance. |
30 | | - * |
31 | | - * <p>Rely on the OpenTelemetry SDK AutoConfiguration extension. Parameters are passed as system |
32 | | - * properties. |
33 | | - * |
34 | | - * <p>TODO: verify how we could use a composite {@link |
35 | | - * io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties} combining the config passed by JVM |
36 | | - * system properties and environment variables with overrides injected by the Otel Maven Extension |
37 | | - */ |
| 30 | +/** Service to configure the {@link OpenTelemetry} instance. */ |
38 | 31 | @Component(role = OpenTelemetrySdkService.class, hint = "opentelemetry-service") |
39 | 32 | public final class OpenTelemetrySdkService implements Initializable, Disposable { |
40 | 33 |
|
@@ -89,60 +82,49 @@ public synchronized void dispose() { |
89 | 82 | @Override |
90 | 83 | public void initialize() throws InitializationException { |
91 | 84 | logger.debug("OpenTelemetry: initialize OpenTelemetrySdkService..."); |
92 | | - if (StringUtils.isBlank( |
93 | | - OtelUtils.getSystemPropertyOrEnvironmentVariable( |
94 | | - "otel.exporter.otlp.endpoint", "OTEL_EXPORTER_OTLP_ENDPOINT", null))) { |
| 85 | + AutoConfiguredOpenTelemetrySdkBuilder autoConfiguredSdkBuilder = |
| 86 | + AutoConfiguredOpenTelemetrySdk.builder(); |
| 87 | + |
| 88 | + // SDK CONFIGURATION PROPERTIES |
| 89 | + autoConfiguredSdkBuilder.addPropertiesSupplier( |
| 90 | + () -> { |
| 91 | + // Change default of "otel.traces.exporter" from "otlp" to "none" |
| 92 | + // The impacts are |
| 93 | + // * If no otel exporter settings are passed, then the Maven extension will not export |
| 94 | + // rather than exporting on OTLP GRPC to http://localhost:4317 |
| 95 | + // * If OTEL_EXPORTER_OTLP_ENDPOINT is defined but OTEL_TRACES_EXPORTER is not, then don't |
| 96 | + // export |
| 97 | + return Collections.singletonMap("otel.traces.exporter", "none"); |
| 98 | + }); |
| 99 | + |
| 100 | + // SDK RESOURCE |
| 101 | + // Don't use the `io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider` framework because we |
| 102 | + // need to get the `RuntimeInformation` component injected by Plexus |
| 103 | + autoConfiguredSdkBuilder.addResourceCustomizer( |
| 104 | + (resource, configProperties) -> |
| 105 | + Resource.builder() |
| 106 | + .putAll(resource) |
| 107 | + .put( |
| 108 | + ResourceAttributes.SERVICE_NAME, MavenOtelSemanticAttributes.SERVICE_NAME_VALUE) |
| 109 | + .put(ResourceAttributes.SERVICE_VERSION, runtimeInformation.getMavenVersion()) |
| 110 | + .build()); |
| 111 | + |
| 112 | + // BUILD SDK |
| 113 | + AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk = |
| 114 | + autoConfiguredSdkBuilder.build(); |
| 115 | + if (logger.isDebugEnabled()) { |
95 | 116 | logger.debug( |
96 | | - "OpenTelemetry: No -Dotel.exporter.otlp.endpoint property or OTEL_EXPORTER_OTLP_ENDPOINT " |
97 | | - + "environment variable found, use a NOOP OpenTelemetry SDK"); |
98 | | - } else { |
99 | | - { |
100 | | - // Don't use a {@code io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider} to inject |
101 | | - // Maven runtime attributes due to a classloading issue when loading the Maven OpenTelemetry |
102 | | - // extension as a pom.xml {@code <extension>}. |
103 | | - String initialCommaSeparatedAttributes = |
104 | | - OtelUtils.getSystemPropertyOrEnvironmentVariable( |
105 | | - "otel.resource.attributes", "OTEL_RESOURCE_ATTRIBUTES", ""); |
106 | | - Map<String, String> attributes = |
107 | | - OtelUtils.getCommaSeparatedMap(initialCommaSeparatedAttributes); |
108 | | - |
109 | | - // service.name |
110 | | - String serviceName = |
111 | | - OtelUtils.getSystemPropertyOrEnvironmentVariable( |
112 | | - "otel.service.name", "OTEL_SERVICE_NAME", null); |
113 | | - |
114 | | - if (!attributes.containsKey(ResourceAttributes.SERVICE_NAME.getKey()) |
115 | | - && StringUtils.isBlank(serviceName)) { |
116 | | - // service.name is not defined in passed configuration, we define it |
117 | | - attributes.put( |
118 | | - ResourceAttributes.SERVICE_NAME.getKey(), |
119 | | - MavenOtelSemanticAttributes.ServiceNameValues.SERVICE_NAME_VALUE); |
120 | | - } |
121 | | - |
122 | | - // service.version |
123 | | - final String mavenVersion = this.runtimeInformation.getMavenVersion(); |
124 | | - if (!attributes.containsKey(ResourceAttributes.SERVICE_VERSION.getKey())) { |
125 | | - attributes.put(ResourceAttributes.SERVICE_VERSION.getKey(), mavenVersion); |
126 | | - } |
127 | | - |
128 | | - String newCommaSeparatedAttributes = OtelUtils.getCommaSeparatedString(attributes); |
129 | | - logger.debug( |
130 | | - "OpenTelemetry: Initial resource attributes: {}", initialCommaSeparatedAttributes); |
131 | | - logger.debug("OpenTelemetry: Use resource attributes: {}", newCommaSeparatedAttributes); |
132 | | - System.setProperty("otel.resource.attributes", newCommaSeparatedAttributes); |
133 | | - } |
134 | | - |
135 | | - this.openTelemetrySdk = OpenTelemetrySdkAutoConfiguration.initialize(false); |
136 | | - this.openTelemetry = this.openTelemetrySdk; |
| 117 | + "OpenTelemetry: OpenTelemetry SDK initialized with " |
| 118 | + + OtelUtils.prettyPrintSdkConfiguration(autoConfiguredOpenTelemetrySdk)); |
137 | 119 | } |
138 | | - |
139 | | - String mojosInstrumentationEnabledAsString = |
140 | | - System.getProperty( |
141 | | - "otel.instrumentation.maven.mojo.enabled", |
142 | | - System.getenv("OTEL_INSTRUMENTATION_MAVEN_MOJO_ENABLED")); |
143 | | - this.mojosInstrumentationEnabled = |
144 | | - Boolean.parseBoolean( |
145 | | - StringUtils.defaultIfBlank(mojosInstrumentationEnabledAsString, "true")); |
| 120 | + this.openTelemetrySdk = autoConfiguredOpenTelemetrySdk.getOpenTelemetrySdk(); |
| 121 | + this.openTelemetry = this.openTelemetrySdk; |
| 122 | + |
| 123 | + Boolean mojoSpansEnabled = |
| 124 | + autoConfiguredOpenTelemetrySdk |
| 125 | + .getConfig() |
| 126 | + .getBoolean("otel.instrumentation.maven.mojo.enabled"); |
| 127 | + this.mojosInstrumentationEnabled = mojoSpansEnabled == null ? true : mojoSpansEnabled; |
146 | 128 |
|
147 | 129 | this.tracer = this.openTelemetry.getTracer("io.opentelemetry.contrib.maven"); |
148 | 130 | } |
|
0 commit comments