-
Notifications
You must be signed in to change notification settings - Fork 415
/
kafka_test.go
331 lines (278 loc) · 9.23 KB
/
kafka_test.go
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.
package kafka
import (
"context"
"errors"
"os"
"testing"
"time"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/mocktracer"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"github.com/confluentinc/confluent-kafka-go/kafka"
"github.com/stretchr/testify/assert"
)
var (
testGroupID = "gotest"
testTopic = "gotest"
)
func TestConsumerChannel(t *testing.T) {
// we can test consuming via the Events channel by artifically sending
// messages. Testing .Poll is done via an integration test.
mt := mocktracer.Start()
defer mt.Stop()
c, err := NewConsumer(&kafka.ConfigMap{
"go.events.channel.enable": true, // required for the events channel to be turned on
"group.id": testGroupID,
"socket.timeout.ms": 10,
"session.timeout.ms": 10,
"enable.auto.offset.store": false,
}, WithAnalyticsRate(0.3))
assert.NoError(t, err)
err = c.Subscribe(testTopic, nil)
assert.NoError(t, err)
go func() {
c.Consumer.Events() <- &kafka.Message{
TopicPartition: kafka.TopicPartition{
Topic: &testTopic,
Partition: 1,
Offset: 1,
},
Key: []byte("key1"),
Value: []byte("value1"),
}
c.Consumer.Events() <- &kafka.Message{
TopicPartition: kafka.TopicPartition{
Topic: &testTopic,
Partition: 1,
Offset: 2,
},
Key: []byte("key2"),
Value: []byte("value2"),
}
}()
msg1 := (<-c.Events()).(*kafka.Message)
assert.Equal(t, []byte("key1"), msg1.Key)
msg2 := (<-c.Events()).(*kafka.Message)
assert.Equal(t, []byte("key2"), msg2.Key)
c.Close()
// wait for the events channel to be closed
<-c.Events()
spans := mt.FinishedSpans()
assert.Len(t, spans, 2)
for i, s := range spans {
assert.Equal(t, "kafka.consume", s.OperationName())
assert.Equal(t, "kafka", s.Tag(ext.ServiceName))
assert.Equal(t, "Consume Topic gotest", s.Tag(ext.ResourceName))
assert.Equal(t, "queue", s.Tag(ext.SpanType))
assert.Equal(t, int32(1), s.Tag("partition"))
assert.Equal(t, 0.3, s.Tag(ext.EventSampleRate))
assert.Equal(t, kafka.Offset(i+1), s.Tag("offset"))
assert.Equal(t, "confluentinc/confluent-kafka-go/kafka", s.Tag(ext.Component))
assert.Equal(t, ext.SpanKindConsumer, s.Tag(ext.SpanKind))
}
}
/*
to run the integration test locally:
docker network create confluent
docker run --rm \
--name zookeeper \
--network confluent \
-p 2181:2181 \
-e ZOOKEEPER_CLIENT_PORT=2181 \
confluentinc/cp-zookeeper:5.0.0
docker run --rm \
--name kafka \
--network confluent \
-p 9092:9092 \
-e KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181 \
-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092 \
-e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 \
-e KAFKA_CREATE_TOPICS=gotest:1:1 \
-e KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1 \
confluentinc/cp-kafka:5.0.0
*/
func TestConsumerFunctional(t *testing.T) {
if _, ok := os.LookupEnv("INTEGRATION"); !ok {
t.Skip("to enable integration test, set the INTEGRATION environment variable")
}
for _, tt := range []struct {
name string
action func(c *Consumer) (*kafka.Message, error)
}{
{
name: "Poll",
action: func(c *Consumer) (*kafka.Message, error) {
switch e := c.Poll(3000).(type) {
case *kafka.Message:
return e, nil
default:
return nil, errors.New("some error")
}
},
},
{
name: "ReadMessage",
action: func(c *Consumer) (*kafka.Message, error) {
return c.ReadMessage(3000 * time.Millisecond)
},
},
} {
t.Run(tt.name, func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
// first write a message to the topic
p, err := NewProducer(&kafka.ConfigMap{
"group.id": testGroupID,
"bootstrap.servers": "127.0.0.1:9092",
"go.delivery.reports": true,
}, WithAnalyticsRate(0.1))
assert.NoError(t, err)
delivery := make(chan kafka.Event, 1)
err = p.Produce(&kafka.Message{
TopicPartition: kafka.TopicPartition{
Topic: &testTopic,
Partition: 0,
},
Key: []byte("key2"),
Value: []byte("value2"),
}, delivery)
assert.NoError(t, err)
msg1, _ := (<-delivery).(*kafka.Message)
p.Close()
// next attempt to consume the message
c, err := NewConsumer(&kafka.ConfigMap{
"group.id": testGroupID,
"bootstrap.servers": "127.0.0.1:9092",
"socket.timeout.ms": 1000,
"session.timeout.ms": 1000,
"enable.auto.offset.store": false,
})
assert.NoError(t, err)
err = c.Assign([]kafka.TopicPartition{
{Topic: &testTopic, Partition: 0, Offset: msg1.TopicPartition.Offset},
})
assert.NoError(t, err)
msg2, err := tt.action(c)
assert.NoError(t, err)
assert.Equal(t, msg1.String(), msg2.String())
c.Close()
// now verify the spans
spans := mt.FinishedSpans()
assert.Len(t, spans, 2)
// they should be linked via headers
assert.Equal(t, spans[0].TraceID(), spans[1].TraceID())
s0 := spans[0] // produce
assert.Equal(t, "kafka.produce", s0.OperationName())
assert.Equal(t, "kafka", s0.Tag(ext.ServiceName))
assert.Equal(t, "Produce Topic gotest", s0.Tag(ext.ResourceName))
assert.Equal(t, 0.1, s0.Tag(ext.EventSampleRate))
assert.Equal(t, "queue", s0.Tag(ext.SpanType))
assert.Equal(t, int32(0), s0.Tag("partition"))
assert.Equal(t, "confluentinc/confluent-kafka-go/kafka", s0.Tag(ext.Component))
assert.Equal(t, ext.SpanKindProducer, s0.Tag(ext.SpanKind))
s1 := spans[1] // consume
assert.Equal(t, "kafka.consume", s1.OperationName())
assert.Equal(t, "kafka", s1.Tag(ext.ServiceName))
assert.Equal(t, "Consume Topic gotest", s1.Tag(ext.ResourceName))
assert.Equal(t, nil, s1.Tag(ext.EventSampleRate))
assert.Equal(t, "queue", s1.Tag(ext.SpanType))
assert.Equal(t, int32(0), s1.Tag("partition"))
assert.Equal(t, "confluentinc/confluent-kafka-go/kafka", s1.Tag(ext.Component))
assert.Equal(t, ext.SpanKindConsumer, s1.Tag(ext.SpanKind))
})
}
}
// This tests the deprecated behavior of using cfg.context as the context passed via kafka messages
// instead of the one passed in the message.
func TestDeprecatedContext(t *testing.T) {
if _, ok := os.LookupEnv("INTEGRATION"); !ok {
t.Skip("to enable integration test, set the INTEGRATION environment variable")
}
tracer.Start()
defer tracer.Stop()
// Create the span to be passed
parentSpan, ctx := tracer.StartSpanFromContext(context.Background(), "test_parent_context")
c, err := NewConsumer(&kafka.ConfigMap{
"go.events.channel.enable": true, // required for the events channel to be turned on
"group.id": testGroupID,
"socket.timeout.ms": 10,
"session.timeout.ms": 10,
"enable.auto.offset.store": false,
}, WithContext(ctx)) // Adds the parent context containing a span
err = c.Subscribe(testTopic, nil)
assert.NoError(t, err)
// This span context will be ignored
messageSpan, _ := tracer.StartSpanFromContext(context.Background(), "test_context_from_message")
messageSpanContext := messageSpan.Context()
/// Produce a message with a span
go func() {
msg := &kafka.Message{
TopicPartition: kafka.TopicPartition{
Topic: &testTopic,
Partition: 1,
Offset: 1,
},
Key: []byte("key1"),
Value: []byte("value1"),
}
// Inject the span context in the message to be produced
carrier := NewMessageCarrier(msg)
tracer.Inject(messageSpan.Context(), carrier)
c.Consumer.Events() <- msg
}()
msg := (<-c.Events()).(*kafka.Message)
// Extract the context from the message
carrier := NewMessageCarrier(msg)
spanContext, err := tracer.Extract(carrier)
assert.NoError(t, err)
parentContext := parentSpan.Context()
/// The context passed is the one from the parent context
assert.EqualValues(t, parentContext.TraceID(), spanContext.TraceID())
/// The context passed is not the one passed in the message
assert.NotEqualValues(t, messageSpanContext.TraceID(), spanContext.TraceID())
c.Close()
// wait for the events channel to be closed
<-c.Events()
}
func TestCustomTags(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
c, err := NewConsumer(&kafka.ConfigMap{
"go.events.channel.enable": true, // required for the events channel to be turned on
"group.id": testGroupID,
"socket.timeout.ms": 10,
"session.timeout.ms": 10,
"enable.auto.offset.store": false,
}, WithCustomTag("foo", func(msg *kafka.Message) interface{} {
return "bar"
}), WithCustomTag("key", func(msg *kafka.Message) interface{} {
return msg.Key
}))
assert.NoError(t, err)
err = c.Subscribe(testTopic, nil)
assert.NoError(t, err)
go func() {
c.Consumer.Events() <- &kafka.Message{
TopicPartition: kafka.TopicPartition{
Topic: &testTopic,
Partition: 1,
Offset: 1,
},
Key: []byte("key1"),
Value: []byte("value1"),
}
}()
<-c.Events()
c.Close()
// wait for the events channel to be closed
<-c.Events()
spans := mt.FinishedSpans()
assert.Len(t, spans, 1)
s := spans[0]
assert.Equal(t, "bar", s.Tag("foo"))
assert.Equal(t, []byte("key1"), s.Tag("key"))
}