Search in sources :

Example 11 with TraceContextOrSamplingFlags

use of brave.propagation.TraceContextOrSamplingFlags in project brave by openzipkin.

the class TracerTest method nextSpan_ensuresSampling_whenCreatingNewChild.

@Test
public void nextSpan_ensuresSampling_whenCreatingNewChild() {
    TraceContext notYetSampled = tracer.newTrace().context().toBuilder().sampled(null).build();
    TraceContextOrSamplingFlags extracted = TraceContextOrSamplingFlags.create(notYetSampled);
    assertThat(tracer.nextSpan(extracted).context().sampled()).isTrue();
}
Also used : StrictCurrentTraceContext(brave.propagation.StrictCurrentTraceContext) TraceContext(brave.propagation.TraceContext) TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags) Test(org.junit.Test)

Example 12 with TraceContextOrSamplingFlags

use of brave.propagation.TraceContextOrSamplingFlags in project brave by openzipkin.

the class TracerTest method nextSpan_extractedTraceContext.

@Test
public void nextSpan_extractedTraceContext() {
    TraceContext traceContext = TraceContext.newBuilder().traceId(1L).spanId(2L).build();
    TraceContextOrSamplingFlags extracted = TraceContextOrSamplingFlags.create(traceContext);
    assertThat(tracer.nextSpan(extracted).context()).extracting(TraceContext::traceId, TraceContext::parentId).containsExactly(1L, 2L);
}
Also used : StrictCurrentTraceContext(brave.propagation.StrictCurrentTraceContext) TraceContext(brave.propagation.TraceContext) TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags) Test(org.junit.Test)

Example 13 with TraceContextOrSamplingFlags

use of brave.propagation.TraceContextOrSamplingFlags in project brave by openzipkin.

the class TracerTest method nextSpan_extractedExtra_newTrace.

@Test
public void nextSpan_extractedExtra_newTrace() {
    TraceContextOrSamplingFlags extracted = TraceContextOrSamplingFlags.create(SamplingFlags.EMPTY).toBuilder().addExtra(1L).build();
    assertThat(tracer.nextSpan(extracted).context().extra()).containsExactly(1L);
}
Also used : TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags) Test(org.junit.Test)

Example 14 with TraceContextOrSamplingFlags

use of brave.propagation.TraceContextOrSamplingFlags in project spring-cloud-sleuth by spring-cloud.

the class TracingChannelInterceptor method beforeHandle.

/**
 * This starts a consumer span as a child of the incoming message or the current trace context.
 * It then creates a span for the handler, placing it in scope.
 */
@Override
public Message<?> beforeHandle(Message<?> message, MessageChannel channel, MessageHandler handler) {
    if (emptyMessage(message)) {
        return message;
    }
    MessageHeaderAccessor headers = mutableHeaderAccessor(message);
    TraceContextOrSamplingFlags extracted = this.extractor.extract(headers);
    // Start and finish a consumer span as we will immediately process it.
    Span consumerSpan = this.tracer.nextSpan(extracted);
    if (!consumerSpan.isNoop()) {
        consumerSpan.kind(Span.Kind.CONSUMER).start();
        consumerSpan.remoteEndpoint(Endpoint.newBuilder().serviceName(REMOTE_SERVICE_NAME).build());
        addTags(message, consumerSpan, channel);
        consumerSpan.finish();
    }
    // create and scope a span for the message processor
    this.threadLocalSpan.next(TraceContextOrSamplingFlags.create(consumerSpan.context())).name("handle").start();
    // remove any trace headers, but don't re-inject as we are synchronously processing the
    // message and can rely on scoping to access this span later.
    MessageHeaderPropagation.removeAnyTraceHeaders(headers, this.tracing.propagation().keys());
    if (log.isDebugEnabled()) {
        log.debug("Created a new span in before handle" + consumerSpan);
    }
    if (message instanceof ErrorMessage) {
        return new ErrorMessage((Throwable) message.getPayload(), headers.getMessageHeaders());
    }
    headers.setImmutable();
    return new GenericMessage<>(message.getPayload(), headers.getMessageHeaders());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) ErrorMessage(org.springframework.messaging.support.ErrorMessage) TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags) Span(brave.Span) ThreadLocalSpan(brave.propagation.ThreadLocalSpan)

Example 15 with TraceContextOrSamplingFlags

use of brave.propagation.TraceContextOrSamplingFlags in project spring-cloud-sleuth by spring-cloud.

the class TracingChannelInterceptor method postReceive.

/**
 * This starts a consumer span as a child of the incoming message or the current trace context,
 * placing it in scope until the receive completes.
 */
@Override
public Message<?> postReceive(Message<?> message, MessageChannel channel) {
    if (emptyMessage(message)) {
        return message;
    }
    MessageHeaderAccessor headers = mutableHeaderAccessor(message);
    TraceContextOrSamplingFlags extracted = this.extractor.extract(headers);
    Span span = this.threadLocalSpan.next(extracted);
    MessageHeaderPropagation.removeAnyTraceHeaders(headers, this.tracing.propagation().keys());
    this.injector.inject(span.context(), headers);
    if (!span.isNoop()) {
        span.kind(Span.Kind.CONSUMER).name("receive").start();
        span.remoteEndpoint(Endpoint.newBuilder().serviceName(REMOTE_SERVICE_NAME).build());
        addTags(message, span, channel);
    }
    if (log.isDebugEnabled()) {
        log.debug("Created a new span in post receive " + span);
    }
    headers.setImmutable();
    return new GenericMessage<>(message.getPayload(), headers.getMessageHeaders());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags) Span(brave.Span) ThreadLocalSpan(brave.propagation.ThreadLocalSpan)

Aggregations

TraceContextOrSamplingFlags (brave.propagation.TraceContextOrSamplingFlags)27 Test (org.junit.Test)13 Span (brave.Span)10 ThreadLocalSpan (brave.propagation.ThreadLocalSpan)4 TraceContext (brave.propagation.TraceContext)4 MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)4 StrictCurrentTraceContext (brave.propagation.StrictCurrentTraceContext)3 GenericMessage (org.springframework.messaging.support.GenericMessage)3 Tracer (brave.Tracer)2 TraceIdContext (brave.propagation.TraceIdContext)2 LinkedHashMap (java.util.LinkedHashMap)2 Endpoint (zipkin2.Endpoint)2 Kind (brave.Span.Kind)1 SpanInScope (brave.Tracer.SpanInScope)1 Result (com.alibaba.dubbo.rpc.Result)1 RpcContext (com.alibaba.dubbo.rpc.RpcContext)1 FutureAdapter (com.alibaba.dubbo.rpc.protocol.dubbo.FutureAdapter)1 SimpleForwardingServerCall (io.grpc.ForwardingServerCall.SimpleForwardingServerCall)1 ServerCall (io.grpc.ServerCall)1 TextMap (io.opentracing.propagation.TextMap)1