|
6 | 6 | package io.opentelemetry.javaagent.instrumentation.kafkaclients.v0_11;
|
7 | 7 |
|
8 | 8 | import static io.opentelemetry.instrumentation.testing.util.TelemetryDataUtil.orderByRootSpanKind;
|
9 |
| -import static org.assertj.core.api.Assertions.assertThat; |
| 9 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; |
10 | 10 |
|
11 | 11 | import io.opentelemetry.api.trace.SpanKind;
|
12 | 12 | import io.opentelemetry.instrumentation.kafkaclients.common.v0_11.internal.KafkaClientBaseTest;
|
@@ -204,4 +204,74 @@ void testRecordsWithTopicPartitionKafkaConsume()
|
204 | 204 | .hasAttributesSatisfyingExactly(
|
205 | 205 | processAttributes(null, greeting, false, false))));
|
206 | 206 | }
|
| 207 | + |
| 208 | + @DisplayName("test kafka null header") |
| 209 | + @Test |
| 210 | + void testKafkaHeaderNull() throws Exception { |
| 211 | + String greeting = "Hello Kafka with null header!"; |
| 212 | + testing.runWithSpan( |
| 213 | + "parent", |
| 214 | + () -> { |
| 215 | + ProducerRecord<Integer, String> producerRecord = |
| 216 | + new ProducerRecord<>(SHARED_TOPIC, 10, greeting); |
| 217 | + producerRecord.headers().add("test-message-header", null); |
| 218 | + producer |
| 219 | + .send( |
| 220 | + producerRecord, |
| 221 | + (meta, ex) -> { |
| 222 | + if (ex == null) { |
| 223 | + testing.runWithSpan("producer callback", () -> {}); |
| 224 | + } else { |
| 225 | + testing.runWithSpan("producer exception: " + ex, () -> {}); |
| 226 | + } |
| 227 | + }) |
| 228 | + .get(5, TimeUnit.SECONDS); |
| 229 | + }); |
| 230 | + |
| 231 | + awaitUntilConsumerIsReady(); |
| 232 | + ConsumerRecords<?, ?> records = poll(Duration.ofSeconds(5)); |
| 233 | + assertThat(records.count()).isEqualTo(1); |
| 234 | + |
| 235 | + for (ConsumerRecord<?, ?> record : records) { |
| 236 | + testing.runWithSpan( |
| 237 | + "processing", |
| 238 | + () -> { |
| 239 | + assertThat(record.key()).isEqualTo(10); |
| 240 | + assertThat(record.value()).isEqualTo(greeting); |
| 241 | + assertThat(record.headers().lastHeader("test-message-header").value()).isNull(); |
| 242 | + }); |
| 243 | + } |
| 244 | + AtomicReference<SpanData> producerSpan = new AtomicReference<>(); |
| 245 | + testing.waitAndAssertSortedTraces( |
| 246 | + orderByRootSpanKind(SpanKind.INTERNAL, SpanKind.CONSUMER), |
| 247 | + trace -> { |
| 248 | + trace.hasSpansSatisfyingExactly( |
| 249 | + span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(), |
| 250 | + span -> |
| 251 | + span.hasName(SHARED_TOPIC + " publish") |
| 252 | + .hasKind(SpanKind.PRODUCER) |
| 253 | + .hasParent(trace.getSpan(0)) |
| 254 | + .hasAttributesSatisfyingExactly(sendAttributes("10", greeting, false)), |
| 255 | + span -> |
| 256 | + span.hasName("producer callback") |
| 257 | + .hasKind(SpanKind.INTERNAL) |
| 258 | + .hasParent(trace.getSpan(0))); |
| 259 | + producerSpan.set(trace.getSpan(1)); |
| 260 | + }, |
| 261 | + trace -> |
| 262 | + trace.hasSpansSatisfyingExactly( |
| 263 | + span -> |
| 264 | + span.hasName(SHARED_TOPIC + " receive") |
| 265 | + .hasKind(SpanKind.CONSUMER) |
| 266 | + .hasNoParent() |
| 267 | + .hasAttributesSatisfyingExactly(receiveAttributes(false)), |
| 268 | + span -> |
| 269 | + span.hasName(SHARED_TOPIC + " process") |
| 270 | + .hasKind(SpanKind.CONSUMER) |
| 271 | + .hasLinks(LinkData.create(producerSpan.get().getSpanContext())) |
| 272 | + .hasParent(trace.getSpan(0)) |
| 273 | + .hasAttributesSatisfyingExactly( |
| 274 | + processAttributes("10", greeting, false, false)), |
| 275 | + span -> span.hasName("processing").hasParent(trace.getSpan(1)))); |
| 276 | + } |
207 | 277 | }
|
0 commit comments