Search in sources :

Example 11 with SdkTracerProvider

use of io.opentelemetry.sdk.trace.SdkTracerProvider in project opentelemetry-java by open-telemetry.

the class AutoConfiguredOpenTelemetrySdkTest method builder_addSamplerCustomizer.

@Test
void builder_addSamplerCustomizer() {
    SdkTracerProvider sdkTracerProvider = builder.addSamplerCustomizer((previous, config) -> {
        assertThat(previous).isEqualTo(Sampler.parentBased(Sampler.alwaysOn()));
        return sampler1;
    }).addSamplerCustomizer((previous, config) -> {
        assertThat(previous).isSameAs(sampler1);
        return sampler2;
    }).build().getOpenTelemetrySdk().getSdkTracerProvider();
    assertThat(sdkTracerProvider).extracting("sharedState").extracting("sampler").isEqualTo(sampler2);
}
Also used : 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) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) Test(org.junit.jupiter.api.Test)

Example 12 with SdkTracerProvider

use of io.opentelemetry.sdk.trace.SdkTracerProvider in project opentelemetry-java by open-telemetry.

the class OpenTracingShimTest method createTracerShim_fromOpenTelemetryInstance.

@Test
void createTracerShim_fromOpenTelemetryInstance() {
    OpenTelemetry openTelemetry = mock(OpenTelemetry.class);
    SdkTracerProvider sdk = SdkTracerProvider.builder().build();
    when(openTelemetry.getTracerProvider()).thenReturn(sdk);
    ContextPropagators contextPropagators = mock(ContextPropagators.class);
    when(contextPropagators.getTextMapPropagator()).thenReturn(mock(TextMapPropagator.class));
    when(openTelemetry.getPropagators()).thenReturn(contextPropagators);
    TracerShim tracerShim = (TracerShim) OpenTracingShim.createTracerShim(openTelemetry);
    assertThat(tracerShim.tracer()).isEqualTo(sdk.get("opentracingshim"));
}
Also used : ContextPropagators(io.opentelemetry.context.propagation.ContextPropagators) OpenTelemetry(io.opentelemetry.api.OpenTelemetry) GlobalOpenTelemetry(io.opentelemetry.api.GlobalOpenTelemetry) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) TextMapPropagator(io.opentelemetry.context.propagation.TextMapPropagator) Test(org.junit.jupiter.api.Test)

Example 13 with SdkTracerProvider

use of io.opentelemetry.sdk.trace.SdkTracerProvider in project opentelemetry-java by open-telemetry.

the class SpanBuilderShimTest method setAttributes_beforeSpanStart.

@Test
void setAttributes_beforeSpanStart() {
    SdkTracerProvider tracerSdkFactory = SdkTracerProvider.builder().setSampler(SamplingPrioritySampler.INSTANCE).build();
    Tracer tracer = tracerSdkFactory.get("SpanShimTest");
    TelemetryInfo telemetryInfo = new TelemetryInfo(tracer, OpenTracingPropagators.builder().build());
    SpanBuilderShim spanBuilder1 = new SpanBuilderShim(telemetryInfo, SPAN_NAME);
    SpanBuilderShim spanBuilder2 = new SpanBuilderShim(telemetryInfo, SPAN_NAME);
    SpanShim span1 = (SpanShim) spanBuilder1.withTag(SamplingPrioritySampler.SAMPLING_PRIORITY_TAG, SamplingPrioritySampler.UNSAMPLED_VALUE).start();
    SpanShim span2 = (SpanShim) spanBuilder2.withTag(SamplingPrioritySampler.SAMPLING_PRIORITY_TAG, SamplingPrioritySampler.SAMPLED_VALUE).start();
    assertThat(span1.getSpan().isRecording()).isFalse();
    assertThat(span2.getSpan().isRecording()).isTrue();
    assertThat(span1.getSpan().getSpanContext().isSampled()).isFalse();
    assertThat(span2.getSpan().getSpanContext().isSampled()).isTrue();
}
Also used : Tracer(io.opentelemetry.api.trace.Tracer) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) Test(org.junit.jupiter.api.Test)

Example 14 with SdkTracerProvider

use of io.opentelemetry.sdk.trace.SdkTracerProvider in project opentelemetry-java by open-telemetry.

the class BatchSpanProcessorTest method continuesIfExporterTimesOut.

@Test
@Timeout(5)
public void continuesIfExporterTimesOut() throws InterruptedException {
    int exporterTimeoutMillis = 10;
    BatchSpanProcessor bsp = BatchSpanProcessor.builder(mockSpanExporter).setExporterTimeout(exporterTimeoutMillis, TimeUnit.MILLISECONDS).setScheduleDelay(1, TimeUnit.MILLISECONDS).setMaxQueueSize(1).build();
    sdkTracerProvider = SdkTracerProvider.builder().addSpanProcessor(bsp).build();
    CountDownLatch exported = new CountDownLatch(1);
    // We return a result we never complete, meaning it will timeout.
    when(mockSpanExporter.export(argThat(spans -> {
        assertThat(spans).anySatisfy(span -> assertThat(span.getName()).isEqualTo(SPAN_NAME_1));
        exported.countDown();
        return true;
    }))).thenReturn(new CompletableResultCode());
    createEndedSpan(SPAN_NAME_1);
    exported.await();
    // Timed out so the span was dropped.
    await().untilAsserted(() -> assertThat(bsp.getBatch()).isEmpty());
    // Still processing new spans.
    CountDownLatch exportedAgain = new CountDownLatch(1);
    reset(mockSpanExporter);
    when(mockSpanExporter.shutdown()).thenReturn(CompletableResultCode.ofSuccess());
    when(mockSpanExporter.export(argThat(spans -> {
        assertThat(spans).anySatisfy(span -> assertThat(span.getName()).isEqualTo(SPAN_NAME_2));
        exportedAgain.countDown();
        return true;
    }))).thenReturn(CompletableResultCode.ofSuccess());
    createEndedSpan(SPAN_NAME_2);
    exported.await();
    await().untilAsserted(() -> assertThat(bsp.getBatch()).isEmpty());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Sampler(io.opentelemetry.sdk.trace.samplers.Sampler) SamplingResult(io.opentelemetry.sdk.trace.samplers.SamplingResult) Strictness(org.mockito.quality.Strictness) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) ArgumentMatchers(org.mockito.ArgumentMatchers) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tracer(io.opentelemetry.api.trace.Tracer) ArrayList(java.util.ArrayList) Mockito.doThrow(org.mockito.Mockito.doThrow) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Nullable(javax.annotation.Nullable) Awaitility.await(org.awaitility.Awaitility.await) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) GuardedBy(io.opentelemetry.api.internal.GuardedBy) Span(io.opentelemetry.api.trace.Span) Collection(java.util.Collection) Mockito.when(org.mockito.Mockito.when) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) ReadableSpan(io.opentelemetry.sdk.trace.ReadableSpan) SuppressLogger(io.opentelemetry.internal.testing.slf4j.SuppressLogger) SpanData(io.opentelemetry.sdk.trace.data.SpanData) Mockito.reset(org.mockito.Mockito.reset) Timeout(org.junit.jupiter.api.Timeout) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 15 with SdkTracerProvider

use of io.opentelemetry.sdk.trace.SdkTracerProvider in project opentelemetry-java-contrib by open-telemetry.

the class MojoGoalExecutionHandlerTest method testSpringBootBuildImage_springboot_2.

@Test
public void testSpringBootBuildImage_springboot_2() throws Exception {
    String pomXmlPath = "projects/springboot_2/pom.xml";
    String mojoGroupId = "org.springframework.boot";
    String mojoArtifactId = "spring-boot-maven-plugin";
    String mojoVersion = "2.5.6";
    String mojoGoal = "build-image";
    MavenProject project = newMavenProject(pomXmlPath);
    ExecutionEvent executionEvent = newMojoStartedExecutionEvent(project, mojoGroupId, mojoArtifactId, mojoVersion, mojoGoal);
    SpringBootBuildImageHandler buildImageHandler = new SpringBootBuildImageHandler();
    List<MavenGoal> supportedGoals = buildImageHandler.getSupportedGoals();
    assertThat(supportedGoals).isEqualTo(Collections.singletonList(MavenGoal.create(mojoGroupId, mojoArtifactId, mojoGoal)));
    try (SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder().build()) {
        SpanBuilder spanBuilder = sdkTracerProvider.tracerBuilder("test-tracer").build().spanBuilder("spring-boot:build-image");
        buildImageHandler.enrichSpan(spanBuilder, executionEvent);
        ReadableSpan span = (ReadableSpan) spanBuilder.startSpan();
        // TODO improve the Maven test harness that interpolates the maven properties like
        // ${project.artifactId}
        assertThat(span.getAttribute(MavenOtelSemanticAttributes.MAVEN_BUILD_CONTAINER_IMAGE_NAME)).isEqualTo("docker.io/john/${project.artifactId}");
        assertThat(span.getAttribute(MavenOtelSemanticAttributes.MAVEN_BUILD_CONTAINER_IMAGE_TAGS)).isEqualTo(Collections.singletonList("${project.version}"));
        assertThat(span.getAttribute(SemanticAttributes.HTTP_URL)).isEqualTo("https://docker.io");
        assertThat(span.getAttribute(SemanticAttributes.PEER_SERVICE)).isEqualTo("docker.io");
        assertThat(span.getAttribute(MavenOtelSemanticAttributes.MAVEN_BUILD_CONTAINER_REGISTRY_URL)).isEqualTo("https://docker.io");
    }
}
Also used : SpanBuilder(io.opentelemetry.api.trace.SpanBuilder) MavenProject(org.apache.maven.project.MavenProject) ExecutionEvent(org.apache.maven.execution.ExecutionEvent) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) ReadableSpan(io.opentelemetry.sdk.trace.ReadableSpan) MavenGoal(io.opentelemetry.maven.MavenGoal) Test(org.junit.jupiter.api.Test)

Aggregations

SdkTracerProvider (io.opentelemetry.sdk.trace.SdkTracerProvider)39 Test (org.junit.jupiter.api.Test)20 ReadableSpan (io.opentelemetry.sdk.trace.ReadableSpan)9 Resource (io.opentelemetry.sdk.resources.Resource)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 SpanBuilder (io.opentelemetry.api.trace.SpanBuilder)7 MavenGoal (io.opentelemetry.maven.MavenGoal)7 OpenTelemetrySdk (io.opentelemetry.sdk.OpenTelemetrySdk)7 ExecutionEvent (org.apache.maven.execution.ExecutionEvent)7 MavenProject (org.apache.maven.project.MavenProject)7 Span (io.opentelemetry.api.trace.Span)6 Tracer (io.opentelemetry.api.trace.Tracer)6 SpanData (io.opentelemetry.sdk.trace.data.SpanData)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 List (java.util.List)5 GlobalOpenTelemetry (io.opentelemetry.api.GlobalOpenTelemetry)4 OpenTelemetry (io.opentelemetry.api.OpenTelemetry)4 SdkLogEmitterProvider (io.opentelemetry.sdk.logs.SdkLogEmitterProvider)4 SdkMeterProvider (io.opentelemetry.sdk.metrics.SdkMeterProvider)4