|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import io.opentelemetry.semconv.trace.attributes.SemanticAttributes |
| 7 | +import org.apache.kafka.clients.producer.ProducerRecord |
| 8 | +import org.apache.kafka.common.TopicPartition |
| 9 | + |
| 10 | +import java.time.Duration |
| 11 | + |
| 12 | +import static io.opentelemetry.api.trace.SpanKind.CONSUMER |
| 13 | +import static io.opentelemetry.api.trace.SpanKind.INTERNAL |
| 14 | +import static io.opentelemetry.api.trace.SpanKind.PRODUCER |
| 15 | + |
| 16 | +class KafkaClientSuppressReceiveSpansTest extends KafkaClientBaseTest { |
| 17 | + |
| 18 | + def "test kafka produce and consume"() { |
| 19 | + when: |
| 20 | + String greeting = "Hello Kafka!" |
| 21 | + runWithSpan("parent") { |
| 22 | + producer.send(new ProducerRecord(SHARED_TOPIC, greeting)) { meta, ex -> |
| 23 | + if (ex == null) { |
| 24 | + runWithSpan("producer callback") {} |
| 25 | + } else { |
| 26 | + runWithSpan("producer exception: " + ex) {} |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + then: |
| 32 | + // check that the message was received |
| 33 | + def records = consumer.poll(Duration.ofSeconds(5).toMillis()) |
| 34 | + for (record in records) { |
| 35 | + runWithSpan("processing") { |
| 36 | + assert record.value() == greeting |
| 37 | + assert record.key() == null |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + assertTraces(1) { |
| 42 | + trace(0, 5) { |
| 43 | + span(0) { |
| 44 | + name "parent" |
| 45 | + kind INTERNAL |
| 46 | + hasNoParent() |
| 47 | + } |
| 48 | + span(1) { |
| 49 | + name SHARED_TOPIC + " send" |
| 50 | + kind PRODUCER |
| 51 | + childOf span(0) |
| 52 | + attributes { |
| 53 | + "${SemanticAttributes.MESSAGING_SYSTEM.key}" "kafka" |
| 54 | + "${SemanticAttributes.MESSAGING_DESTINATION.key}" SHARED_TOPIC |
| 55 | + "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" |
| 56 | + } |
| 57 | + } |
| 58 | + span(2) { |
| 59 | + name SHARED_TOPIC + " process" |
| 60 | + kind CONSUMER |
| 61 | + childOf span(1) |
| 62 | + attributes { |
| 63 | + "${SemanticAttributes.MESSAGING_SYSTEM.key}" "kafka" |
| 64 | + "${SemanticAttributes.MESSAGING_DESTINATION.key}" SHARED_TOPIC |
| 65 | + "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" |
| 66 | + "${SemanticAttributes.MESSAGING_OPERATION.key}" "process" |
| 67 | + "${SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES.key}" Long |
| 68 | + "${SemanticAttributes.MESSAGING_KAFKA_PARTITION.key}" { it >= 0 } |
| 69 | + "kafka.offset" Long |
| 70 | + "kafka.record.queue_time_ms" { it >= 0 } |
| 71 | + } |
| 72 | + } |
| 73 | + span(3) { |
| 74 | + name "processing" |
| 75 | + childOf span(2) |
| 76 | + } |
| 77 | + span(4) { |
| 78 | + name "producer callback" |
| 79 | + kind INTERNAL |
| 80 | + childOf span(0) |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + def "test pass through tombstone"() { |
| 87 | + when: |
| 88 | + producer.send(new ProducerRecord<>(SHARED_TOPIC, null)) |
| 89 | + |
| 90 | + then: |
| 91 | + // check that the message was received |
| 92 | + def records = consumer.poll(Duration.ofSeconds(5).toMillis()) |
| 93 | + for (record in records) { |
| 94 | + assert record.value() == null |
| 95 | + assert record.key() == null |
| 96 | + } |
| 97 | + |
| 98 | + assertTraces(1) { |
| 99 | + trace(0, 2) { |
| 100 | + span(0) { |
| 101 | + name SHARED_TOPIC + " send" |
| 102 | + kind PRODUCER |
| 103 | + hasNoParent() |
| 104 | + attributes { |
| 105 | + "${SemanticAttributes.MESSAGING_SYSTEM.key}" "kafka" |
| 106 | + "${SemanticAttributes.MESSAGING_DESTINATION.key}" SHARED_TOPIC |
| 107 | + "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" |
| 108 | + "${SemanticAttributes.MESSAGING_KAFKA_TOMBSTONE.key}" true |
| 109 | + } |
| 110 | + } |
| 111 | + span(1) { |
| 112 | + name SHARED_TOPIC + " process" |
| 113 | + kind CONSUMER |
| 114 | + childOf span(0) |
| 115 | + attributes { |
| 116 | + "${SemanticAttributes.MESSAGING_SYSTEM.key}" "kafka" |
| 117 | + "${SemanticAttributes.MESSAGING_DESTINATION.key}" SHARED_TOPIC |
| 118 | + "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" |
| 119 | + "${SemanticAttributes.MESSAGING_OPERATION.key}" "process" |
| 120 | + "${SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES.key}" Long |
| 121 | + "${SemanticAttributes.MESSAGING_KAFKA_PARTITION.key}" { it >= 0 } |
| 122 | + "${SemanticAttributes.MESSAGING_KAFKA_TOMBSTONE.key}" true |
| 123 | + "kafka.offset" Long |
| 124 | + "kafka.record.queue_time_ms" { it >= 0 } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + def "test records(TopicPartition) kafka consume"() { |
| 132 | + setup: |
| 133 | + def partition = 0 |
| 134 | +
|
| 135 | + when: "send message" |
| 136 | + def greeting = "Hello from MockConsumer!" |
| 137 | + producer.send(new ProducerRecord<>(SHARED_TOPIC, partition, null, greeting)) |
| 138 | +
|
| 139 | + then: "wait for PRODUCER span" |
| 140 | + waitForTraces(1) |
| 141 | +
|
| 142 | + when: "receive messages" |
| 143 | + def consumerRecords = consumer.poll(Duration.ofSeconds(5).toMillis()) |
| 144 | + def recordsInPartition = consumerRecords.records(new TopicPartition(SHARED_TOPIC, partition)) |
| 145 | + for (record in recordsInPartition) { |
| 146 | + assert record.value() == greeting |
| 147 | + assert record.key() == null |
| 148 | + } |
| 149 | +
|
| 150 | + then: |
| 151 | + assertTraces(1) { |
| 152 | + trace(0, 2) { |
| 153 | + span(0) { |
| 154 | + name SHARED_TOPIC + " send" |
| 155 | + kind PRODUCER |
| 156 | + hasNoParent() |
| 157 | + attributes { |
| 158 | + "${SemanticAttributes.MESSAGING_SYSTEM.key}" "kafka" |
| 159 | + "${SemanticAttributes.MESSAGING_DESTINATION.key}" SHARED_TOPIC |
| 160 | + "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" |
| 161 | + "${SemanticAttributes.MESSAGING_KAFKA_PARTITION.key}" { it >= 0 } |
| 162 | + } |
| 163 | + } |
| 164 | + span(1) { |
| 165 | + name SHARED_TOPIC + " process" |
| 166 | + kind CONSUMER |
| 167 | + childOf span(0) |
| 168 | + attributes { |
| 169 | + "${SemanticAttributes.MESSAGING_SYSTEM.key}" "kafka" |
| 170 | + "${SemanticAttributes.MESSAGING_DESTINATION.key}" SHARED_TOPIC |
| 171 | + "${SemanticAttributes.MESSAGING_DESTINATION_KIND.key}" "topic" |
| 172 | + "${SemanticAttributes.MESSAGING_OPERATION.key}" "process" |
| 173 | + "${SemanticAttributes.MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES.key}" Long |
| 174 | + "${SemanticAttributes.MESSAGING_KAFKA_PARTITION.key}" { it >= 0 } |
| 175 | + "kafka.offset" Long |
| 176 | + "kafka.record.queue_time_ms" { it >= 0 } |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments