From 106ceb0206f6882e47973b4207e1ae15d45790ad Mon Sep 17 00:00:00 2001 From: bm1549 Date: Tue, 10 Mar 2026 15:53:12 -0400 Subject: [PATCH] Fix flaky KafkaClientDataStreamsDisabledForkedTest batch consume test The test mapped consumer traces to producer spans by positional index after SORT_TRACES_BY_ID sorting. Since trace IDs are random, the consumer-to-producer mapping was non-deterministic, causing intermittent `span.parentId == parent.spanId` assertion failures. Fix by dynamically finding each consumer span's actual parent producer span via parentId matching instead of relying on sort order. Co-Authored-By: Claude Opus 4.6 --- .../test/groovy/KafkaClientTestBase.groovy | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/dd-java-agent/instrumentation/kafka/kafka-clients-0.11/src/test/groovy/KafkaClientTestBase.groovy b/dd-java-agent/instrumentation/kafka/kafka-clients-0.11/src/test/groovy/KafkaClientTestBase.groovy index cf0ff225f8c..87eb04ed885 100644 --- a/dd-java-agent/instrumentation/kafka/kafka-clients-0.11/src/test/groovy/KafkaClientTestBase.groovy +++ b/dd-java-agent/instrumentation/kafka/kafka-clients-0.11/src/test/groovy/KafkaClientTestBase.groovy @@ -923,14 +923,17 @@ abstract class KafkaClientTestBase extends VersionedNamingTestBase { queueSpan(it, trace(0)[2]) } } else { - trace(1) { - consumerSpan(it, consumerProperties, trace(0)[6], 0..0) - } - trace(1) { - consumerSpan(it, consumerProperties, trace(0)[4], 0..1) - } - trace(1) { - consumerSpan(it, consumerProperties, trace(0)[2], 0..1) + // Consumer traces are sorted by random span ID, so we can't assume a fixed + // mapping between consumer trace index and producer span index. Instead, find + // the actual parent producer span for each consumer trace dynamically. + def producerSpans = [trace(0)[2], trace(0)[4], trace(0)[6]] + (1..3).each { traceIdx -> + def consumerSpanParentId = trace(traceIdx)[0].parentId + def parentProducerSpan = producerSpans.find { it.spanId == consumerSpanParentId } + assert parentProducerSpan != null : "Consumer trace $traceIdx has no matching producer span" + trace(1) { + consumerSpan(it, consumerProperties, parentProducerSpan, 0..1) + } } } }