Search in sources :

Example 1 with OpenTelemetrySdk

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;
}
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 OpenTelemetrySdk

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);
}
Also used : InMemorySpanExporter(io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Sampler(io.opentelemetry.sdk.trace.samplers.Sampler) BeforeEach(org.junit.jupiter.api.BeforeEach) LogProcessor(io.opentelemetry.sdk.logs.LogProcessor) SpanId(io.opentelemetry.api.trace.SpanId) W3CTraceContextPropagator(io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator) OpenTelemetrySdk(io.opentelemetry.sdk.OpenTelemetrySdk) Resource(io.opentelemetry.sdk.resources.Resource) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) ResourceAttributes(io.opentelemetry.semconv.resource.attributes.ResourceAttributes) TextMapPropagator(io.opentelemetry.context.propagation.TextMapPropagator) MetricReader(io.opentelemetry.sdk.metrics.export.MetricReader) SimpleSpanProcessor(io.opentelemetry.sdk.trace.export.SimpleSpanProcessor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) ContextKey(io.opentelemetry.context.ContextKey) Context(io.opentelemetry.context.Context) IdGenerator(io.opentelemetry.sdk.trace.IdGenerator) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Span(io.opentelemetry.api.trace.Span) SdkLogEmitterProvider(io.opentelemetry.sdk.logs.SdkLogEmitterProvider) OpenTelemetry(io.opentelemetry.api.OpenTelemetry) SpanExporter(io.opentelemetry.sdk.trace.export.SpanExporter) Mockito.when(org.mockito.Mockito.when) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) Mockito.verify(org.mockito.Mockito.verify) GlobalOpenTelemetry(io.opentelemetry.api.GlobalOpenTelemetry) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) SdkMeterProvider(io.opentelemetry.sdk.metrics.SdkMeterProvider) Mockito(org.mockito.Mockito) List(java.util.List) InMemorySpanExporter(io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter) AttributeKey.stringKey(io.opentelemetry.api.common.AttributeKey.stringKey) TextMapGetter(io.opentelemetry.context.propagation.TextMapGetter) MetricReaderFactory(io.opentelemetry.sdk.metrics.export.MetricReaderFactory) TraceId(io.opentelemetry.api.trace.TraceId) SpanData(io.opentelemetry.sdk.trace.data.SpanData) Collections(java.util.Collections) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) SpanData(io.opentelemetry.sdk.trace.data.SpanData) OpenTelemetrySdk(io.opentelemetry.sdk.OpenTelemetrySdk) Test(org.junit.jupiter.api.Test)

Example 3 with OpenTelemetrySdk

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);
}
Also used : Context(io.opentelemetry.context.Context) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Sampler(io.opentelemetry.sdk.trace.samplers.Sampler) BeforeEach(org.junit.jupiter.api.BeforeEach) LogProcessor(io.opentelemetry.sdk.logs.LogProcessor) SpanId(io.opentelemetry.api.trace.SpanId) W3CTraceContextPropagator(io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator) OpenTelemetrySdk(io.opentelemetry.sdk.OpenTelemetrySdk) Resource(io.opentelemetry.sdk.resources.Resource) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) ResourceAttributes(io.opentelemetry.semconv.resource.attributes.ResourceAttributes) TextMapPropagator(io.opentelemetry.context.propagation.TextMapPropagator) MetricReader(io.opentelemetry.sdk.metrics.export.MetricReader) SimpleSpanProcessor(io.opentelemetry.sdk.trace.export.SimpleSpanProcessor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) ContextKey(io.opentelemetry.context.ContextKey) Context(io.opentelemetry.context.Context) IdGenerator(io.opentelemetry.sdk.trace.IdGenerator) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Span(io.opentelemetry.api.trace.Span) SdkLogEmitterProvider(io.opentelemetry.sdk.logs.SdkLogEmitterProvider) OpenTelemetry(io.opentelemetry.api.OpenTelemetry) SpanExporter(io.opentelemetry.sdk.trace.export.SpanExporter) Mockito.when(org.mockito.Mockito.when) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) Mockito.verify(org.mockito.Mockito.verify) GlobalOpenTelemetry(io.opentelemetry.api.GlobalOpenTelemetry) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) SdkMeterProvider(io.opentelemetry.sdk.metrics.SdkMeterProvider) Mockito(org.mockito.Mockito) List(java.util.List) InMemorySpanExporter(io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter) AttributeKey.stringKey(io.opentelemetry.api.common.AttributeKey.stringKey) TextMapGetter(io.opentelemetry.context.propagation.TextMapGetter) MetricReaderFactory(io.opentelemetry.sdk.metrics.export.MetricReaderFactory) TraceId(io.opentelemetry.api.trace.TraceId) SpanData(io.opentelemetry.sdk.trace.data.SpanData) Collections(java.util.Collections) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) OpenTelemetrySdk(io.opentelemetry.sdk.OpenTelemetrySdk) Test(org.junit.jupiter.api.Test)

Example 4 with OpenTelemetrySdk

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);
}
Also used : OpenTelemetrySdk(io.opentelemetry.sdk.OpenTelemetrySdk) Test(org.junit.jupiter.api.Test)

Example 5 with OpenTelemetrySdk

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);
}
Also used : OpenTelemetrySdk(io.opentelemetry.sdk.OpenTelemetrySdk) Test(org.junit.jupiter.api.Test)

Aggregations

OpenTelemetrySdk (io.opentelemetry.sdk.OpenTelemetrySdk)12 Test (org.junit.jupiter.api.Test)6 SdkTracerProvider (io.opentelemetry.sdk.trace.SdkTracerProvider)5 Span (io.opentelemetry.api.trace.Span)4 SdkLogEmitterProvider (io.opentelemetry.sdk.logs.SdkLogEmitterProvider)4 InMemorySpanExporter (io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter)4 List (java.util.List)4 Context (io.opentelemetry.context.Context)3 CompletableResultCode (io.opentelemetry.sdk.common.CompletableResultCode)3 SdkMeterProvider (io.opentelemetry.sdk.metrics.SdkMeterProvider)3 Resource (io.opentelemetry.sdk.resources.Resource)3 SpanData (io.opentelemetry.sdk.trace.data.SpanData)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 GlobalOpenTelemetry (io.opentelemetry.api.GlobalOpenTelemetry)2 OpenTelemetry (io.opentelemetry.api.OpenTelemetry)2 AttributeKey.stringKey (io.opentelemetry.api.common.AttributeKey.stringKey)2 SpanId (io.opentelemetry.api.trace.SpanId)2 TraceId (io.opentelemetry.api.trace.TraceId)2 W3CTraceContextPropagator (io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator)2