-
Notifications
You must be signed in to change notification settings - Fork 13
/
config.go
87 lines (70 loc) · 2.11 KB
/
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
package kafka
import (
"fmt"
"github.com/Shopify/sarama"
"github.com/circonus-labs/circonus-unified-agent/plugins/common/tls"
)
// ReadConfig for kafka clients meaning to read from Kafka.
type ReadConfig struct {
Config
}
// SetConfig on the sarama.Config object from the ReadConfig struct.
func (k *ReadConfig) SetConfig(config *sarama.Config) error {
config.Consumer.Return.Errors = true
return k.Config.SetConfig(config)
}
// WriteConfig for kafka clients meaning to write to kafka
type WriteConfig struct {
Config
RequiredAcks int `toml:"required_acks"`
MaxRetry int `toml:"max_retry"`
MaxMessageBytes int `toml:"max_message_bytes"`
IdempotentWrites bool `toml:"idempotent_writes"`
}
// SetConfig on the sarama.Config object from the WriteConfig struct.
func (k *WriteConfig) SetConfig(config *sarama.Config) error {
config.Producer.Return.Successes = true
config.Producer.Idempotent = k.IdempotentWrites
config.Producer.Retry.Max = k.MaxRetry
if k.MaxMessageBytes > 0 {
config.Producer.MaxMessageBytes = k.MaxMessageBytes
}
config.Producer.RequiredAcks = sarama.RequiredAcks(k.RequiredAcks)
return k.Config.SetConfig(config)
}
// Config common to all Kafka clients.
type Config struct {
tls.ClientConfig
Version string `toml:"version"`
ClientID string `toml:"client_id"`
SASLAuth
CompressionCodec int `toml:"compression_codec"`
}
// SetConfig on the sarama.Config object from the Config struct.
func (k *Config) SetConfig(config *sarama.Config) error {
if k.Version != "" {
version, err := sarama.ParseKafkaVersion(k.Version)
if err != nil {
return fmt.Errorf("parse kafka ver (%s): %w", k.Version, err)
}
config.Version = version
}
if k.ClientID != "" {
config.ClientID = k.ClientID
} else {
config.ClientID = "Circonus"
}
config.Producer.Compression = sarama.CompressionCodec(k.CompressionCodec)
tlsConfig, err := k.ClientConfig.TLSConfig()
if err != nil {
return fmt.Errorf("TLSConfig: %w", err)
}
if tlsConfig != nil {
config.Net.TLS.Config = tlsConfig
config.Net.TLS.Enable = true
}
if err := k.SetSASLConfig(config); err != nil {
return err
}
return nil
}