dev.vality.woody.api.trace.context.ExampleConfiguration Maven / Gradle / Ivy
package dev.vality.woody.api.trace.context;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
/**
* All SDK management takes place here, away from the instrumentation code, which should only access
* the OpenTelemetry APIs.
*/
class ExampleConfiguration {
/**
* Initializes the OpenTelemetry SDK with a logging span exporter and the W3C Trace Context
* propagator.
*
* @return A ready-to-use {@link OpenTelemetry} instance.
*/
static OpenTelemetry initOpenTelemetry() {
SdkTracerProvider sdkTracerProvider =
SdkTracerProvider.builder()
.build();
OpenTelemetrySdk sdk =
OpenTelemetrySdk.builder()
.setTracerProvider(sdkTracerProvider)
.setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance()))
.build();
Runtime.getRuntime().addShutdownHook(new Thread(sdkTracerProvider::close));
return sdk;
}
}