Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flaky spring kafka batch test #5740

Merged
merged 2 commits into from
Apr 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import io.opentelemetry.instrumentation.testing.GlobalTraceUtil
import io.opentelemetry.sdk.trace.data.SpanData
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes
import java.time.Duration
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
import org.apache.kafka.clients.admin.NewTopic
import org.apache.kafka.clients.consumer.ConsumerRecord
import org.springframework.boot.SpringApplication
Expand Down Expand Up @@ -64,12 +67,28 @@ class SpringKafkaInstrumentationTest extends AgentInstrumentationSpecification {
def kafkaTemplate = applicationContext.getBean("kafkaTemplate", KafkaTemplate)

when:
runWithSpan("producer") {
// wrapping in a transaction is needed to remove the possibility of messages being picked up separately by the consumer
kafkaTemplate.executeInTransaction({ ops ->
ops.send("testTopic", "10", "testSpan1")
ops.send("testTopic", "20", "testSpan2")
})
// This test assumes that messages are sent and received as a batch. Occasionally it happens
// that the messages are not received as a batch, but one by one. This doesn't match what the
// assertion expects. To reduce flakiness we retry the test when messages weren't received as
// a batch.
def maxAttempts = 5
for (i in 1..maxAttempts) {
Listener.reset()

runWithSpan("producer") {
kafkaTemplate.executeInTransaction({ ops ->
ops.send("testTopic", "10", "testSpan1")
ops.send("testTopic", "20", "testSpan2")
})
}

Listener.waitForMessages()
if (Listener.getLastBatchSize() == 2) {
break
} else if (i < maxAttempts) {
ignoreTracesAndClear(2)
System.err.println("Messages weren't received as batch, retrying")
}
}

then:
Expand Down Expand Up @@ -242,15 +261,35 @@ class SpringKafkaInstrumentationTest extends AgentInstrumentationSpecification {
}

static class Listener {
static AtomicInteger lastBatchSize = new AtomicInteger()
static CountDownLatch messageReceived = new CountDownLatch(2)

@KafkaListener(id = "testListener", topics = "testTopic", containerFactory = "batchFactory")
void listener(List<ConsumerRecord<String, String>> records) {
lastBatchSize.set(records.size())
records.size().times {
messageReceived.countDown()
}

GlobalTraceUtil.runWithSpan("consumer") {}
records.forEach({ record ->
if (record.value() == "error") {
throw new IllegalArgumentException("boom")
}
})
}

static void reset() {
messageReceived = new CountDownLatch(2)
lastBatchSize.set(0)
}

static void waitForMessages() {
messageReceived.await(30, TimeUnit.SECONDS)
}

static int getLastBatchSize() {
return lastBatchSize.get()
}
}
}