-
Notifications
You must be signed in to change notification settings - Fork 868
/
OpenTelemetryInstaller.java
50 lines (41 loc) · 1.82 KB
/
OpenTelemetryInstaller.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
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.tooling;
import static java.util.Collections.singletonMap;
import io.opentelemetry.javaagent.bootstrap.OpenTelemetrySdkAccess;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import io.opentelemetry.sdk.common.CompletableResultCode;
import java.util.Arrays;
public final class OpenTelemetryInstaller {
/**
* Install the {@link OpenTelemetrySdk} using autoconfigure, and return the {@link
* AutoConfiguredOpenTelemetrySdk}.
*
* @return the {@link AutoConfiguredOpenTelemetrySdk}
*/
public static AutoConfiguredOpenTelemetrySdk installOpenTelemetrySdk(
ClassLoader extensionClassLoader) {
AutoConfiguredOpenTelemetrySdk autoConfiguredSdk =
AutoConfiguredOpenTelemetrySdk.builder()
.setResultAsGlobal()
.setServiceClassLoader(extensionClassLoader)
// disable the logs exporter by default for the time being
// FIXME remove this in the 2.x branch
.addPropertiesSupplier(() -> singletonMap("otel.logs.exporter", "none"))
.build();
OpenTelemetrySdk sdk = autoConfiguredSdk.getOpenTelemetrySdk();
OpenTelemetrySdkAccess.internalSetForceFlush(
(timeout, unit) -> {
CompletableResultCode traceResult = sdk.getSdkTracerProvider().forceFlush();
CompletableResultCode metricsResult = sdk.getSdkMeterProvider().forceFlush();
CompletableResultCode logsResult = sdk.getSdkLoggerProvider().forceFlush();
CompletableResultCode.ofAll(Arrays.asList(traceResult, metricsResult, logsResult))
.join(timeout, unit);
});
return autoConfiguredSdk;
}
private OpenTelemetryInstaller() {}
}