use of io.opentelemetry.sdk.OpenTelemetrySdk 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;
}
use of io.opentelemetry.sdk.OpenTelemetrySdk in project opentelemetry-java by open-telemetry.
the class AutoConfiguredOpenTelemetrySdkTest method tracerProviderCustomizer.
@Test
void tracerProviderCustomizer() {
InMemorySpanExporter spanExporter = InMemorySpanExporter.create();
AutoConfiguredOpenTelemetrySdkBuilder autoConfiguration = AutoConfiguredOpenTelemetrySdk.builder().addTracerProviderCustomizer((tracerProviderBuilder, config) -> {
tracerProviderBuilder.setResource(Resource.builder().put("cat", "meow").build());
return tracerProviderBuilder.addSpanProcessor(SimpleSpanProcessor.create(spanExporter));
}).addResourceCustomizer((resource, config) -> resource.merge(Resource.builder().put("cow", "moo").build())).addPropertiesSupplier(() -> Collections.singletonMap("otel.metrics.exporter", "none")).addPropertiesSupplier(() -> Collections.singletonMap("otel.traces.exporter", "none")).addPropertiesSupplier(() -> Collections.singletonMap("otel.logs.exporter", "none")).setResultAsGlobal(false);
GlobalOpenTelemetry.set(OpenTelemetry.noop());
AutoConfiguredOpenTelemetrySdk autoConfigured = autoConfiguration.build();
assertThat(autoConfigured.getResource().getAttribute(stringKey("cow"))).isEqualTo("moo");
OpenTelemetrySdk sdk = autoConfigured.getOpenTelemetrySdk();
sdk.getTracerProvider().get("test").spanBuilder("test").startSpan().end();
List<SpanData> spanItems = spanExporter.getFinishedSpanItems();
assertThat(spanItems.size()).isEqualTo(1);
SpanData spanData = spanItems.get(0);
assertThat(spanData.getResource().getAttribute(stringKey("cat"))).isEqualTo("meow");
// Ensures the export happened.
sdk.getSdkTracerProvider().shutdown().join(10, TimeUnit.SECONDS);
}
use of io.opentelemetry.sdk.OpenTelemetrySdk in project opentelemetry-java by open-telemetry.
the class AutoConfiguredOpenTelemetrySdkTest method builder_addPropagatorCustomizer.
@Test
void builder_addPropagatorCustomizer() {
Context extracted = Context.root().with(ContextKey.named("animal"), "bear");
when(propagator2.extract(any(), any(), any())).thenReturn(extracted);
OpenTelemetrySdk sdk = builder.addPropertiesSupplier(() -> Collections.singletonMap("otel.propagators", "tracecontext")).addPropagatorCustomizer((previous, config) -> {
assertThat(previous).isSameAs(W3CTraceContextPropagator.getInstance());
return propagator1;
}).addPropagatorCustomizer((previous, config) -> {
assertThat(previous).isSameAs(propagator1);
return propagator2;
}).build().getOpenTelemetrySdk();
assertThat(sdk.getPropagators().getTextMapPropagator().extract(Context.root(), Collections.emptyMap(), getter)).isEqualTo(extracted);
}
use of io.opentelemetry.sdk.OpenTelemetrySdk in project opentelemetry-java by open-telemetry.
the class AutoConfiguredOpenTelemetrySdkTest method builder_setResultAsGlobalTrue.
@Test
void builder_setResultAsGlobalTrue() {
OpenTelemetrySdk openTelemetry = builder.setResultAsGlobal(true).build().getOpenTelemetrySdk();
assertThat(GlobalOpenTelemetry.get()).extracting("delegate").isSameAs(openTelemetry);
}
use of io.opentelemetry.sdk.OpenTelemetrySdk in project opentelemetry-java by open-telemetry.
the class AutoConfiguredOpenTelemetrySdkTest method initializeAndGet.
@Test
void initializeAndGet() {
OpenTelemetrySdk sdk = AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk();
assertThat(GlobalOpenTelemetry.get()).extracting("delegate").isSameAs(sdk);
}
Aggregations