|
| 1 | +import datadog.trace.agent.test.InstrumentationSpecification |
| 2 | +import datadog.trace.agent.test.asserts.TraceAssert |
| 3 | +import listener.KafkaBatchCoroutineConfig |
| 4 | +import listener.KafkaBatchCoroutineListener |
| 5 | +import datadog.trace.api.DDSpanTypes |
| 6 | +import datadog.trace.bootstrap.instrumentation.api.InstrumentationTags |
| 7 | +import datadog.trace.bootstrap.instrumentation.api.Tags |
| 8 | +import datadog.trace.core.DDSpan |
| 9 | +import org.apache.kafka.clients.producer.ProducerRecord |
| 10 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext |
| 11 | +import org.springframework.kafka.config.KafkaListenerEndpointRegistry |
| 12 | +import org.springframework.kafka.core.KafkaTemplate |
| 13 | +import org.springframework.kafka.test.EmbeddedKafkaBroker |
| 14 | +import org.springframework.kafka.test.utils.ContainerTestUtils |
| 15 | + |
| 16 | +import java.util.concurrent.TimeUnit |
| 17 | + |
| 18 | +class KafkaBatchListenerCoroutineTest extends InstrumentationSpecification { |
| 19 | + |
| 20 | + private static final String TOPIC = "batch-coroutine-topic" |
| 21 | + private static final String CONSUMER_GROUP = "batch-coroutine-group" |
| 22 | + |
| 23 | + def "batch @KafkaListener suspend fun - spans must be in the same trace as kafka.consume"() { |
| 24 | + setup: |
| 25 | + def appContext = new AnnotationConfigApplicationContext(KafkaBatchCoroutineConfig) |
| 26 | + def listener = appContext.getBean(KafkaBatchCoroutineListener) |
| 27 | + def template = appContext.getBean(KafkaTemplate) |
| 28 | + def broker = appContext.getBean(EmbeddedKafkaBroker) |
| 29 | + def registry = appContext.getBean(KafkaListenerEndpointRegistry) |
| 30 | + |
| 31 | + // Wait until listener container has been assigned partitions before sending. |
| 32 | + registry.listenerContainers.each { container -> |
| 33 | + ContainerTestUtils.waitForAssignment(container, broker.partitionsPerTopic) |
| 34 | + } |
| 35 | + |
| 36 | + TEST_WRITER.clear() |
| 37 | + |
| 38 | + when: "two messages are sent before the consumer polls so they arrive in one batch" |
| 39 | + registry.listenerContainers.each { it.stop() } |
| 40 | + template.send(new ProducerRecord(TOPIC, "key", "hello-batch")) |
| 41 | + template.send(new ProducerRecord(TOPIC, "key", "hello-batch")) |
| 42 | + template.flush() |
| 43 | + registry.listenerContainers.each { it.start() } |
| 44 | + |
| 45 | + then: "the listener processes the batch within 15 s" |
| 46 | + listener.latch.await(15, TimeUnit.SECONDS) |
| 47 | + listener.receivedValues == ["hello-batch", "hello-batch"] |
| 48 | + |
| 49 | + and: "child.work is a child of spring.consume" |
| 50 | + DDSpan produce1Span, produce2Span, springConsumeParent |
| 51 | + assertTraces(10, SORT_TRACES_BY_ID) { |
| 52 | + trace(1) { |
| 53 | + produceSpan(it) |
| 54 | + produce1Span = span(0) |
| 55 | + } |
| 56 | + trace(1) { |
| 57 | + produceSpan(it) |
| 58 | + produce2Span = span(0) |
| 59 | + } |
| 60 | + |
| 61 | + trace(1) { kafkaConsumeSpan(it, produce1Span, 0) } |
| 62 | + trace(1) { kafkaConsumeSpan(it, produce2Span, 1) } |
| 63 | + trace(1) { kafkaConsumeSpan(it, produce1Span, 0) } |
| 64 | + trace(1) { kafkaConsumeSpan(it, produce2Span, 1) } |
| 65 | + |
| 66 | + trace(1) { |
| 67 | + // consume messages in one batch |
| 68 | + springConsumeSpan(it) |
| 69 | + springConsumeParent = span(0) |
| 70 | + } |
| 71 | + // child work span connected to the spring consume span |
| 72 | + trace(1) { childWorkSpan(it, springConsumeParent) } |
| 73 | + |
| 74 | + trace(1) { kafkaConsumeSpan(it, produce1Span, 0) } |
| 75 | + trace(1) { kafkaConsumeSpan(it, produce2Span, 1) } |
| 76 | + } |
| 77 | + |
| 78 | + cleanup: |
| 79 | + appContext.close() |
| 80 | + } |
| 81 | + |
| 82 | + private static void produceSpan(TraceAssert trace) { |
| 83 | + trace.span { |
| 84 | + operationName "kafka.produce" |
| 85 | + resourceName "Produce Topic $TOPIC" |
| 86 | + spanType "queue" |
| 87 | + errored false |
| 88 | + measured true |
| 89 | + parent() |
| 90 | + tags { |
| 91 | + "$Tags.COMPONENT" "java-kafka" |
| 92 | + "$Tags.SPAN_KIND" Tags.SPAN_KIND_PRODUCER |
| 93 | + "$InstrumentationTags.MESSAGING_DESTINATION_NAME" TOPIC |
| 94 | + "$InstrumentationTags.KAFKA_BOOTSTRAP_SERVERS" { String } |
| 95 | + peerServiceFrom(InstrumentationTags.KAFKA_BOOTSTRAP_SERVERS) |
| 96 | + defaultTags() |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private static void kafkaConsumeSpan(TraceAssert trace, DDSpan parent, int offset) { |
| 102 | + trace.span { |
| 103 | + operationName "kafka.consume" |
| 104 | + resourceName "Consume Topic $TOPIC" |
| 105 | + spanType "queue" |
| 106 | + errored false |
| 107 | + measured true |
| 108 | + childOf parent |
| 109 | + tags { |
| 110 | + "$Tags.COMPONENT" "java-kafka" |
| 111 | + "$Tags.SPAN_KIND" Tags.SPAN_KIND_CONSUMER |
| 112 | + "$InstrumentationTags.MESSAGING_DESTINATION_NAME" TOPIC |
| 113 | + "$InstrumentationTags.KAFKA_BOOTSTRAP_SERVERS" { String } |
| 114 | + peerServiceFrom(InstrumentationTags.KAFKA_BOOTSTRAP_SERVERS) |
| 115 | + "$InstrumentationTags.CONSUMER_GROUP" CONSUMER_GROUP |
| 116 | + "$InstrumentationTags.OFFSET" offset |
| 117 | + "$InstrumentationTags.PARTITION" { Integer } |
| 118 | + "$InstrumentationTags.RECORD_QUEUE_TIME_MS" { Long } |
| 119 | + "$InstrumentationTags.RECORD_END_TO_END_DURATION_MS" { Long } |
| 120 | + defaultTags(true) |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private static void springConsumeSpan(TraceAssert trace) { |
| 126 | + trace.span { |
| 127 | + operationName "spring.consume" |
| 128 | + resourceName "KafkaBatchCoroutineListener.consume" |
| 129 | + spanType DDSpanTypes.MESSAGE_CONSUMER |
| 130 | + errored false |
| 131 | + measured true |
| 132 | + parent() |
| 133 | + tags { |
| 134 | + "$Tags.COMPONENT" "spring-messaging" |
| 135 | + "$Tags.SPAN_KIND" Tags.SPAN_KIND_CONSUMER |
| 136 | + defaultTags(true) |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private static void childWorkSpan(TraceAssert trace, DDSpan parent) { |
| 142 | + trace.span { |
| 143 | + operationName "child.work" |
| 144 | + childOf parent |
| 145 | + tags { defaultTags() } |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments