Search in sources :

Example 21 with SdkTracerProvider

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

the class SimpleSpanProcessorTest method tracerSdk_NotSampled_Span.

@Test
void tracerSdk_NotSampled_Span() {
    WaitingSpanExporter waitingSpanExporter = new WaitingSpanExporter(1, CompletableResultCode.ofSuccess());
    SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder().addSpanProcessor(SimpleSpanProcessor.create(waitingSpanExporter)).setSampler(mockSampler).build();
    when(mockSampler.shouldSample(any(), any(), any(), any(), any(), anyList())).thenReturn(SamplingResult.drop());
    try {
        Tracer tracer = sdkTracerProvider.get(getClass().getName());
        tracer.spanBuilder(SPAN_NAME).startSpan();
        tracer.spanBuilder(SPAN_NAME).startSpan();
        when(mockSampler.shouldSample(any(), any(), any(), any(), any(), anyList())).thenReturn(SamplingResult.recordAndSample());
        Span span = tracer.spanBuilder(SPAN_NAME).startSpan();
        span.end();
        // Spans are recorded and exported in the same order as they are ended, we test that a non
        // sampled span is not exported by creating and ending a sampled span after a non sampled span
        // and checking that the first exported span is the sampled span (the non sampled did not get
        // exported).
        List<SpanData> exported = waitingSpanExporter.waitForExport();
        // Need to check this because otherwise the variable span1 is unused, other option is to not
        // have a span1 variable.
        assertThat(exported).containsExactly(((ReadableSpan) span).toSpanData());
    } finally {
        sdkTracerProvider.shutdown();
    }
}
Also used : SpanData(io.opentelemetry.sdk.trace.data.SpanData) Tracer(io.opentelemetry.api.trace.Tracer) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) WaitingSpanExporter(io.opentelemetry.sdk.trace.export.BatchSpanProcessorTest.WaitingSpanExporter) ReadWriteSpan(io.opentelemetry.sdk.trace.ReadWriteSpan) Span(io.opentelemetry.api.trace.Span) ReadableSpan(io.opentelemetry.sdk.trace.ReadableSpan) Test(org.junit.jupiter.api.Test)

Example 22 with SdkTracerProvider

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

the class OpenTelemetrySdkTest method testConfiguration_tracerSettings.

@Test
void testConfiguration_tracerSettings() {
    Resource resource = Resource.create(Attributes.builder().put("cat", "meow").build());
    IdGenerator idGenerator = mock(IdGenerator.class);
    SpanLimits spanLimits = SpanLimits.getDefault();
    OpenTelemetrySdk openTelemetry = OpenTelemetrySdk.builder().setTracerProvider(SdkTracerProvider.builder().setClock(clock).setResource(resource).setIdGenerator(idGenerator).setSpanLimits(spanLimits).build()).build();
    TracerProvider unobfuscatedTracerProvider = ((OpenTelemetrySdk.ObfuscatedTracerProvider) openTelemetry.getTracerProvider()).unobfuscate();
    assertThat(unobfuscatedTracerProvider).isInstanceOfSatisfying(SdkTracerProvider.class, sdkTracerProvider -> assertThat(sdkTracerProvider.getSpanLimits()).isEqualTo(spanLimits));
    // Since TracerProvider is in a different package, the only alternative to this reflective
    // approach would be to make the fields public for testing which is worse than this.
    assertThat(unobfuscatedTracerProvider).extracting("sharedState").hasFieldOrPropertyWithValue("clock", clock).hasFieldOrPropertyWithValue("resource", resource).hasFieldOrPropertyWithValue("idGenerator", idGenerator);
}
Also used : Resource(io.opentelemetry.sdk.resources.Resource) SpanLimits(io.opentelemetry.sdk.trace.SpanLimits) IdGenerator(io.opentelemetry.sdk.trace.IdGenerator) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) TracerProvider(io.opentelemetry.api.trace.TracerProvider) Test(org.junit.jupiter.api.Test)

Example 23 with SdkTracerProvider

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

the class OtlpExporterIntegrationTest method testTraceExport.

private static void testTraceExport(SpanExporter spanExporter) {
    SdkTracerProvider tracerProvider = SdkTracerProvider.builder().addSpanProcessor(SimpleSpanProcessor.create(spanExporter)).setResource(RESOURCE).build();
    SpanContext linkContext = SpanContext.create(IdGenerator.random().generateTraceId(), IdGenerator.random().generateSpanId(), TraceFlags.getDefault(), TraceState.getDefault());
    Span span = tracerProvider.get(OtlpExporterIntegrationTest.class.getName()).spanBuilder("my span name").addLink(linkContext).startSpan();
    span.setAttribute("key", "value");
    span.addEvent("event");
    span.end();
    await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> assertThat(grpcServer.traceRequests).hasSize(1));
    ExportTraceServiceRequest request = grpcServer.traceRequests.get(0);
    assertThat(request.getResourceSpansCount()).isEqualTo(1);
    ResourceSpans resourceSpans = request.getResourceSpans(0);
    assertThat(resourceSpans.getResource().getAttributesList()).contains(KeyValue.newBuilder().setKey(ResourceAttributes.SERVICE_NAME.getKey()).setValue(AnyValue.newBuilder().setStringValue("integration test").build()).build());
    assertThat(resourceSpans.getInstrumentationLibrarySpansCount()).isEqualTo(1);
    InstrumentationLibrarySpans ilSpans = resourceSpans.getInstrumentationLibrarySpans(0);
    assertThat(ilSpans.getInstrumentationLibrary().getName()).isEqualTo(OtlpExporterIntegrationTest.class.getName());
    assertThat(ilSpans.getSpansCount()).isEqualTo(1);
    io.opentelemetry.proto.trace.v1.Span protoSpan = ilSpans.getSpans(0);
    assertThat(protoSpan.getTraceId().toByteArray()).isEqualTo(span.getSpanContext().getTraceIdBytes());
    assertThat(protoSpan.getSpanId().toByteArray()).isEqualTo(span.getSpanContext().getSpanIdBytes());
    assertThat(protoSpan.getName()).isEqualTo("my span name");
    assertThat(protoSpan.getAttributesList()).isEqualTo(Collections.singletonList(KeyValue.newBuilder().setKey("key").setValue(AnyValue.newBuilder().setStringValue("value").build()).build()));
    assertThat(protoSpan.getEventsCount()).isEqualTo(1);
    assertThat(protoSpan.getEvents(0).getName()).isEqualTo("event");
    assertThat(protoSpan.getLinksCount()).isEqualTo(1);
    Link link = protoSpan.getLinks(0);
    assertThat(link.getTraceId().toByteArray()).isEqualTo(linkContext.getTraceIdBytes());
    assertThat(link.getSpanId().toByteArray()).isEqualTo(linkContext.getSpanIdBytes());
}
Also used : ExportTraceServiceRequest(io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest) SpanContext(io.opentelemetry.api.trace.SpanContext) InstrumentationLibrarySpans(io.opentelemetry.proto.trace.v1.InstrumentationLibrarySpans) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) ResourceSpans(io.opentelemetry.proto.trace.v1.ResourceSpans) Span(io.opentelemetry.api.trace.Span) Link(io.opentelemetry.proto.trace.v1.Span.Link)

Example 24 with SdkTracerProvider

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

the class OpenTelemetryRule method create.

/**
 * Returns a {@link OpenTelemetryRule} with a default SDK initialized with an in-memory span
 * exporter and W3C trace context propagation.
 */
public static OpenTelemetryRule create() {
    InMemorySpanExporter spanExporter = InMemorySpanExporter.create();
    SdkTracerProvider tracerProvider = SdkTracerProvider.builder().addSpanProcessor(SimpleSpanProcessor.create(spanExporter)).build();
    OpenTelemetrySdk openTelemetry = OpenTelemetrySdk.builder().setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance())).setTracerProvider(tracerProvider).build();
    return new OpenTelemetryRule(openTelemetry, spanExporter);
}
Also used : InMemorySpanExporter(io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) OpenTelemetrySdk(io.opentelemetry.sdk.OpenTelemetrySdk)

Example 25 with SdkTracerProvider

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

the class OpenTelemetryExtension method create.

/**
 * Returns a {@link OpenTelemetryExtension} with a default SDK initialized with an in-memory span
 * exporter and W3C trace context propagation.
 */
public static OpenTelemetryExtension create() {
    InMemorySpanExporter spanExporter = InMemorySpanExporter.create();
    SdkTracerProvider tracerProvider = SdkTracerProvider.builder().addSpanProcessor(SimpleSpanProcessor.create(spanExporter)).build();
    OpenTelemetrySdk openTelemetry = OpenTelemetrySdk.builder().setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance())).setTracerProvider(tracerProvider).build();
    return new OpenTelemetryExtension(openTelemetry, spanExporter);
}
Also used : InMemorySpanExporter(io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) OpenTelemetrySdk(io.opentelemetry.sdk.OpenTelemetrySdk)

Aggregations

SdkTracerProvider (io.opentelemetry.sdk.trace.SdkTracerProvider)37 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