forked from open-telemetry/opentelemetry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoConfiguredOpenTelemetrySdkBuilder.java
610 lines (550 loc) · 25.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
* 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.api.events.GlobalEventEmitterProvider;
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.internal.SpiHelper;
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.autoconfigure.spi.ConfigurationException;
import io.opentelemetry.sdk.autoconfigure.spi.internal.AutoConfigureListener;
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
import io.opentelemetry.sdk.logs.LogRecordProcessor;
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder;
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
import io.opentelemetry.sdk.logs.internal.SdkEventEmitterProvider;
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.SpanProcessor;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import io.opentelemetry.sdk.trace.samplers.Sampler;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;
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.
*
* @since 1.28.0
*/
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 SpanProcessor, ConfigProperties, ? extends SpanProcessor>
spanProcessorCustomizer = (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<SdkLoggerProviderBuilder, ConfigProperties, SdkLoggerProviderBuilder>
loggerProviderCustomizer = (a, unused) -> a;
private BiFunction<? super LogRecordExporter, ConfigProperties, ? extends LogRecordExporter>
logRecordExporterCustomizer = (a, unused) -> a;
private BiFunction<? super LogRecordProcessor, ConfigProperties, ? extends LogRecordProcessor>
logRecordProcessorCustomizer = (a, unused) -> a;
private BiFunction<? super Resource, ConfigProperties, ? extends Resource> resourceCustomizer =
(a, unused) -> a;
private Supplier<Map<String, String>> propertiesSupplier = Collections::emptyMap;
private final List<Function<ConfigProperties, Map<String, String>>> propertiesCustomizers =
new ArrayList<>();
private SpiHelper spiHelper =
SpiHelper.create(AutoConfiguredOpenTelemetrySdk.class.getClassLoader());
private boolean registerShutdownHook = true;
private boolean setResultAsGlobal = false;
private boolean customized;
AutoConfiguredOpenTelemetrySdkBuilder() {}
/**
* Sets the {@link ConfigProperties} to use when resolving properties for auto-configuration.
* {@link #addPropertiesSupplier(Supplier)} and {@link #addPropertiesCustomizer(Function)} 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 BiFunction} to invoke for all autoconfigured {@link
* io.opentelemetry.sdk.trace.SpanProcessor}. The return value of the {@link BiFunction} will
* replace the passed-in argument. In contrast to {@link #addSpanExporterCustomizer(BiFunction)}
* this allows modifications to happen before batching occurs. As a result, it is possible to
* efficiently filter spans, add artificial spans or delay spans for enhancing them with external,
* delayed data.
*
* <p>Multiple calls will execute the customizers in order.
*/
@Override
public AutoConfiguredOpenTelemetrySdkBuilder addSpanProcessorCustomizer(
BiFunction<? super SpanProcessor, ConfigProperties, ? extends SpanProcessor>
spanProcessorCustomizer) {
requireNonNull(spanProcessorCustomizer, "spanProcessorCustomizer");
this.spanProcessorCustomizer =
mergeCustomizer(this.spanProcessorCustomizer, spanProcessorCustomizer);
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 Function} to invoke the with the {@link ConfigProperties} to allow customization.
* The return value of the {@link Function} will be merged into the {@link ConfigProperties}
* before it is used for auto-configuration, overwriting the properties that are already there.
*
* <p>Multiple calls will cause properties to be merged in order, with later ones overwriting
* duplicate keys in earlier ones.
*/
@Override
public AutoConfiguredOpenTelemetrySdkBuilder addPropertiesCustomizer(
Function<ConfigProperties, Map<String, String>> propertiesCustomizer) {
requireNonNull(propertiesCustomizer, "propertiesCustomizer");
this.propertiesCustomizers.add(propertiesCustomizer);
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 SdkLoggerProviderBuilder} 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 addLoggerProviderCustomizer(
BiFunction<SdkLoggerProviderBuilder, ConfigProperties, SdkLoggerProviderBuilder>
loggerProviderCustomizer) {
requireNonNull(loggerProviderCustomizer, "loggerProviderCustomizer");
this.loggerProviderCustomizer =
mergeCustomizer(this.loggerProviderCustomizer, loggerProviderCustomizer);
return this;
}
/**
* Adds a {@link BiFunction} to invoke with the default autoconfigured {@link LogRecordExporter}
* 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 addLogRecordExporterCustomizer(
BiFunction<? super LogRecordExporter, ConfigProperties, ? extends LogRecordExporter>
logRecordExporterCustomizer) {
requireNonNull(logRecordExporterCustomizer, "logRecordExporterCustomizer");
this.logRecordExporterCustomizer =
mergeCustomizer(this.logRecordExporterCustomizer, logRecordExporterCustomizer);
return this;
}
/**
* Adds a {@link BiFunction} to invoke for all autoconfigured {@link
* io.opentelemetry.sdk.logs.LogRecordProcessor}s. The return value of the {@link BiFunction} will
* replace the passed-in argument. In contrast to {@link
* #addLogRecordExporterCustomizer(BiFunction)} (BiFunction)} this allows modifications to happen
* before batching occurs. As a result, it is possible to efficiently filter logs, add artificial
* logs or delay logs for enhancing them with external, delayed data.
*
* <p>Multiple calls will execute the customizers in order.
*/
@Override
public AutoConfigurationCustomizer addLogRecordProcessorCustomizer(
BiFunction<? super LogRecordProcessor, ConfigProperties, ? extends LogRecordProcessor>
logRecordProcessorCustomizer) {
requireNonNull(logRecordProcessorCustomizer, "logRecordProcessorCustomizer");
this.logRecordProcessorCustomizer =
mergeCustomizer(this.logRecordProcessorCustomizer, logRecordProcessorCustomizer);
return this;
}
/**
* Disable 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.
*/
public AutoConfiguredOpenTelemetrySdkBuilder disableShutdownHook() {
this.registerShutdownHook = false;
return this;
}
/**
* Sets whether the configured {@link OpenTelemetrySdk} should be set as the application's
* {@linkplain io.opentelemetry.api.GlobalOpenTelemetry global} instance.
*
* <p>By default, {@link GlobalOpenTelemetry} is not set.
*/
public AutoConfiguredOpenTelemetrySdkBuilder setResultAsGlobal() {
this.setResultAsGlobal = true;
return this;
}
/** Sets the {@link ClassLoader} to be used to load SPI implementations. */
public AutoConfiguredOpenTelemetrySdkBuilder setServiceClassLoader(
ClassLoader serviceClassLoader) {
requireNonNull(serviceClassLoader, "serviceClassLoader");
this.spiHelper = SpiHelper.create(serviceClassLoader);
return this;
}
public AutoConfiguredOpenTelemetrySdkBuilder setComponentLoader(ComponentLoader componentLoader) {
requireNonNull(componentLoader, "componentLoader");
this.spiHelper = SpiHelper.create(componentLoader);
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 :
spiHelper.loadOrdered(AutoConfigurationCustomizerProvider.class)) {
customizer.customize(this);
}
}
ConfigProperties config = getConfig();
AutoConfiguredOpenTelemetrySdk fromFileConfiguration = maybeConfigureFromFile(config);
if (fromFileConfiguration != null) {
maybeRegisterShutdownHook(fromFileConfiguration.getOpenTelemetrySdk());
maybeSetAsGlobal(fromFileConfiguration.getOpenTelemetrySdk());
return fromFileConfiguration;
}
Resource resource =
ResourceConfiguration.configureResource(config, spiHelper, resourceCustomizer);
// Track any closeable resources created throughout configuration. If an exception short
// circuits configuration, partially configured components will be closed.
List<Closeable> closeables = new ArrayList<>();
try {
OpenTelemetrySdk openTelemetrySdk = OpenTelemetrySdk.builder().build();
boolean sdkEnabled = !config.getBoolean("otel.sdk.disabled", false);
if (sdkEnabled) {
SdkMeterProviderBuilder meterProviderBuilder = SdkMeterProvider.builder();
meterProviderBuilder.setResource(resource);
MeterProviderConfiguration.configureMeterProvider(
meterProviderBuilder, config, spiHelper, metricExporterCustomizer, closeables);
meterProviderBuilder = meterProviderCustomizer.apply(meterProviderBuilder, config);
SdkMeterProvider meterProvider = meterProviderBuilder.build();
closeables.add(meterProvider);
SdkTracerProviderBuilder tracerProviderBuilder = SdkTracerProvider.builder();
tracerProviderBuilder.setResource(resource);
TracerProviderConfiguration.configureTracerProvider(
tracerProviderBuilder,
config,
spiHelper,
meterProvider,
spanExporterCustomizer,
spanProcessorCustomizer,
samplerCustomizer,
closeables);
tracerProviderBuilder = tracerProviderCustomizer.apply(tracerProviderBuilder, config);
SdkTracerProvider tracerProvider = tracerProviderBuilder.build();
closeables.add(tracerProvider);
SdkLoggerProviderBuilder loggerProviderBuilder = SdkLoggerProvider.builder();
loggerProviderBuilder.setResource(resource);
LoggerProviderConfiguration.configureLoggerProvider(
loggerProviderBuilder,
config,
spiHelper,
meterProvider,
logRecordExporterCustomizer,
logRecordProcessorCustomizer,
closeables);
loggerProviderBuilder = loggerProviderCustomizer.apply(loggerProviderBuilder, config);
SdkLoggerProvider loggerProvider = loggerProviderBuilder.build();
closeables.add(loggerProvider);
ContextPropagators propagators =
PropagatorConfiguration.configurePropagators(config, spiHelper, propagatorCustomizer);
OpenTelemetrySdkBuilder sdkBuilder =
OpenTelemetrySdk.builder()
.setTracerProvider(tracerProvider)
.setLoggerProvider(loggerProvider)
.setMeterProvider(meterProvider)
.setPropagators(propagators);
openTelemetrySdk = sdkBuilder.build();
}
maybeRegisterShutdownHook(openTelemetrySdk);
maybeSetAsGlobal(openTelemetrySdk);
callAutoConfigureListeners(spiHelper, openTelemetrySdk);
return AutoConfiguredOpenTelemetrySdk.create(openTelemetrySdk, resource, config);
} catch (RuntimeException e) {
logger.info(
"Error encountered during autoconfiguration. Closing partially configured components.");
for (Closeable closeable : closeables) {
try {
logger.fine("Closing " + closeable.getClass().getName());
closeable.close();
} catch (IOException ex) {
logger.warning(
"Error closing " + closeable.getClass().getName() + ": " + ex.getMessage());
}
}
if (e instanceof ConfigurationException) {
throw e;
}
throw new ConfigurationException("Unexpected configuration error", e);
}
}
@Nullable
private static AutoConfiguredOpenTelemetrySdk maybeConfigureFromFile(ConfigProperties config) {
String configurationFile = config.getString("otel.config.file");
if (configurationFile == null || configurationFile.isEmpty()) {
return null;
}
logger.fine("Autoconfiguring from configuration file: " + configurationFile);
FileInputStream fis;
try {
fis = new FileInputStream(configurationFile);
} catch (FileNotFoundException e) {
throw new ConfigurationException("Configuration file not found", e);
}
try {
Class<?> configurationFactory =
Class.forName("io.opentelemetry.sdk.extension.incubator.fileconfig.FileConfiguration");
Method parseAndCreate = configurationFactory.getMethod("parseAndCreate", InputStream.class);
OpenTelemetrySdk sdk = (OpenTelemetrySdk) parseAndCreate.invoke(null, fis);
// Note: can't access file configuration resource without reflection so setting a dummy
// resource
return AutoConfiguredOpenTelemetrySdk.create(sdk, Resource.getDefault(), config);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException e) {
throw new ConfigurationException(
"Error configuring from file. Is opentelemetry-sdk-extension-incubator on the classpath?",
e);
} catch (InvocationTargetException e) {
Throwable cause = e.getCause();
if (cause instanceof ConfigurationException) {
throw (ConfigurationException) cause;
}
throw new ConfigurationException("Unexpected error configuring from file", e);
}
}
private void maybeRegisterShutdownHook(OpenTelemetrySdk openTelemetrySdk) {
if (!registerShutdownHook) {
return;
}
Runtime.getRuntime().addShutdownHook(shutdownHook(openTelemetrySdk));
}
private void maybeSetAsGlobal(OpenTelemetrySdk openTelemetrySdk) {
if (!setResultAsGlobal) {
return;
}
GlobalOpenTelemetry.set(openTelemetrySdk);
GlobalEventEmitterProvider.set(
SdkEventEmitterProvider.create(openTelemetrySdk.getSdkLoggerProvider()));
logger.log(
Level.FINE, "Global OpenTelemetry set to {0} by autoconfiguration", openTelemetrySdk);
}
// Visible for testing
void callAutoConfigureListeners(SpiHelper spiHelper, OpenTelemetrySdk openTelemetrySdk) {
for (AutoConfigureListener listener : spiHelper.getListeners()) {
try {
listener.afterAutoConfigure(openTelemetrySdk);
} catch (Throwable throwable) {
logger.log(
Level.WARNING, "Error invoking listener " + listener.getClass().getName(), throwable);
}
}
}
@SuppressWarnings("deprecation") // Support deprecated SdkTracerProviderConfigurer
private void mergeSdkTracerProviderConfigurer() {
for (io.opentelemetry.sdk.autoconfigure.spi.traces.SdkTracerProviderConfigurer configurer :
spiHelper.load(
io.opentelemetry.sdk.autoconfigure.spi.traces.SdkTracerProviderConfigurer.class)) {
addTracerProviderCustomizer(
(builder, config) -> {
configurer.configure(builder, config);
return builder;
});
}
}
private ConfigProperties getConfig() {
ConfigProperties config = this.config;
if (config == null) {
config = computeConfigProperties();
}
return config;
}
private ConfigProperties computeConfigProperties() {
return spiHelper
.loadOptional(ConfigProperties.class)
.orElseGet(
() -> {
DefaultConfigProperties properties =
DefaultConfigProperties.create(propertiesSupplier.get());
for (Function<ConfigProperties, Map<String, String>> customizer :
propertiesCustomizers) {
Map<String, String> overrides = customizer.apply(properties);
properties = properties.withOverrides(overrides);
}
return properties;
});
}
// Visible for testing
Thread shutdownHook(OpenTelemetrySdk sdk) {
return new Thread(sdk::close);
}
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;
};
}
}