Search in sources :

Example 21 with CONSUMER

use of zipkin2.Span.Kind.CONSUMER in project brave by openzipkin.

the class ITKafkaTracing method poll_creates_one_consumer_span_per_extracted_context.

@Test
public void poll_creates_one_consumer_span_per_extracted_context() throws Exception {
    String topic1 = testName.getMethodName() + "1";
    String topic2 = testName.getMethodName() + "2";
    producer = createTracingProducer();
    consumer = createTracingConsumer(topic1, topic2);
    producer.send(new ProducerRecord<>(topic1, TEST_KEY, TEST_VALUE)).get();
    producer.send(new ProducerRecord<>(topic2, TEST_KEY, TEST_VALUE)).get();
    ConsumerRecords<String, String> records = consumer.poll(10000);
    assertThat(records).hasSize(2);
    Span producerSpan1 = producerSpans.take(), producerSpan2 = producerSpans.take();
    Span consumerSpan1 = consumerSpans.take(), consumerSpan2 = consumerSpans.take();
    // Check to see the trace is continued between the producer and the consumer
    // we don't know the order the spans will come in. Correlate with the tag instead.
    String firstTopic = producerSpan1.tags().get(KAFKA_TOPIC_TAG);
    if (firstTopic.equals(consumerSpan1.tags().get(KAFKA_TOPIC_TAG))) {
        assertThat(producerSpan1.traceId()).isEqualTo(consumerSpan1.traceId());
        assertThat(producerSpan2.traceId()).isEqualTo(consumerSpan2.traceId());
    } else {
        assertThat(producerSpan1.traceId()).isEqualTo(consumerSpan2.traceId());
        assertThat(producerSpan2.traceId()).isEqualTo(consumerSpan1.traceId());
    }
}
Also used : ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Span(zipkin2.Span) Test(org.junit.Test)

Example 22 with CONSUMER

use of zipkin2.Span.Kind.CONSUMER in project brave by openzipkin.

the class TracingConsumerTest method should_create_only_one_consumer_span_per_topic.

@Test
public void should_create_only_one_consumer_span_per_topic() {
    Map<TopicPartition, Long> offsets = new HashMap<>();
    // 2 partitions in the same topic
    offsets.put(new TopicPartition(TEST_TOPIC, 0), 0L);
    offsets.put(new TopicPartition(TEST_TOPIC, 1), 0L);
    consumer.updateBeginningOffsets(offsets);
    consumer.assign(offsets.keySet());
    // create 500 messages
    for (int i = 0; i < 250; i++) {
        consumer.addRecord(new ConsumerRecord<>(TEST_TOPIC, 0, i, TEST_KEY, TEST_VALUE));
        consumer.addRecord(new ConsumerRecord<>(TEST_TOPIC, 1, i, TEST_KEY, TEST_VALUE));
    }
    Consumer<String, String> tracingConsumer = kafkaTracing.consumer(consumer);
    tracingConsumer.poll(10);
    // only one consumer span reported
    assertThat(spans).hasSize(1).flatExtracting(s -> s.tags().entrySet()).containsOnly(entry("kafka.topic", "myTopic"));
}
Also used : Consumer(org.apache.kafka.clients.consumer.Consumer) TopicPartition(org.apache.kafka.common.TopicPartition) MockConsumer(org.apache.kafka.clients.consumer.MockConsumer) Span(zipkin2.Span) HashMap(java.util.HashMap) Test(org.junit.Test) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) OffsetResetStrategy(org.apache.kafka.clients.consumer.OffsetResetStrategy) Assertions.entry(org.assertj.core.api.Assertions.entry) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Map(java.util.Map) AssertionsForInterfaceTypes.assertThat(org.assertj.core.api.AssertionsForInterfaceTypes.assertThat) Before(org.junit.Before) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) Test(org.junit.Test)

Example 23 with CONSUMER

use of zipkin2.Span.Kind.CONSUMER in project zipkin by openzipkin.

the class CassandraSpanConsumerTest method searchDisabled_doesntIndex.

@Test
public void searchDisabled_doesntIndex() {
    consumer = spanConsumer(CassandraStorage.newBuilder().searchEnabled(false));
    Span span = spanWithoutAnnotationsOrTags.toBuilder().addAnnotation(TODAY * 1000L, "annotation").putTag("foo", "bar").duration(10000L).build();
    assertThat(consumer.accept(singletonList(span))).extracting("input.annotation_query").satisfies(q -> assertThat(q).isNull());
}
Also used : Span(zipkin2.Span) Test(org.junit.Test)

Example 24 with CONSUMER

use of zipkin2.Span.Kind.CONSUMER in project zipkin by openzipkin.

the class DependencyLinker method putTrace.

/**
 * All {@code spans} must have the same trace id.
 */
public DependencyLinker putTrace(List<Span> spans) {
    if (spans.isEmpty())
        return this;
    SpanNode traceTree = builder.build(spans);
    if (logger.isLoggable(FINE))
        logger.fine("traversing trace tree, breadth-first");
    for (Iterator<SpanNode> i = traceTree.traverse(); i.hasNext(); ) {
        SpanNode current = i.next();
        Span currentSpan = current.span();
        if (logger.isLoggable(FINE)) {
            logger.fine("processing " + currentSpan);
        }
        Kind kind = currentSpan.kind();
        // spans, we proceed to use the name the client chose.
        if (Kind.CLIENT.equals(kind) && !current.children().isEmpty()) {
            continue;
        }
        String serviceName = currentSpan.localServiceName();
        String remoteServiceName = currentSpan.remoteServiceName();
        if (kind == null) {
            // Treat unknown type of span as a client span if we know both sides
            if (serviceName != null && remoteServiceName != null) {
                kind = Kind.CLIENT;
            } else {
                logger.fine("non remote span; skipping");
                continue;
            }
        }
        String child;
        String parent;
        switch(kind) {
            case SERVER:
            case CONSUMER:
                child = serviceName;
                parent = remoteServiceName;
                if (current == traceTree) {
                    // we are the root-most span.
                    if (parent == null) {
                        logger.fine("root's client is unknown; skipping");
                        continue;
                    }
                }
                break;
            case CLIENT:
            case PRODUCER:
                parent = serviceName;
                child = remoteServiceName;
                break;
            default:
                logger.fine("unknown kind; skipping");
                continue;
        }
        boolean isError = currentSpan.tags().containsKey("error");
        if (kind == Kind.PRODUCER || kind == Kind.CONSUMER) {
            if (parent == null || child == null) {
                logger.fine("cannot link messaging span to its broker; skipping");
            } else {
                addLink(parent, child, isError);
            }
            continue;
        }
        // Local spans may be between the current node and its remote parent
        Span remoteAncestor = firstRemoteAncestor(current);
        String remoteAncestorName;
        if (remoteAncestor != null && (remoteAncestorName = remoteAncestor.localServiceName()) != null) {
            // Check for this and backfill a link from the nearest remote to that service as necessary.
            if (kind == Kind.CLIENT && serviceName != null && !remoteAncestorName.equals(serviceName)) {
                logger.fine("detected missing link to client span");
                // we don't know if there's an error here
                addLink(remoteAncestorName, serviceName, false);
            }
            if (kind == Kind.SERVER || parent == null)
                parent = remoteAncestorName;
            // client, we need to check it for errors.
            if (!isError && Kind.CLIENT.equals(remoteAncestor.kind()) && currentSpan.parentId() != null && currentSpan.parentId().equals(remoteAncestor.id())) {
                isError = remoteAncestor.tags().containsKey("error");
            }
        }
        if (parent == null || child == null) {
            logger.fine("cannot find remote ancestor; skipping");
            continue;
        }
        addLink(parent, child, isError);
    }
    return this;
}
Also used : Kind(zipkin2.Span.Kind) Span(zipkin2.Span)

Example 25 with CONSUMER

use of zipkin2.Span.Kind.CONSUMER in project zipkin by openzipkin.

the class SpanConverterTest method producerAndConsumer_loopback_shared.

/**
 * shared v1 IDs for messaging spans isn't supported, but shouldn't break
 */
@Test
public void producerAndConsumer_loopback_shared() {
    V1Span v1 = V1Span.newBuilder().traceId(1).parentId(2).id(3).name("message").addAnnotation(1472470996199000L, "ms", FRONTEND).addAnnotation(1472470996238000L, "ws", FRONTEND).addAnnotation(1472470996403000L, "wr", FRONTEND).addAnnotation(1472470996406000L, "mr", FRONTEND).build();
    Span.Builder newBuilder = Span.newBuilder().traceId("1").parentId("2").id("3").name("message");
    Span producer = newBuilder.clone().kind(Kind.PRODUCER).localEndpoint(FRONTEND).timestamp(1472470996199000L).duration(1472470996238000L - 1472470996199000L).build();
    Span consumer = newBuilder.clone().kind(Kind.CONSUMER).shared(true).localEndpoint(FRONTEND).timestamp(1472470996403000L).duration(1472470996406000L - 1472470996403000L).build();
    assertThat(v1SpanConverter.convert(v1)).containsExactly(producer, consumer);
}
Also used : Span(zipkin2.Span) Test(org.junit.Test)

Aggregations

Span (zipkin2.Span)17 Test (org.junit.Test)13 Test (org.junit.jupiter.api.Test)7 LogEntry (zipkin2.collector.scribe.generated.LogEntry)6 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)4 Call (zipkin2.Call)4 Endpoint (zipkin2.Endpoint)4 StorageComponent (zipkin2.storage.StorageComponent)4 List (java.util.List)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 TopicPartition (org.apache.kafka.common.TopicPartition)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Before (org.junit.Before)3 Tracing (brave.Tracing)2 Sampler (brave.sampler.Sampler)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 TimeUnit (java.util.concurrent.TimeUnit)2 MethodInvocation (org.aopalliance.intercept.MethodInvocation)2 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)2