-
Notifications
You must be signed in to change notification settings - Fork 13
/
consumer_config.go
338 lines (286 loc) · 10.5 KB
/
consumer_config.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
332
333
334
335
336
337
338
package kafka
import (
"bytes"
"encoding/json"
"fmt"
"regexp"
"strings"
"time"
"github.com/segmentio/kafka-go"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
kcronsumer "github.com/Trendyol/kafka-cronsumer/pkg/kafka"
lcronsumer "github.com/Trendyol/kafka-cronsumer/pkg/logger"
)
type ReaderConfig kafka.ReaderConfig
type BatchConsumeFn func([]*Message) error
type PreBatchFn func([]*Message) []*Message
type ConsumeFn func(*Message) error
type SkipMessageByHeaderFn func(header []kafka.Header) bool
type DialConfig struct {
Timeout time.Duration
KeepAlive time.Duration
}
type ConsumerConfig struct {
DistributedTracingConfiguration DistributedTracingConfiguration
Logger LoggerInterface
APIConfiguration APIConfiguration
MetricConfiguration MetricConfiguration
SASL *SASLConfig
TLS *TLSConfig
Dial *DialConfig
BatchConfiguration *BatchConfiguration
ConsumeFn ConsumeFn
SkipMessageByHeaderFn SkipMessageByHeaderFn
TransactionalRetry *bool
RetryConfiguration RetryConfiguration
LogLevel LogLevel
Rack string
VerifyTopicOnStartup bool
ClientID string
Reader ReaderConfig
CommitInterval time.Duration
MessageGroupDuration time.Duration
Concurrency int
DistributedTracingEnabled bool
RetryEnabled bool
APIEnabled bool
// MetricPrefix is used for prometheus fq name prefix.
// If not provided, default metric prefix value is `kafka_konsumer`.
// Currently, there are two exposed prometheus metrics. `processed_messages_total_current` and `unprocessed_messages_total_current`.
// So, if default metric prefix used, metrics names are `kafka_konsumer_processed_messages_total_current` and
// `kafka_konsumer_unprocessed_messages_total_current`.
MetricPrefix string
}
func (cfg RetryConfiguration) JSON() string {
return fmt.Sprintf(`{"Brokers": ["%s"], "Topic": %q, "StartTimeCron": %q, "WorkDuration": %q, `+
`"MaxRetry": %d, "VerifyTopicOnStartup": %t, "Rack": %q}`,
strings.Join(cfg.Brokers, "\", \""), cfg.Topic, cfg.StartTimeCron,
cfg.WorkDuration, cfg.MaxRetry, cfg.VerifyTopicOnStartup, cfg.Rack)
}
func (cfg *BatchConfiguration) JSON() string {
if cfg == nil {
return "{}"
}
return fmt.Sprintf(`{"MessageGroupLimit": %d}`, cfg.MessageGroupLimit)
}
func (cfg ReaderConfig) JSON() string {
return fmt.Sprintf(`{"Brokers": ["%s"], "GroupId": %q, "GroupTopics": ["%s"], `+
`"MaxWait": %q, "CommitInterval": %q, "StartOffset": %q}`,
strings.Join(cfg.Brokers, "\", \""), cfg.GroupID, strings.Join(cfg.GroupTopics, "\", \""),
cfg.MaxWait, cfg.CommitInterval, kcronsumer.ToStringOffset(cfg.StartOffset))
}
func (cfg *ConsumerConfig) JSON() string {
if cfg == nil {
return "{}"
}
return fmt.Sprintf(`{"ClientID": %q, "Reader": %s, "BatchConfiguration": %s, "MessageGroupDuration": %q, `+
`"TransactionalRetry": %t, "Concurrency": %d, "RetryEnabled": %t, "RetryConfiguration": %s, `+
`"VerifyTopicOnStartup": %t, "Rack": %q, "SASL": %s, "TLS": %s}`,
cfg.ClientID, cfg.Reader.JSON(), cfg.BatchConfiguration.JSON(),
cfg.MessageGroupDuration, *cfg.TransactionalRetry, cfg.Concurrency,
cfg.RetryEnabled, cfg.RetryConfiguration.JSON(), cfg.VerifyTopicOnStartup,
cfg.Rack, cfg.SASL.JSON(), cfg.TLS.JSON())
}
func (cfg *ConsumerConfig) JSONPretty() string {
return jsonPretty(cfg.JSON())
}
func (cfg *ConsumerConfig) String() string {
re := regexp.MustCompile(`"(\w+)"\s*:`)
modifiedString := re.ReplaceAllString(cfg.JSON(), `$1:`)
modifiedString = modifiedString[1 : len(modifiedString)-1]
return modifiedString
}
func (cfg *ConsumerConfig) newCronsumerConfig() *kcronsumer.Config {
cronsumerCfg := kcronsumer.Config{
MetricPrefix: cfg.RetryConfiguration.MetricPrefix,
ClientID: cfg.RetryConfiguration.ClientID,
Brokers: cfg.RetryConfiguration.Brokers,
Consumer: kcronsumer.ConsumerConfig{
ClientID: cfg.ClientID,
GroupID: cfg.Reader.GroupID,
Topic: cfg.RetryConfiguration.Topic,
DeadLetterTopic: cfg.RetryConfiguration.DeadLetterTopic,
Cron: cfg.RetryConfiguration.StartTimeCron,
Duration: cfg.RetryConfiguration.WorkDuration,
MaxRetry: cfg.RetryConfiguration.MaxRetry,
VerifyTopicOnStartup: cfg.RetryConfiguration.VerifyTopicOnStartup,
Concurrency: cfg.RetryConfiguration.Concurrency,
QueueCapacity: cfg.RetryConfiguration.QueueCapacity,
MinBytes: cfg.Reader.MinBytes,
MaxBytes: cfg.Reader.MaxBytes,
MaxWait: cfg.Reader.MaxWait,
CommitInterval: cfg.Reader.CommitInterval,
HeartbeatInterval: cfg.Reader.HeartbeatInterval,
SessionTimeout: cfg.Reader.SessionTimeout,
RebalanceTimeout: cfg.Reader.RebalanceTimeout,
StartOffset: kcronsumer.ToStringOffset(cfg.Reader.StartOffset),
RetentionTime: cfg.Reader.RetentionTime,
},
Producer: kcronsumer.ProducerConfig{
Balancer: cfg.RetryConfiguration.Balancer,
Brokers: cfg.RetryConfiguration.Brokers,
BatchSize: cfg.RetryConfiguration.ProducerBatchSize,
BatchTimeout: cfg.RetryConfiguration.ProducerBatchTimeout,
},
LogLevel: lcronsumer.Level(cfg.RetryConfiguration.LogLevel),
}
if cfg.RetryConfiguration.SkipMessageByHeaderFn != nil {
cronsumerCfg.Consumer.SkipMessageByHeaderFn = func(headers []kcronsumer.Header) bool {
return cfg.RetryConfiguration.SkipMessageByHeaderFn(toHeaders(headers))
}
}
if !cfg.RetryConfiguration.SASL.IsEmpty() {
cronsumerCfg.SASL.Enabled = true
cronsumerCfg.SASL.AuthType = string(cfg.RetryConfiguration.SASL.Type)
cronsumerCfg.SASL.Username = cfg.RetryConfiguration.SASL.Username
cronsumerCfg.SASL.Password = cfg.RetryConfiguration.SASL.Password
cronsumerCfg.SASL.Rack = cfg.RetryConfiguration.Rack
}
if !cfg.RetryConfiguration.TLS.IsEmpty() {
cronsumerCfg.SASL.RootCAPath = cfg.RetryConfiguration.TLS.RootCAPath
cronsumerCfg.SASL.IntermediateCAPath = cfg.RetryConfiguration.TLS.IntermediateCAPath
}
return &cronsumerCfg
}
func (cfg *ConsumerConfig) getTopics() []string {
if len(cfg.Reader.GroupTopics) > 0 {
return cfg.Reader.GroupTopics
}
return []string{cfg.Reader.Topic}
}
type APIConfiguration struct {
// Port default is 8090
Port *int
// HealthCheckPath default is /healthcheck
HealthCheckPath *string
}
type MetricConfiguration struct {
// Path default is /metrics
Path *string
}
type DistributedTracingConfiguration struct {
TracerProvider trace.TracerProvider
Propagator propagation.TextMapPropagator
}
func toHeaders(cronsumerHeaders []kcronsumer.Header) []Header {
headers := make([]Header, 0, len(cronsumerHeaders))
for i := range cronsumerHeaders {
headers = append(headers, Header{
Key: cronsumerHeaders[i].Key,
Value: cronsumerHeaders[i].Value,
})
}
return headers
}
type RetryConfiguration struct {
// MetricPrefix is used for prometheus fq name prefix.
// If not provided, default metric prefix value is `kafka_cronsumer`.
// Currently, there are two exposed prometheus metrics. `retried_messages_total_current` and `discarded_messages_total_current`.
// So, if default metric prefix used, metrics names are `kafka_cronsumer_retried_messages_total_current` and
// `kafka_cronsumer_discarded_messages_total_current`.
MetricPrefix string
SASL *SASLConfig
TLS *TLSConfig
ClientID string
StartTimeCron string
Topic string
DeadLetterTopic string
Rack string
VerifyTopicOnStartup bool
LogLevel LogLevel
Brokers []string
Balancer Balancer
MaxRetry int
WorkDuration time.Duration
SkipMessageByHeaderFn SkipMessageByHeaderFn
Concurrency int
QueueCapacity int
ProducerBatchSize int
ProducerBatchTimeout time.Duration
}
type BatchConfiguration struct {
BatchConsumeFn BatchConsumeFn
PreBatchFn PreBatchFn
MessageGroupLimit int
MessageGroupByteSizeLimit any
}
func (cfg *ConsumerConfig) newKafkaDialer() (*kafka.Dialer, error) {
dialer := &Dialer{
Dialer: &kafka.Dialer{
ClientID: cfg.ClientID,
},
}
if cfg.Dial != nil {
dialer.Dialer.Timeout = cfg.Dial.Timeout
dialer.Dialer.KeepAlive = cfg.Dial.KeepAlive
}
if cfg.SASL == nil && cfg.TLS == nil {
return dialer.Dialer, nil
}
if err := fillLayer(dialer, cfg.SASL, cfg.TLS); err != nil {
return nil, err
}
return dialer.Dialer, nil
}
func (cfg *ConsumerConfig) newKafkaReader() (Reader, error) {
cfg.setDefaults()
dialer, err := cfg.newKafkaDialer()
if err != nil {
return nil, err
}
readerCfg := kafka.ReaderConfig(cfg.Reader)
readerCfg.Dialer = dialer
if cfg.Rack != "" {
readerCfg.GroupBalancers = []kafka.GroupBalancer{kafka.RackAffinityGroupBalancer{Rack: cfg.Rack}}
}
reader := kafka.NewReader(readerCfg)
if cfg.DistributedTracingEnabled {
return NewOtelReaderWrapper(cfg, reader)
}
return NewReaderWrapper(reader), nil
}
func (cfg *ConsumerConfig) setDefaults() {
if cfg.Concurrency == 0 {
cfg.Concurrency = 1
}
if cfg.RetryConfiguration.Concurrency == 0 {
cfg.RetryConfiguration.Concurrency = cfg.Concurrency
}
if cfg.CommitInterval == 0 {
cfg.CommitInterval = time.Second
// Kafka-go library default value is 0, we need to also change this.
cfg.Reader.CommitInterval = time.Second
} else {
cfg.Reader.CommitInterval = cfg.CommitInterval
}
if cfg.MessageGroupDuration == 0 {
cfg.MessageGroupDuration = time.Second
}
if cfg.BatchConfiguration != nil && cfg.BatchConfiguration.MessageGroupLimit == 0 {
cfg.BatchConfiguration.MessageGroupLimit = 100
}
if cfg.DistributedTracingEnabled {
if cfg.DistributedTracingConfiguration.Propagator == nil {
cfg.DistributedTracingConfiguration.Propagator = otel.GetTextMapPropagator()
}
if cfg.DistributedTracingConfiguration.TracerProvider == nil {
cfg.DistributedTracingConfiguration.TracerProvider = otel.GetTracerProvider()
}
}
if cfg.TransactionalRetry == nil {
cfg.TransactionalRetry = NewBoolPtr(true)
}
}
func NewBoolPtr(value bool) *bool {
return &value
}
func jsonPretty(jsonString string) string {
var out bytes.Buffer
err := json.Indent(&out, []byte(jsonString), "", "\t")
if err != nil {
return jsonString
}
return out.String()
}