-
-
Notifications
You must be signed in to change notification settings - Fork 475
Expand file tree
/
Copy pathKafkaQueueSystemTest.kt
More file actions
117 lines (95 loc) · 3.88 KB
/
Copy pathKafkaQueueSystemTest.kt
File metadata and controls
117 lines (95 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package io.sentry.systemtest
import io.sentry.systemtest.util.TestHelper
import kotlin.test.Test
import kotlin.test.assertEquals
import org.junit.Before
/**
* System tests for Kafka queue instrumentation.
*
* Requires:
* - The sample app running with `--spring.profiles.active=kafka`
* - A Kafka broker at localhost:9092
* - The mock Sentry server at localhost:8000
*/
class KafkaQueueSystemTest {
lateinit var testHelper: TestHelper
@Before
fun setup() {
testHelper = TestHelper("http://localhost:8080")
testHelper.reset()
}
@Test
fun `producer endpoint creates queue publish span`() {
val restClient = testHelper.restClient
restClient.produceKafkaMessage("test-message")
assertEquals(200, restClient.lastKnownStatusCode)
testHelper.ensureTransactionReceived { transaction, _ ->
testHelper.doesTransactionContainSpanWithOp(transaction, "queue.publish")
}
}
@Test
fun `consumer creates queue process transaction`() {
val restClient = testHelper.restClient
restClient.produceKafkaMessage("test-consumer-message")
assertEquals(200, restClient.lastKnownStatusCode)
// The consumer runs asynchronously, so wait for the queue.process transaction
testHelper.ensureTransactionReceived { transaction, _ ->
testHelper.doesTransactionHaveOp(transaction, "queue.process")
}
}
@Test
fun `producer and consumer share same trace`() {
val restClient = testHelper.restClient
restClient.produceKafkaMessage("trace-test-message")
assertEquals(200, restClient.lastKnownStatusCode)
// Capture the trace ID from the producer transaction (has queue.publish span)
var producerTraceId: String? = null
testHelper.ensureTransactionReceived { transaction, _ ->
if (testHelper.doesTransactionContainSpanWithOp(transaction, "queue.publish")) {
producerTraceId = transaction.contexts.trace?.traceId?.toString()
true
} else {
false
}
}
// Verify the consumer transaction has the same trace ID
// Use retryCount=3 since the consumer may take a moment to process
testHelper.ensureEnvelopeReceived(retryCount = 3) { envelopeString ->
val envelope =
testHelper.jsonSerializer.deserializeEnvelope(envelopeString.byteInputStream())
?: return@ensureEnvelopeReceived false
val txItem =
envelope.items.firstOrNull { it.header.type == io.sentry.SentryItemType.Transaction }
?: return@ensureEnvelopeReceived false
val tx =
txItem.getTransaction(testHelper.jsonSerializer) ?: return@ensureEnvelopeReceived false
tx.contexts.trace?.operation == "queue.process" &&
tx.contexts.trace?.traceId?.toString() == producerTraceId
}
}
@Test
fun `queue publish span has messaging attributes`() {
val restClient = testHelper.restClient
restClient.produceKafkaMessage("attrs-test")
assertEquals(200, restClient.lastKnownStatusCode)
testHelper.ensureTransactionReceived { transaction, _ ->
val span = transaction.spans.firstOrNull { it.op == "queue.publish" }
if (span == null) return@ensureTransactionReceived false
val data = span.data ?: return@ensureTransactionReceived false
data["messaging.system"] == "kafka" && data["messaging.destination.name"] == "sentry-topic"
}
}
@Test
fun `queue process transaction has messaging attributes`() {
val restClient = testHelper.restClient
restClient.produceKafkaMessage("process-attrs-test")
assertEquals(200, restClient.lastKnownStatusCode)
testHelper.ensureTransactionReceived { transaction, _ ->
if (!testHelper.doesTransactionHaveOp(transaction, "queue.process")) {
return@ensureTransactionReceived false
}
val data = transaction.contexts.trace?.data ?: return@ensureTransactionReceived false
data["messaging.system"] == "kafka" && data["messaging.destination.name"] == "sentry-topic"
}
}
}