Search in sources :

Example 1 with AutoConfiguredOpenTelemetrySdkBuilder

use of io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder in project opentelemetry-java-instrumentation by open-telemetry.

the class OpenTelemetryInstaller method installOpenTelemetrySdk.

/**
 * Install the {@link OpenTelemetrySdk} using autoconfigure, and return the {@link
 * AutoConfiguredOpenTelemetrySdk}.
 *
 * @return the {@link AutoConfiguredOpenTelemetrySdk}
 */
static AutoConfiguredOpenTelemetrySdk installOpenTelemetrySdk(Config config) {
    System.setProperty("io.opentelemetry.context.contextStorageProvider", "default");
    AutoConfiguredOpenTelemetrySdkBuilder builder = AutoConfiguredOpenTelemetrySdk.builder().setResultAsGlobal(true).addPropertiesSupplier(config::getAllProperties);
    ClassLoader classLoader = AgentInitializer.getExtensionsClassLoader();
    if (classLoader != null) {
        // May be null in unit tests.
        builder.setServiceClassLoader(classLoader);
    }
    AutoConfiguredOpenTelemetrySdk autoConfiguredSdk = builder.build();
    OpenTelemetrySdk sdk = autoConfiguredSdk.getOpenTelemetrySdk();
    OpenTelemetrySdkAccess.internalSetForceFlush((timeout, unit) -> {
        CompletableResultCode traceResult = sdk.getSdkTracerProvider().forceFlush();
        CompletableResultCode metricsResult = sdk.getSdkMeterProvider().forceFlush();
        CompletableResultCode.ofAll(Arrays.asList(traceResult, metricsResult)).join(timeout, unit);
    });
    SdkLogEmitterProvider sdkLogEmitterProvider = autoConfiguredSdk.getOpenTelemetrySdk().getSdkLogEmitterProvider();
    LogEmitterProvider logEmitterProvider = DelegatingLogEmitterProvider.from(sdkLogEmitterProvider);
    AgentLogEmitterProvider.set(logEmitterProvider);
    return autoConfiguredSdk;
}
Also used : AutoConfiguredOpenTelemetrySdkBuilder(io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder) AgentLogEmitterProvider(io.opentelemetry.javaagent.instrumentation.api.appender.internal.AgentLogEmitterProvider) DelegatingLogEmitterProvider(io.opentelemetry.instrumentation.sdk.appender.internal.DelegatingLogEmitterProvider) SdkLogEmitterProvider(io.opentelemetry.sdk.logs.SdkLogEmitterProvider) LogEmitterProvider(io.opentelemetry.instrumentation.api.appender.internal.LogEmitterProvider) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) AutoConfiguredOpenTelemetrySdk(io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk) SdkLogEmitterProvider(io.opentelemetry.sdk.logs.SdkLogEmitterProvider) AutoConfiguredOpenTelemetrySdk(io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk) OpenTelemetrySdk(io.opentelemetry.sdk.OpenTelemetrySdk)

Example 2 with AutoConfiguredOpenTelemetrySdkBuilder

use of io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder in project opentelemetry-plugin by jenkinsci.

the class OpenTelemetrySdkProvider method initialize.

public void initialize(@Nonnull OpenTelemetryConfiguration configuration) {
    // shutdown existing SDK
    preDestroy();
    AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder();
    // PROPERTIES
    sdkBuilder.addPropertiesSupplier(() -> configuration.toOpenTelemetryProperties());
    // RESOURCE
    sdkBuilder.addResourceCustomizer((resource, configProperties) -> {
        ResourceBuilder resourceBuilder = Resource.builder().putAll(resource).putAll(configuration.toOpenTelemetryResource());
        return resourceBuilder.build();
    });
    sdkBuilder.registerShutdownHook(// SDK closed by io.jenkins.plugins.opentelemetry.OpenTelemetrySdkProvider.preDestroy()
    false).setResultAsGlobal(// ensure GlobalOpenTelemetry.set() is invoked
    true);
    AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk = sdkBuilder.build();
    this.openTelemetrySdk = autoConfiguredOpenTelemetrySdk.getOpenTelemetrySdk();
    this.resource = autoConfiguredOpenTelemetrySdk.getResource();
    this.config = autoConfiguredOpenTelemetrySdk.getConfig();
    this.openTelemetry = this.openTelemetrySdk;
    this.tracer.setDelegate(openTelemetry.getTracer(GlobalOpenTelemetrySdk.INSTRUMENTATION_NAME, OtelUtils.getOpentelemetryPluginVersion()));
    this.logEmitter = openTelemetrySdk.getSdkLogEmitterProvider().get(GlobalOpenTelemetrySdk.INSTRUMENTATION_NAME);
    this.meter = openTelemetry.getMeterProvider().get(GlobalOpenTelemetrySdk.INSTRUMENTATION_NAME);
    LOGGER.log(Level.INFO, () -> "OpenTelemetry SDK initialized: " + OtelUtils.prettyPrintOtelSdkConfig(autoConfiguredOpenTelemetrySdk));
}
Also used : AutoConfiguredOpenTelemetrySdkBuilder(io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder) ResourceBuilder(io.opentelemetry.sdk.resources.ResourceBuilder) AutoConfiguredOpenTelemetrySdk(io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk)

Aggregations

AutoConfiguredOpenTelemetrySdk (io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk)2 AutoConfiguredOpenTelemetrySdkBuilder (io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder)2 LogEmitterProvider (io.opentelemetry.instrumentation.api.appender.internal.LogEmitterProvider)1 DelegatingLogEmitterProvider (io.opentelemetry.instrumentation.sdk.appender.internal.DelegatingLogEmitterProvider)1 AgentLogEmitterProvider (io.opentelemetry.javaagent.instrumentation.api.appender.internal.AgentLogEmitterProvider)1 OpenTelemetrySdk (io.opentelemetry.sdk.OpenTelemetrySdk)1 CompletableResultCode (io.opentelemetry.sdk.common.CompletableResultCode)1 SdkLogEmitterProvider (io.opentelemetry.sdk.logs.SdkLogEmitterProvider)1 ResourceBuilder (io.opentelemetry.sdk.resources.ResourceBuilder)1