-
Notifications
You must be signed in to change notification settings - Fork 839
/
AutoConfiguredOpenTelemetrySdkBuilder.java
418 lines (372 loc) · 17.3 KB
/
AutoConfiguredOpenTelemetrySdkBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.sdk.autoconfigure;
import static java.util.Objects.requireNonNull;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.OpenTelemetrySdkBuilder;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.logs.SdkLogEmitterProvider;
import io.opentelemetry.sdk.logs.SdkLogEmitterProviderBuilder;
import io.opentelemetry.sdk.logs.export.LogExporter;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import io.opentelemetry.sdk.trace.samplers.Sampler;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
/**
* A builder for configuring auto-configuration of the OpenTelemetry SDK. Notably, auto-configured
* components can be customized, for example by delegating to them from a wrapper that tweaks
* behavior such as filtering out telemetry attributes.
*/
public final class AutoConfiguredOpenTelemetrySdkBuilder implements AutoConfigurationCustomizer {
private static final Logger logger =
Logger.getLogger(AutoConfiguredOpenTelemetrySdkBuilder.class.getName());
@Nullable private ConfigProperties config;
private BiFunction<SdkTracerProviderBuilder, ConfigProperties, SdkTracerProviderBuilder>
tracerProviderCustomizer = (a, unused) -> a;
private BiFunction<? super TextMapPropagator, ConfigProperties, ? extends TextMapPropagator>
propagatorCustomizer = (a, unused) -> a;
private BiFunction<? super SpanExporter, ConfigProperties, ? extends SpanExporter>
spanExporterCustomizer = (a, unused) -> a;
private BiFunction<? super Sampler, ConfigProperties, ? extends Sampler> samplerCustomizer =
(a, unused) -> a;
private BiFunction<SdkMeterProviderBuilder, ConfigProperties, SdkMeterProviderBuilder>
meterProviderCustomizer = (a, unused) -> a;
private BiFunction<? super MetricExporter, ConfigProperties, ? extends MetricExporter>
metricExporterCustomizer = (a, unused) -> a;
private BiFunction<SdkLogEmitterProviderBuilder, ConfigProperties, SdkLogEmitterProviderBuilder>
logEmitterProviderCustomizer = (a, unused) -> a;
private BiFunction<? super LogExporter, ConfigProperties, ? extends LogExporter>
logExporterCustomizer = (a, unused) -> a;
private BiFunction<? super Resource, ConfigProperties, ? extends Resource> resourceCustomizer =
(a, unused) -> a;
private Supplier<Map<String, String>> propertiesSupplier = Collections::emptyMap;
private ClassLoader serviceClassLoader =
AutoConfiguredOpenTelemetrySdkBuilder.class.getClassLoader();
private boolean registerShutdownHook = true;
private boolean setResultAsGlobal = true;
private boolean customized;
AutoConfiguredOpenTelemetrySdkBuilder() {}
/**
* Sets the {@link ConfigProperties} to use when resolving properties for auto-configuration.
* {@link #addPropertiesSupplier(Supplier)} will have no effect if this method is used.
*/
AutoConfiguredOpenTelemetrySdkBuilder setConfig(ConfigProperties config) {
requireNonNull(config, "config");
this.config = config;
return this;
}
/**
* Adds a {@link BiFunction} to invoke the with the {@link SdkTracerProviderBuilder} to allow
* customization. The return value of the {@link BiFunction} will replace the passed-in argument.
*
* <p>Multiple calls will execute the customizers in order.
*/
@Override
public AutoConfiguredOpenTelemetrySdkBuilder addTracerProviderCustomizer(
BiFunction<SdkTracerProviderBuilder, ConfigProperties, SdkTracerProviderBuilder>
tracerProviderCustomizer) {
requireNonNull(tracerProviderCustomizer, "tracerProviderCustomizer");
this.tracerProviderCustomizer =
mergeCustomizer(this.tracerProviderCustomizer, tracerProviderCustomizer);
return this;
}
/**
* Adds a {@link BiFunction} to invoke with the default autoconfigured {@link TextMapPropagator}
* to allow customization. The return value of the {@link BiFunction} will replace the passed-in
* argument.
*
* <p>Multiple calls will execute the customizers in order.
*/
@Override
public AutoConfiguredOpenTelemetrySdkBuilder addPropagatorCustomizer(
BiFunction<? super TextMapPropagator, ConfigProperties, ? extends TextMapPropagator>
propagatorCustomizer) {
requireNonNull(propagatorCustomizer, "propagatorCustomizer");
this.propagatorCustomizer = mergeCustomizer(this.propagatorCustomizer, propagatorCustomizer);
return this;
}
/**
* Adds a {@link BiFunction} to invoke with the default autoconfigured {@link Resource} to allow
* customization. The return value of the {@link BiFunction} will replace the passed-in argument.
*
* <p>Multiple calls will execute the customizers in order.
*/
@Override
public AutoConfiguredOpenTelemetrySdkBuilder addResourceCustomizer(
BiFunction<? super Resource, ConfigProperties, ? extends Resource> resourceCustomizer) {
requireNonNull(resourceCustomizer, "resourceCustomizer");
this.resourceCustomizer = mergeCustomizer(this.resourceCustomizer, resourceCustomizer);
return this;
}
/**
* Adds a {@link BiFunction} to invoke with the default autoconfigured {@link Sampler} to allow
* customization. The return value of the {@link BiFunction} will replace the passed-in argument.
*
* <p>Multiple calls will execute the customizers in order.
*/
@Override
public AutoConfiguredOpenTelemetrySdkBuilder addSamplerCustomizer(
BiFunction<? super Sampler, ConfigProperties, ? extends Sampler> samplerCustomizer) {
requireNonNull(samplerCustomizer, "samplerCustomizer");
this.samplerCustomizer = mergeCustomizer(this.samplerCustomizer, samplerCustomizer);
return this;
}
/**
* Adds a {@link BiFunction} to invoke with the default autoconfigured {@link SpanExporter} to
* allow customization. The return value of the {@link BiFunction} will replace the passed-in
* argument.
*
* <p>Multiple calls will execute the customizers in order.
*/
@Override
public AutoConfiguredOpenTelemetrySdkBuilder addSpanExporterCustomizer(
BiFunction<? super SpanExporter, ConfigProperties, ? extends SpanExporter>
spanExporterCustomizer) {
requireNonNull(spanExporterCustomizer, "spanExporterCustomizer");
this.spanExporterCustomizer =
mergeCustomizer(this.spanExporterCustomizer, spanExporterCustomizer);
return this;
}
/**
* Adds a {@link Supplier} of a map of property names and values to use as defaults for the {@link
* ConfigProperties} used during auto-configuration. The order of precedence of properties is
* system properties > environment variables > the suppliers registered with this method.
*
* <p>Multiple calls will cause properties to be merged in order, with later ones overwriting
* duplicate keys in earlier ones.
*/
@Override
public AutoConfiguredOpenTelemetrySdkBuilder addPropertiesSupplier(
Supplier<Map<String, String>> propertiesSupplier) {
requireNonNull(propertiesSupplier, "propertiesSupplier");
this.propertiesSupplier = mergeProperties(this.propertiesSupplier, propertiesSupplier);
return this;
}
/**
* Adds a {@link BiFunction} to invoke the with the {@link SdkMeterProviderBuilder} to allow
* customization. The return value of the {@link BiFunction} will replace the passed-in argument.
*
* <p>Multiple calls will execute the customizers in order.
*/
@Override
public AutoConfiguredOpenTelemetrySdkBuilder addMeterProviderCustomizer(
BiFunction<SdkMeterProviderBuilder, ConfigProperties, SdkMeterProviderBuilder>
meterProviderCustomizer) {
requireNonNull(meterProviderCustomizer, "meterProviderCustomizer");
this.meterProviderCustomizer =
mergeCustomizer(this.meterProviderCustomizer, meterProviderCustomizer);
return this;
}
/**
* Adds a {@link BiFunction} to invoke with the default autoconfigured {@link SpanExporter} to
* allow customization. The return value of the {@link BiFunction} will replace the passed-in
* argument.
*
* <p>Multiple calls will execute the customizers in order.
*/
@Override
public AutoConfiguredOpenTelemetrySdkBuilder addMetricExporterCustomizer(
BiFunction<? super MetricExporter, ConfigProperties, ? extends MetricExporter>
metricExporterCustomizer) {
requireNonNull(metricExporterCustomizer, "metricExporterCustomizer");
this.metricExporterCustomizer =
mergeCustomizer(this.metricExporterCustomizer, metricExporterCustomizer);
return this;
}
/**
* Adds a {@link BiFunction} to invoke the with the {@link SdkLogEmitterProviderBuilder} to allow
* customization. The return value of the {@link BiFunction} will replace the passed-in argument.
*
* <p>Multiple calls will execute the customizers in order.
*/
@Override
public AutoConfiguredOpenTelemetrySdkBuilder addLogEmitterProviderCustomizer(
BiFunction<SdkLogEmitterProviderBuilder, ConfigProperties, SdkLogEmitterProviderBuilder>
logEmitterProviderCustomizer) {
requireNonNull(logEmitterProviderCustomizer, "logEmitterProviderCustomizer");
this.logEmitterProviderCustomizer =
mergeCustomizer(this.logEmitterProviderCustomizer, logEmitterProviderCustomizer);
return this;
}
/**
* Adds a {@link BiFunction} to invoke with the default autoconfigured {@link LogExporter} to
* allow customization. The return value of the {@link BiFunction} will replace the passed-in
* argument.
*
* <p>Multiple calls will execute the customizers in order.
*/
@Override
public AutoConfiguredOpenTelemetrySdkBuilder addLogExporterCustomizer(
BiFunction<? super LogExporter, ConfigProperties, ? extends LogExporter>
logExporterCustomizer) {
requireNonNull(logExporterCustomizer, "logExporterCustomizer");
this.logExporterCustomizer = mergeCustomizer(this.logExporterCustomizer, logExporterCustomizer);
return this;
}
/**
* Control the registration of a shutdown hook to shut down the SDK when appropriate. By default,
* the shutdown hook is registered.
*
* <p>Skipping the registration of the shutdown hook may cause unexpected behavior. This
* configuration is for SDK consumers that require control over the SDK lifecycle. In this case,
* alternatives must be provided by the SDK consumer to shut down the SDK.
*
* @param registerShutdownHook a boolean <code>true</code> will register the hook, otherwise
* <code>false</code> will skip registration.
*/
public AutoConfiguredOpenTelemetrySdkBuilder registerShutdownHook(boolean registerShutdownHook) {
this.registerShutdownHook = registerShutdownHook;
return this;
}
/**
* Sets whether the configured {@link OpenTelemetrySdk} should be set as the application's
* {@linkplain io.opentelemetry.api.GlobalOpenTelemetry global} instance.
*/
public AutoConfiguredOpenTelemetrySdkBuilder setResultAsGlobal(boolean setResultAsGlobal) {
this.setResultAsGlobal = setResultAsGlobal;
return this;
}
/** Sets the {@link ClassLoader} to be used to load SPI implementations. */
public AutoConfiguredOpenTelemetrySdkBuilder setServiceClassLoader(
ClassLoader serviceClassLoader) {
requireNonNull(serviceClassLoader, "serviceClassLoader");
this.serviceClassLoader = serviceClassLoader;
return this;
}
/**
* Returns a new {@link AutoConfiguredOpenTelemetrySdk} holding components auto-configured using
* the settings of this {@link AutoConfiguredOpenTelemetrySdkBuilder}.
*/
public AutoConfiguredOpenTelemetrySdk build() {
if (!customized) {
customized = true;
mergeSdkTracerProviderConfigurer();
for (AutoConfigurationCustomizerProvider customizer :
ServiceLoader.load(AutoConfigurationCustomizerProvider.class, serviceClassLoader)) {
customizer.customize(this);
}
}
ConfigProperties config = getConfig();
Resource resource =
ResourceConfiguration.configureResource(config, serviceClassLoader, resourceCustomizer);
OpenTelemetrySdk openTelemetrySdk = OpenTelemetrySdk.builder().build();
boolean sdkEnabled =
Optional.ofNullable(config.getBoolean("otel.experimental.sdk.enabled")).orElse(true);
if (sdkEnabled) {
SdkMeterProviderBuilder meterProviderBuilder = SdkMeterProvider.builder();
meterProviderBuilder.setResource(resource);
MeterProviderConfiguration.configureMeterProvider(
meterProviderBuilder, config, serviceClassLoader, metricExporterCustomizer);
meterProviderBuilder = meterProviderCustomizer.apply(meterProviderBuilder, config);
SdkMeterProvider meterProvider = meterProviderBuilder.build();
SdkTracerProviderBuilder tracerProviderBuilder = SdkTracerProvider.builder();
tracerProviderBuilder.setResource(resource);
TracerProviderConfiguration.configureTracerProvider(
tracerProviderBuilder,
config,
serviceClassLoader,
meterProvider,
spanExporterCustomizer,
samplerCustomizer);
tracerProviderBuilder = tracerProviderCustomizer.apply(tracerProviderBuilder, config);
SdkTracerProvider tracerProvider = tracerProviderBuilder.build();
SdkLogEmitterProviderBuilder logEmitterProviderBuilder = SdkLogEmitterProvider.builder();
logEmitterProviderBuilder.setResource(resource);
LogEmitterProviderConfiguration.configureLogEmitterProvider(
logEmitterProviderBuilder, config, meterProvider, logExporterCustomizer);
logEmitterProviderBuilder =
logEmitterProviderCustomizer.apply(logEmitterProviderBuilder, config);
SdkLogEmitterProvider logEmitterProvider = logEmitterProviderBuilder.build();
if (registerShutdownHook) {
Runtime.getRuntime()
.addShutdownHook(
new Thread(
() -> {
List<CompletableResultCode> shutdown = new ArrayList<>();
shutdown.add(tracerProvider.shutdown());
shutdown.add(meterProvider.shutdown());
shutdown.add(logEmitterProvider.shutdown());
CompletableResultCode.ofAll(shutdown).join(10, TimeUnit.SECONDS);
}));
}
ContextPropagators propagators =
PropagatorConfiguration.configurePropagators(
config, serviceClassLoader, propagatorCustomizer);
OpenTelemetrySdkBuilder sdkBuilder =
OpenTelemetrySdk.builder()
.setTracerProvider(tracerProvider)
.setLogEmitterProvider(logEmitterProvider)
.setMeterProvider(meterProvider)
.setPropagators(propagators);
openTelemetrySdk = sdkBuilder.build();
}
if (setResultAsGlobal) {
GlobalOpenTelemetry.set(openTelemetrySdk);
logger.log(
Level.FINE, "Global OpenTelemetry set to {0} by autoconfiguration", openTelemetrySdk);
}
return AutoConfiguredOpenTelemetrySdk.create(openTelemetrySdk, resource, config);
}
@SuppressWarnings("deprecation") // Support deprecated SdkTracerProviderConfigurer
private void mergeSdkTracerProviderConfigurer() {
for (io.opentelemetry.sdk.autoconfigure.spi.traces.SdkTracerProviderConfigurer configurer :
ServiceLoader.load(
io.opentelemetry.sdk.autoconfigure.spi.traces.SdkTracerProviderConfigurer.class,
serviceClassLoader)) {
addTracerProviderCustomizer(
(builder, config) -> {
configurer.configure(builder, config);
return builder;
});
}
}
private ConfigProperties getConfig() {
ConfigProperties config = this.config;
if (config == null) {
config = DefaultConfigProperties.get(propertiesSupplier.get());
}
return config;
}
private static <I, O1, O2> BiFunction<I, ConfigProperties, O2> mergeCustomizer(
BiFunction<? super I, ConfigProperties, ? extends O1> first,
BiFunction<? super O1, ConfigProperties, ? extends O2> second) {
return (I configured, ConfigProperties config) -> {
O1 firstResult = first.apply(configured, config);
return second.apply(firstResult, config);
};
}
private static Supplier<Map<String, String>> mergeProperties(
Supplier<Map<String, String>> first, Supplier<Map<String, String>> second) {
return () -> {
Map<String, String> merged = new HashMap<>();
merged.putAll(first.get());
merged.putAll(second.get());
return merged;
};
}
}