Search in sources :

Example 6 with SpanKind

use of io.opentelemetry.api.trace.SpanKind in project opentelemetry-java by open-telemetry.

the class TestClientServerTest method verify.

private void verify() {
    await().atMost(Duration.ofSeconds(15)).until(finishedSpansSize(otelTesting), equalTo(2));
    List<SpanData> finished = otelTesting.getSpans();
    assertThat(finished).hasSize(2);
    assertThat(finished.get(1).getTraceId()).isEqualTo(finished.get(0).getTraceId());
    SpanKind firstSpanKind = finished.get(0).getKind();
    switch(firstSpanKind) {
        case CLIENT:
            assertThat(finished.get(1).getKind()).isEqualTo(SpanKind.SERVER);
            break;
        case SERVER:
            assertThat(finished.get(1).getKind()).isEqualTo(SpanKind.CLIENT);
            break;
        default:
            fail("Unexpected first span kind: " + firstSpanKind);
    }
    assertThat(tracer.scopeManager().activeSpan()).isNull();
}
Also used : SpanData(io.opentelemetry.sdk.trace.data.SpanData) SpanKind(io.opentelemetry.api.trace.SpanKind)

Example 7 with SpanKind

use of io.opentelemetry.api.trace.SpanKind in project opentelemetry-java-instrumentation by open-telemetry.

the class Instrumenter method start.

/**
 * Starts a new operation to be instrumented. The {@code parentContext} is the parent of the
 * resulting instrumented operation and should usually be {@code Context.current()}. The {@code
 * request} is the request object of this operation. The returned {@link Context} should be
 * propagated along with the operation and passed to {@link #end(Context, Object, Object,
 * Throwable)} when it is finished.
 */
public Context start(Context parentContext, REQUEST request) {
    SpanKind spanKind = spanKindExtractor.extract(request);
    SpanBuilder spanBuilder = tracer.spanBuilder(spanNameExtractor.extract(request)).setSpanKind(spanKind).setParent(parentContext);
    Instant startTime = null;
    if (timeExtractor != null) {
        startTime = timeExtractor.extractStartTime(request);
        spanBuilder.setStartTimestamp(startTime);
    }
    SpanLinksBuilder spanLinksBuilder = new SpanLinksBuilderImpl(spanBuilder);
    for (SpanLinksExtractor<? super REQUEST> spanLinksExtractor : spanLinksExtractors) {
        spanLinksExtractor.extract(spanLinksBuilder, parentContext, request);
    }
    UnsafeAttributes attributesBuilder = new UnsafeAttributes();
    for (AttributesExtractor<? super REQUEST, ? super RESPONSE> extractor : attributesExtractors) {
        extractor.onStart(attributesBuilder, parentContext, request);
    }
    Attributes attributes = attributesBuilder;
    Context context = parentContext;
    for (ContextCustomizer<? super REQUEST> contextCustomizer : contextCustomizers) {
        context = contextCustomizer.start(context, request, attributes);
    }
    if (!requestListeners.isEmpty()) {
        long startNanos = getNanos(startTime);
        for (RequestListener requestListener : requestListeners) {
            context = requestListener.start(context, attributes, startNanos);
        }
    }
    spanBuilder.setAllAttributes(attributes);
    Span span = spanBuilder.startSpan();
    context = context.with(span);
    // for them to generate exemplars
    if (!requestMetricListeners.isEmpty()) {
        long startNanos = getNanos(startTime);
        for (RequestListener requestListener : requestMetricListeners) {
            context = requestListener.start(context, attributes, startNanos);
        }
    }
    return spanSuppressionStrategy.storeInContext(context, spanKind, span);
}
Also used : Context(io.opentelemetry.context.Context) SpanBuilder(io.opentelemetry.api.trace.SpanBuilder) Instant(java.time.Instant) Attributes(io.opentelemetry.api.common.Attributes) SpanKind(io.opentelemetry.api.trace.SpanKind) Span(io.opentelemetry.api.trace.Span)

Example 8 with SpanKind

use of io.opentelemetry.api.trace.SpanKind in project opentelemetry-java-instrumentation by open-telemetry.

the class CamelRoutePolicy method spanOnExchangeBegin.

private static Context spanOnExchangeBegin(Route route, Exchange exchange, SpanDecorator sd, Context parentContext) {
    Span activeSpan = Span.fromContext(parentContext);
    if (!activeSpan.getSpanContext().isValid()) {
        parentContext = CamelPropagationUtil.extractParent(exchange.getIn().getHeaders(), route.getEndpoint());
    }
    SpanKind spanKind = spanKind(activeSpan, sd);
    CamelRequest request = CamelRequest.create(sd, exchange, route.getEndpoint(), CamelDirection.INBOUND, spanKind);
    sd.updateServerSpanName(parentContext, exchange, route.getEndpoint(), CamelDirection.INBOUND);
    if (!instrumenter().shouldStart(parentContext, request)) {
        return null;
    }
    Context context = instrumenter().start(parentContext, request);
    ActiveContextManager.activate(context, request);
    return context;
}
Also used : Context(io.opentelemetry.context.Context) Span(io.opentelemetry.api.trace.Span) SpanKind(io.opentelemetry.api.trace.SpanKind)

Aggregations

SpanKind (io.opentelemetry.api.trace.SpanKind)8 Attributes (io.opentelemetry.api.common.Attributes)4 Context (io.opentelemetry.context.Context)4 SpanContext (io.opentelemetry.api.trace.SpanContext)3 Test (org.junit.jupiter.api.Test)3 Span (io.opentelemetry.api.trace.Span)2 SpanBuilder (io.opentelemetry.api.trace.SpanBuilder)2 LinkData (io.opentelemetry.sdk.trace.data.LinkData)2 SpanData (io.opentelemetry.sdk.trace.data.SpanData)2 Sampler (io.opentelemetry.sdk.trace.samplers.Sampler)2 AttributeKey (io.opentelemetry.api.common.AttributeKey)1 TraceState (io.opentelemetry.api.trace.TraceState)1 TracerProvider (io.opentelemetry.api.trace.TracerProvider)1 Resource (io.opentelemetry.sdk.resources.Resource)1 TestClock (io.opentelemetry.sdk.testing.time.TestClock)1 EventData (io.opentelemetry.sdk.trace.data.EventData)1 SamplingDecision (io.opentelemetry.sdk.trace.samplers.SamplingDecision)1 SamplingResult (io.opentelemetry.sdk.trace.samplers.SamplingResult)1 SemanticAttributes (io.opentelemetry.semconv.trace.attributes.SemanticAttributes)1 Instant (java.time.Instant)1