|
| 1 | +package io.sentry.systemtest |
| 2 | + |
| 3 | +import io.sentry.systemtest.util.TestHelper |
| 4 | +import kotlin.test.Test |
| 5 | +import kotlin.test.assertEquals |
| 6 | +import org.junit.Before |
| 7 | + |
| 8 | +/** |
| 9 | + * System tests for Kafka queue instrumentation. |
| 10 | + * |
| 11 | + * Requires: |
| 12 | + * - The sample app running with `--spring.profiles.active=kafka` |
| 13 | + * - A Kafka broker at localhost:9092 |
| 14 | + * - The mock Sentry server at localhost:8000 |
| 15 | + */ |
| 16 | +class KafkaQueueSystemTest { |
| 17 | + lateinit var testHelper: TestHelper |
| 18 | + |
| 19 | + @Before |
| 20 | + fun setup() { |
| 21 | + testHelper = TestHelper("http://localhost:8080") |
| 22 | + testHelper.reset() |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + fun `producer endpoint creates queue publish span`() { |
| 27 | + val restClient = testHelper.restClient |
| 28 | + |
| 29 | + restClient.produceKafkaMessage("test-message") |
| 30 | + assertEquals(200, restClient.lastKnownStatusCode) |
| 31 | + |
| 32 | + testHelper.ensureTransactionReceived { transaction, _ -> |
| 33 | + testHelper.doesTransactionContainSpanWithOp(transaction, "queue.publish") |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + fun `consumer creates queue process transaction`() { |
| 39 | + val restClient = testHelper.restClient |
| 40 | + |
| 41 | + restClient.produceKafkaMessage("test-consumer-message") |
| 42 | + assertEquals(200, restClient.lastKnownStatusCode) |
| 43 | + |
| 44 | + // The consumer runs asynchronously, so wait for the queue.process transaction |
| 45 | + testHelper.ensureTransactionReceived { transaction, _ -> |
| 46 | + testHelper.doesTransactionHaveOp(transaction, "queue.process") |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + fun `producer and consumer share same trace`() { |
| 52 | + val restClient = testHelper.restClient |
| 53 | + |
| 54 | + restClient.produceKafkaMessage("trace-test-message") |
| 55 | + assertEquals(200, restClient.lastKnownStatusCode) |
| 56 | + |
| 57 | + // Capture the trace ID from the producer transaction |
| 58 | + var producerTraceId: String? = null |
| 59 | + testHelper.ensureTransactionReceived { transaction, _ -> |
| 60 | + if (testHelper.doesTransactionContainSpanWithOp(transaction, "queue.publish")) { |
| 61 | + producerTraceId = transaction.contexts.trace?.traceId?.toString() |
| 62 | + true |
| 63 | + } else { |
| 64 | + false |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Verify the consumer transaction has the same trace ID |
| 69 | + testHelper.ensureTransactionReceived { transaction, _ -> |
| 70 | + if (testHelper.doesTransactionHaveOp(transaction, "queue.process")) { |
| 71 | + val consumerTraceId = transaction.contexts.trace?.traceId?.toString() |
| 72 | + producerTraceId != null && consumerTraceId == producerTraceId |
| 73 | + } else { |
| 74 | + false |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun `queue publish span has messaging attributes`() { |
| 81 | + val restClient = testHelper.restClient |
| 82 | + |
| 83 | + restClient.produceKafkaMessage("attrs-test") |
| 84 | + assertEquals(200, restClient.lastKnownStatusCode) |
| 85 | + |
| 86 | + testHelper.ensureTransactionReceived { transaction, _ -> |
| 87 | + val span = transaction.spans.firstOrNull { it.op == "queue.publish" } |
| 88 | + if (span == null) return@ensureTransactionReceived false |
| 89 | + |
| 90 | + val data = span.data ?: return@ensureTransactionReceived false |
| 91 | + data["messaging.system"] == "kafka" && data["messaging.destination.name"] == "sentry-topic" |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + fun `queue process transaction has messaging attributes`() { |
| 97 | + val restClient = testHelper.restClient |
| 98 | + |
| 99 | + restClient.produceKafkaMessage("process-attrs-test") |
| 100 | + assertEquals(200, restClient.lastKnownStatusCode) |
| 101 | + |
| 102 | + testHelper.ensureTransactionReceived { transaction, _ -> |
| 103 | + if (!testHelper.doesTransactionHaveOp(transaction, "queue.process")) { |
| 104 | + return@ensureTransactionReceived false |
| 105 | + } |
| 106 | + |
| 107 | + val data = transaction.contexts.trace?.data ?: return@ensureTransactionReceived false |
| 108 | + data["messaging.system"] == "kafka" && data["messaging.destination.name"] == "sentry-topic" |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments