forked from mostafa/xk6-kafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
producer.go
322 lines (270 loc) · 9.58 KB
/
producer.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
package kafka
import (
"fmt"
"time"
kafkago "github.com/segmentio/kafka-go"
"github.com/segmentio/kafka-go/compress"
"go.k6.io/k6/metrics"
)
var (
// CompressionCodecs is a map of compression codec names to their respective codecs
// TODO: add as global constants to JS
CompressionCodecs = map[string]compress.Codec{
"Gzip": &compress.GzipCodec,
"Snappy": &compress.SnappyCodec,
"Lz4": &compress.Lz4Codec,
"Zstd": &compress.ZstdCodec,
}
// DefaultSerializer is string serializer
DefaultSerializer = StringSerializer
)
// Writer creates a new Kafka writer
// TODO: accept a configuration
func (k *Kafka) Writer(brokers []string, topic string, saslConfig SASLConfig, tlsConfig TLSConfig, compression string) (*kafkago.Writer, *Xk6KafkaError) {
dialer, err := GetDialer(saslConfig, tlsConfig)
if err != nil {
if err.Unwrap() != nil {
k.logger.WithField("error", err).Error(err)
}
return nil, err
}
// TODO: add AllowAutoTopicCreation to writer configuration
writerConfig := kafkago.WriterConfig{
Brokers: brokers,
Topic: topic,
Balancer: &kafkago.LeastBytes{},
BatchSize: 1,
Dialer: dialer,
Async: false,
}
if codec, ok := CompressionCodecs[compression]; ok {
writerConfig.CompressionCodec = codec
}
// TODO: instantiate Writer directly
return kafkago.NewWriter(writerConfig), nil
}
// Produce sends messages to Kafka
func (k *Kafka) Produce(
writer *kafkago.Writer, messages []map[string]interface{},
keySchema string, valueSchema string, autoCreateTopic bool) *Xk6KafkaError {
writer.AllowAutoTopicCreation = autoCreateTopic
return k.produceInternal(writer, messages, Configuration{}, keySchema, valueSchema)
}
// ProduceWithConfiguration sends messages to Kafka with the given configuration
func (k *Kafka) ProduceWithConfiguration(
writer *kafkago.Writer, messages []map[string]interface{},
configurationJson string, keySchema string, valueSchema string, autoCreateTopic bool) *Xk6KafkaError {
writer.AllowAutoTopicCreation = autoCreateTopic
configuration, err := UnmarshalConfiguration(configurationJson)
if err != nil {
if err.Unwrap() != nil {
k.logger.WithField("error", err).Error(err)
}
return err
}
return k.produceInternal(writer, messages, configuration, keySchema, valueSchema)
}
// GetSerializer returns the serializer for the given schema
func (k *Kafka) GetSerializer(schema string) Serializer {
if ser, ok := k.serializerRegistry.Registry[schema]; ok {
return ser.GetSerializer()
}
return SerializeString
}
// produceInternal sends messages to Kafka with the given configuration
func (k *Kafka) produceInternal(
writer *kafkago.Writer, messages []map[string]interface{},
configuration Configuration, keySchema string, valueSchema string) *Xk6KafkaError {
state := k.vu.State()
if state == nil {
k.logger.WithField("error", ErrorForbiddenInInitContext).Error(ErrorForbiddenInInitContext)
return ErrorForbiddenInInitContext
}
ctx := k.vu.Context()
if ctx == nil {
err := NewXk6KafkaError(noContextError, "No context.", nil)
k.logger.WithField("error", err).Info(err)
return err
}
err := ValidateConfiguration(configuration)
if err != nil {
configuration.Producer.KeySerializer = DefaultSerializer
configuration.Producer.ValueSerializer = DefaultSerializer
state.Logger.WithField("error", err).Warn("Using default string serializers")
}
keySerializer := k.GetSerializer(configuration.Producer.KeySerializer)
valueSerializer := k.GetSerializer(configuration.Producer.ValueSerializer)
kafkaMessages := make([]kafkago.Message, len(messages))
for i, message := range messages {
kafkaMessages[i] = kafkago.Message{}
// Topic can be explicitly set on each individual message
// Setting topic on the writer and the messages are mutually exclusive
if _, has_topic := message["topic"]; has_topic {
kafkaMessages[i].Topic = message["topic"].(string)
}
if _, has_offset := message["offset"]; has_offset {
kafkaMessages[i].Offset = message["offset"].(int64)
}
// If time is set, use it to set the time on the message,
// otherwise use the current time.
if _, has_time := message["time"]; has_time {
kafkaMessages[i].Time = time.UnixMilli(message["time"].(int64))
}
// If a key was provided, add it to the message. Keys are optional.
if _, has_key := message["key"]; has_key {
keyData, err := keySerializer(
configuration, writer.Stats().Topic, message["key"], "key", keySchema, 0)
if err != nil && err.Unwrap() != nil {
k.logger.WithField("error", err).Error(err)
}
kafkaMessages[i].Key = keyData
}
// Then add the message
valueData, err := valueSerializer(configuration, writer.Stats().Topic, message["value"], "value", valueSchema, 0)
if err != nil && err.Unwrap() != nil {
k.logger.WithField("error", err).Error(err)
}
kafkaMessages[i].Value = valueData
// If headers are provided, add them to the message.
if _, has_headers := message["headers"]; has_headers {
for key, value := range message["headers"].(map[string]interface{}) {
kafkaMessages[i].Headers = append(kafkaMessages[i].Headers, kafkago.Header{
Key: key,
Value: []byte(fmt.Sprint(value)),
})
}
}
}
originalErr := writer.WriteMessages(k.vu.Context(), kafkaMessages...)
err = k.reportWriterStats(writer.Stats())
if err != nil {
k.logger.WithField("error", err).Error(err)
}
if originalErr != nil {
if originalErr == k.vu.Context().Err() {
k.logger.WithField("error", k.vu.Context().Err()).Error(k.vu.Context().Err())
return NewXk6KafkaError(contextCancelled, "Context cancelled.", originalErr)
} else {
// TODO: fix this
// Ignore stats reporting errors here, because we can't return twice,
// and there is no way to wrap the error in another one.
k.logger.WithField("error", originalErr).Error(originalErr)
return NewXk6KafkaError(failedWriteMessage, "Failed to write messages.", err)
}
}
return nil
}
// reportWriterStats reports the writer stats to the state
func (k *Kafka) reportWriterStats(currentStats kafkago.WriterStats) *Xk6KafkaError {
state := k.vu.State()
if state == nil {
k.logger.WithField("error", ErrorForbiddenInInitContext).Error(ErrorForbiddenInInitContext)
return ErrorForbiddenInInitContext
}
ctx := k.vu.Context()
if ctx == nil {
err := NewXk6KafkaError(cannotReportStats, "Cannot report writer stats, no context.", nil)
k.logger.WithField("error", err).Info(err)
return err
}
tags := make(map[string]string)
tags["topic"] = currentStats.Topic
now := time.Now()
metrics.PushIfNotDone(ctx, state.Samples, metrics.Sample{
Time: now,
Metric: k.metrics.WriterWrites,
Tags: metrics.IntoSampleTags(&tags),
Value: float64(currentStats.Writes),
})
metrics.PushIfNotDone(ctx, state.Samples, metrics.Sample{
Time: now,
Metric: k.metrics.WriterMessages,
Tags: metrics.IntoSampleTags(&tags),
Value: float64(currentStats.Messages),
})
metrics.PushIfNotDone(ctx, state.Samples, metrics.Sample{
Time: now,
Metric: k.metrics.WriterBytes,
Tags: metrics.IntoSampleTags(&tags),
Value: float64(currentStats.Bytes),
})
metrics.PushIfNotDone(ctx, state.Samples, metrics.Sample{
Time: now,
Metric: k.metrics.WriterErrors,
Tags: metrics.IntoSampleTags(&tags),
Value: float64(currentStats.Errors),
})
metrics.PushIfNotDone(ctx, state.Samples, metrics.Sample{
Time: now,
Metric: k.metrics.WriterWriteTime,
Tags: metrics.IntoSampleTags(&tags),
Value: metrics.D(currentStats.WriteTime.Avg),
})
metrics.PushIfNotDone(ctx, state.Samples, metrics.Sample{
Time: now,
Metric: k.metrics.WriterWaitTime,
Tags: metrics.IntoSampleTags(&tags),
Value: metrics.D(currentStats.WaitTime.Avg),
})
metrics.PushIfNotDone(ctx, state.Samples, metrics.Sample{
Time: now,
Metric: k.metrics.WriterRetries,
Tags: metrics.IntoSampleTags(&tags),
Value: float64(currentStats.Retries.Avg),
})
metrics.PushIfNotDone(ctx, state.Samples, metrics.Sample{
Time: now,
Metric: k.metrics.WriterBatchSize,
Tags: metrics.IntoSampleTags(&tags),
Value: float64(currentStats.BatchSize.Avg),
})
metrics.PushIfNotDone(ctx, state.Samples, metrics.Sample{
Time: now,
Metric: k.metrics.WriterBatchBytes,
Tags: metrics.IntoSampleTags(&tags),
Value: float64(currentStats.BatchBytes.Avg),
})
metrics.PushIfNotDone(ctx, state.Samples, metrics.Sample{
Time: now,
Metric: k.metrics.WriterMaxAttempts,
Tags: metrics.IntoSampleTags(&tags),
Value: float64(currentStats.MaxAttempts),
})
metrics.PushIfNotDone(ctx, state.Samples, metrics.Sample{
Time: now,
Metric: k.metrics.WriterMaxBatchSize,
Tags: metrics.IntoSampleTags(&tags),
Value: float64(currentStats.MaxBatchSize),
})
metrics.PushIfNotDone(ctx, state.Samples, metrics.Sample{
Time: now,
Metric: k.metrics.WriterBatchTimeout,
Tags: metrics.IntoSampleTags(&tags),
Value: float64(currentStats.BatchTimeout),
})
metrics.PushIfNotDone(ctx, state.Samples, metrics.Sample{
Time: now,
Metric: k.metrics.WriterReadTimeout,
Tags: metrics.IntoSampleTags(&tags),
Value: float64(currentStats.ReadTimeout),
})
metrics.PushIfNotDone(ctx, state.Samples, metrics.Sample{
Time: now,
Metric: k.metrics.WriterWriteTimeout,
Tags: metrics.IntoSampleTags(&tags),
Value: float64(currentStats.WriteTimeout),
})
metrics.PushIfNotDone(ctx, state.Samples, metrics.Sample{
Time: now,
Metric: k.metrics.WriterRequiredAcks,
Tags: metrics.IntoSampleTags(&tags),
Value: float64(currentStats.RequiredAcks),
})
metrics.PushIfNotDone(ctx, state.Samples, metrics.Sample{
Time: now,
Metric: k.metrics.WriterAsync,
Tags: metrics.IntoSampleTags(&tags),
Value: metrics.B(currentStats.Async),
})
return nil
}