Search in sources :

Example 16 with TraceContextOrSamplingFlags

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

the class TracingChannelInterceptor method preSend.

/**
 * Starts and propagates {@link Span.Kind#PRODUCER} span for each message sent.
 */
@Override
public Message<?> preSend(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.PRODUCER).name("send").start();
        span.remoteEndpoint(Endpoint.newBuilder().serviceName(REMOTE_SERVICE_NAME).build());
        addTags(message, span, channel);
    }
    if (log.isDebugEnabled()) {
        log.debug("Created a new span in pre send" + span);
    }
    headers.setImmutable();
    Message<?> outputMessage = new GenericMessage<>(message.getPayload(), headers.getMessageHeaders());
    if (isDirectChannel(channel)) {
        beforeHandle(outputMessage, channel, null);
    }
    return outputMessage;
}
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)

Example 17 with TraceContextOrSamplingFlags

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

the class TracingChannelInterceptor method nextSpan.

/**
 * Use this to create a span for processing the given message. Note: the result has no name and is
 * not started.
 * <p>
 * <p>This creates a child from identifiers extracted from the message headers, or a new span if
 * one couldn't be extracted.
 */
public Span nextSpan(Message<?> message) {
    MessageHeaderAccessor headers = mutableHeaderAccessor(message);
    TraceContextOrSamplingFlags extracted = this.extractor.extract(headers);
    headers.setImmutable();
    Span result = this.tracer.nextSpan(extracted);
    if (extracted.context() == null && !result.isNoop()) {
        addTags(message, result, null);
    }
    if (log.isDebugEnabled()) {
        log.debug("Created a new span " + result);
    }
    return result;
}
Also used : MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags) Span(brave.Span) ThreadLocalSpan(brave.propagation.ThreadLocalSpan)

Example 18 with TraceContextOrSamplingFlags

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

the class TracingServerInterceptor method interceptCall.

@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(final ServerCall<ReqT, RespT> call, final Metadata headers, final ServerCallHandler<ReqT, RespT> next) {
    TraceContextOrSamplingFlags extracted = extractor.extract(headers);
    Span span = extracted.context() != null ? tracer.joinSpan(extracted.context()) : tracer.nextSpan(extracted);
    span.kind(Span.Kind.SERVER);
    parser.onStart(call, headers, span.customizer());
    // startCall invokes user interceptors, so we place the span in scope here
    ServerCall.Listener<ReqT> result;
    try (Tracer.SpanInScope ws = tracer.withSpanInScope(span)) {
        result = next.startCall(new TracingServerCall<>(span, call, parser), headers);
    } catch (RuntimeException | Error e) {
        parser.onError(e, span.customizer());
        span.finish();
        throw e;
    }
    // This ensures the server implementation can see the span in scope
    return new ScopingServerCallListener<>(tracer, span, result, parser);
}
Also used : SimpleForwardingServerCall(io.grpc.ForwardingServerCall.SimpleForwardingServerCall) ServerCall(io.grpc.ServerCall) Tracer(brave.Tracer) TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags) Span(brave.Span)

Example 19 with TraceContextOrSamplingFlags

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

the class KafkaTracing method joinSpan.

/**
 * Retrieve the span extracted from the record headers. Creates a root span if the context is not
 * available.
 *
 * @deprecated this results in appending to a span already complete. Please use {@link
 * #nextSpan(ConsumerRecord)}
 */
@Deprecated
public Span joinSpan(ConsumerRecord<?, ?> record) {
    TraceContextOrSamplingFlags extracted = extractAndClearHeaders(record);
    if (extracted.context() != null) {
        // avoid creating an unnecessary child
        return tracing.tracer().toSpan(extracted.context());
    }
    Span result = tracing.tracer().nextSpan(extracted);
    if (!result.isNoop())
        addTags(record, result.customizer());
    return result;
}
Also used : TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags) Span(brave.Span)

Example 20 with TraceContextOrSamplingFlags

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

the class TracingRabbitListenerAdvice method extractTraceContextAndRemoveHeaders.

TraceContextOrSamplingFlags extractTraceContextAndRemoveHeaders(Message message) {
    MessageProperties messageProperties = message.getMessageProperties();
    TraceContextOrSamplingFlags extracted = extractor.extract(messageProperties);
    Map<String, Object> headers = messageProperties.getHeaders();
    for (String key : tracing.propagation().keys()) {
        headers.remove(key);
    }
    return extracted;
}
Also used : MessageProperties(org.springframework.amqp.core.MessageProperties) TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags)

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