-
Notifications
You must be signed in to change notification settings - Fork 13
/
producer_config.go
106 lines (91 loc) · 3.03 KB
/
producer_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
package kafka
import (
"fmt"
"regexp"
"strings"
"time"
"go.opentelemetry.io/otel"
"github.com/segmentio/kafka-go"
)
type WriterConfig struct {
ErrorLogger kafka.Logger
Logger kafka.Logger
Balancer kafka.Balancer
Completion func(messages []kafka.Message, err error)
Topic string
Brokers []string
ReadTimeout time.Duration
BatchTimeout time.Duration
BatchBytes int64
WriteTimeout time.Duration
RequiredAcks kafka.RequiredAcks
BatchSize int
WriteBackoffMax time.Duration
WriteBackoffMin time.Duration
MaxAttempts int
Async bool
Compression kafka.Compression
AllowAutoTopicCreation bool
}
func (cfg WriterConfig) JSON() string {
return fmt.Sprintf(`{"Brokers": ["%s"], "Balancer": %q, "Compression": %q}`,
strings.Join(cfg.Brokers, "\", \""), GetBalancerString(cfg.Balancer), cfg.Compression.String())
}
type TransportConfig struct {
MetadataTopics []string
DialTimeout time.Duration
IdleTimeout time.Duration
MetadataTTL time.Duration
}
type ProducerConfig struct {
DistributedTracingConfiguration DistributedTracingConfiguration
Transport *TransportConfig
SASL *SASLConfig
TLS *TLSConfig
ClientID string
Writer WriterConfig
DistributedTracingEnabled bool
}
func (cfg *ProducerConfig) String() string {
re := regexp.MustCompile(`"(\w+)"\s*:`)
modifiedString := re.ReplaceAllString(cfg.JSON(), `$1:`)
modifiedString = modifiedString[1 : len(modifiedString)-1]
return modifiedString
}
func (cfg *ProducerConfig) JSON() string {
if cfg == nil {
return "{}"
}
return fmt.Sprintf(`{"Writer": %s, "ClientID": %q, "DistributedTracingEnabled": %t, "SASL": %s, "TLS": %s}`,
cfg.Writer.JSON(), cfg.ClientID, cfg.DistributedTracingEnabled, cfg.SASL.JSON(), cfg.TLS.JSON())
}
func (cfg *ProducerConfig) JSONPretty() string {
return jsonPretty(cfg.JSON())
}
func (cfg *ProducerConfig) newKafkaTransport() (*kafka.Transport, error) {
transport := &Transport{
Transport: &kafka.Transport{
ClientID: cfg.ClientID,
},
}
if cfg.Transport != nil {
transport.Transport.DialTimeout = cfg.Transport.DialTimeout
transport.Transport.IdleTimeout = cfg.Transport.IdleTimeout
transport.Transport.MetadataTTL = cfg.Transport.MetadataTTL
transport.Transport.MetadataTopics = cfg.Transport.MetadataTopics
}
if err := fillLayer(transport, cfg.SASL, cfg.TLS); err != nil {
return nil, err
}
return transport.Transport, nil
}
func (cfg *ProducerConfig) setDefaults() {
if cfg.DistributedTracingEnabled {
if cfg.DistributedTracingConfiguration.TracerProvider == nil {
cfg.DistributedTracingConfiguration.TracerProvider = otel.GetTracerProvider()
}
if cfg.DistributedTracingConfiguration.Propagator == nil {
cfg.DistributedTracingConfiguration.Propagator = otel.GetTextMapPropagator()
}
}
}