Search in sources :

Example 6 with TraceContextOrSamplingFlags

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

the class ITTracingFilter_Consumer method propagates_sampledFalse.

/**
 * Unlike Brave 3, Brave 4 propagates trace ids even when unsampled
 */
@Test
public void propagates_sampledFalse() throws Exception {
    setTracing(tracingBuilder(Sampler.NEVER_SAMPLE).build());
    client.get().sayHello("jorge");
    TraceContextOrSamplingFlags extracted = server.takeRequest();
    assertThat(extracted.sampled()).isFalse();
// @After will check that nothing is reported
}
Also used : TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags) Test(org.junit.Test)

Example 7 with TraceContextOrSamplingFlags

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

the class ITTracingClientInterceptor method propagates_sampledFalse.

/**
 * Unlike Brave 3, Brave 4 propagates trace ids even when unsampled
 */
@Test
public void propagates_sampledFalse() throws Exception {
    tracing = GrpcTracing.create(tracingBuilder(Sampler.NEVER_SAMPLE).build());
    closeClient(client);
    client = newClient();
    GreeterGrpc.newBlockingStub(client).sayHello(HELLO_REQUEST);
    TraceContextOrSamplingFlags extracted = server.takeRequest();
    assertThat(extracted.sampled()).isFalse();
// @After will check that nothing is reported
}
Also used : TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags) Test(org.junit.Test)

Example 8 with TraceContextOrSamplingFlags

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

the class KafkaTracing method nextSpan.

/**
 * Use this to create a span for processing the given record. Note: the result has no name and is
 * not started.
 *
 * <p>This creates a child from identifiers extracted from the record headers, or a new span if
 * one couldn't be extracted.
 */
public Span nextSpan(ConsumerRecord<?, ?> record) {
    TraceContextOrSamplingFlags extracted = extractAndClearHeaders(record);
    Span result = tracing.tracer().nextSpan(extracted);
    if (extracted.context() == null && !result.isNoop()) {
        addTags(record, result);
    }
    return result;
}
Also used : TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags) Span(brave.Span)

Example 9 with TraceContextOrSamplingFlags

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

the class TracingConsumer method poll.

/**
 * This
 */
@Override
public ConsumerRecords<K, V> poll(long timeout) {
    ConsumerRecords<K, V> records = delegate.poll(timeout);
    if (records.isEmpty() || tracing.isNoop())
        return records;
    Map<String, Span> consumerSpansForTopic = new LinkedHashMap<>();
    for (TopicPartition partition : records.partitions()) {
        String topic = partition.topic();
        List<ConsumerRecord<K, V>> recordsInPartition = records.records(partition);
        for (int i = 0, length = recordsInPartition.size(); i < length; i++) {
            ConsumerRecord<K, V> record = recordsInPartition.get(i);
            TraceContextOrSamplingFlags extracted = extractor.extract(record.headers());
            // make or reuse a span for this topic
            if (extracted.samplingFlags() != null && extracted.extra().isEmpty()) {
                Span consumerSpanForTopic = consumerSpansForTopic.get(topic);
                if (consumerSpanForTopic == null) {
                    consumerSpansForTopic.put(topic, consumerSpanForTopic = tracing.tracer().nextSpan(extracted).name("poll").kind(Span.Kind.CONSUMER).tag(KafkaTags.KAFKA_TOPIC_TAG, topic).start());
                }
                // no need to remove propagation headers as we failed to extract anything
                injector.inject(consumerSpanForTopic.context(), record.headers());
            } else {
                // we extracted request-scoped data, so cannot share a consumer span.
                Span span = tracing.tracer().nextSpan(extracted);
                if (!span.isNoop()) {
                    span.name("poll").kind(Span.Kind.CONSUMER).tag(KafkaTags.KAFKA_TOPIC_TAG, topic);
                    if (remoteServiceName != null) {
                        span.remoteEndpoint(Endpoint.newBuilder().serviceName(remoteServiceName).build());
                    }
                    // span won't be shared by other records
                    span.start().finish();
                }
                // remove prior propagation headers from the record
                tracing.propagation().keys().forEach(key -> record.headers().remove(key));
                injector.inject(span.context(), record.headers());
            }
        }
    }
    consumerSpansForTopic.values().forEach(span -> {
        if (remoteServiceName != null) {
            span.remoteEndpoint(Endpoint.newBuilder().serviceName(remoteServiceName).build());
        }
        span.finish();
    });
    return records;
}
Also used : Span(brave.Span) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Endpoint(zipkin2.Endpoint) LinkedHashMap(java.util.LinkedHashMap) TopicPartition(org.apache.kafka.common.TopicPartition) TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags)

Example 10 with TraceContextOrSamplingFlags

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

the class TracerTest method nextSpan_extractedExtra_appendsToChildOfCurrent.

@Test
public void nextSpan_extractedExtra_appendsToChildOfCurrent() {
    // current parent already has extra stuff
    Span parent = tracer.toSpan(tracer.newTrace().context().toBuilder().extra(asList(1L)).build());
    TraceContextOrSamplingFlags extracted = TraceContextOrSamplingFlags.create(SamplingFlags.EMPTY).toBuilder().addExtra(2L).build();
    try (Tracer.SpanInScope ws = tracer.withSpanInScope(parent)) {
        assertThat(tracer.nextSpan(extracted).context().extra()).containsExactly(1L, 2L);
    }
}
Also used : TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags) Test(org.junit.Test)

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