-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
config.go
273 lines (238 loc) · 8.68 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
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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package kafka
import (
"errors"
"fmt"
"time"
"github.com/Shopify/sarama"
"github.com/elastic/beats/v7/libbeat/common/cfgwarn"
"github.com/elastic/beats/v7/libbeat/common/kafka"
"github.com/elastic/beats/v7/libbeat/common/transport/kerberos"
"github.com/elastic/beats/v7/libbeat/reader/parser"
"github.com/elastic/elastic-agent-libs/monitoring"
"github.com/elastic/elastic-agent-libs/monitoring/adapter"
"github.com/elastic/elastic-agent-libs/transport/tlscommon"
)
type kafkaInputConfig struct {
// Kafka hosts with port, e.g. "localhost:9092"
Hosts []string `config:"hosts" validate:"required"`
Topics []string `config:"topics" validate:"required"`
GroupID string `config:"group_id" validate:"required"`
ClientID string `config:"client_id"`
Version kafka.Version `config:"version"`
InitialOffset initialOffset `config:"initial_offset"`
ConnectBackoff time.Duration `config:"connect_backoff" validate:"min=0"`
ConsumeBackoff time.Duration `config:"consume_backoff" validate:"min=0"`
WaitClose time.Duration `config:"wait_close" validate:"min=0"`
MaxWaitTime time.Duration `config:"max_wait_time"`
IsolationLevel isolationLevel `config:"isolation_level"`
Fetch kafkaFetch `config:"fetch"`
Rebalance kafkaRebalance `config:"rebalance"`
TLS *tlscommon.Config `config:"ssl"`
Kerberos *kerberos.Config `config:"kerberos"`
Username string `config:"username"`
Password string `config:"password"`
Sasl kafka.SaslConfig `config:"sasl"`
ExpandEventListFromField string `config:"expand_event_list_from_field"`
Parsers parser.Config `config:",inline"`
}
type kafkaFetch struct {
Min int32 `config:"min" validate:"min=1"`
Default int32 `config:"default" validate:"min=1"`
Max int32 `config:"max" validate:"min=0"`
}
type kafkaRebalance struct {
Strategy rebalanceStrategy `config:"strategy"`
Timeout time.Duration `config:"timeout"`
MaxRetries int `config:"max_retries"`
RetryBackoff time.Duration `config:"retry_backoff" validate:"min=0"`
}
type initialOffset int
const (
initialOffsetOldest initialOffset = iota
initialOffsetNewest
)
type rebalanceStrategy int
const (
rebalanceStrategyRange rebalanceStrategy = iota
rebalanceStrategyRoundRobin
)
type isolationLevel int
const (
isolationLevelReadUncommitted = iota
isolationLevelReadCommitted
)
var (
initialOffsets = map[string]initialOffset{
"oldest": initialOffsetOldest,
"newest": initialOffsetNewest,
}
rebalanceStrategies = map[string]rebalanceStrategy{
"range": rebalanceStrategyRange,
"roundrobin": rebalanceStrategyRoundRobin,
}
isolationLevels = map[string]isolationLevel{
"read_uncommitted": isolationLevelReadUncommitted,
"read_committed": isolationLevelReadCommitted,
}
)
// The default config for the kafka input. When in doubt, default values
// were chosen to match sarama's defaults.
func defaultConfig() kafkaInputConfig {
return kafkaInputConfig{
Version: kafka.Version("1.0.0"),
InitialOffset: initialOffsetOldest,
ClientID: "filebeat",
ConnectBackoff: 30 * time.Second,
ConsumeBackoff: 2 * time.Second,
WaitClose: 2 * time.Second,
MaxWaitTime: 250 * time.Millisecond,
IsolationLevel: isolationLevelReadUncommitted,
Fetch: kafkaFetch{
Min: 1,
Default: (1 << 20), // 1 MB
Max: 0,
},
Rebalance: kafkaRebalance{
Strategy: rebalanceStrategyRange,
Timeout: 60 * time.Second,
MaxRetries: 4,
RetryBackoff: 2 * time.Second,
},
}
}
// Validate validates the config.
func (c *kafkaInputConfig) Validate() error {
if len(c.Hosts) == 0 {
return errors.New("no hosts configured")
}
if err := c.Version.Validate(); err != nil {
return err
}
if c.Username != "" && c.Password == "" {
return fmt.Errorf("password must be set when username is configured")
}
return nil
}
func newSaramaConfig(config kafkaInputConfig) (*sarama.Config, error) {
k := sarama.NewConfig()
version, ok := config.Version.Get()
if !ok {
return nil, fmt.Errorf("unknown/unsupported kafka version: %v", config.Version)
}
k.Version = version
k.Consumer.Return.Errors = true
k.Consumer.Offsets.Initial = config.InitialOffset.asSaramaOffset()
k.Consumer.Retry.Backoff = config.ConsumeBackoff
k.Consumer.MaxWaitTime = config.MaxWaitTime
k.Consumer.IsolationLevel = config.IsolationLevel.asSaramaIsolationLevel()
k.Consumer.Fetch.Min = config.Fetch.Min
k.Consumer.Fetch.Default = config.Fetch.Default
k.Consumer.Fetch.Max = config.Fetch.Max
k.Consumer.Group.Rebalance.Strategy = config.Rebalance.Strategy.asSaramaStrategy()
k.Consumer.Group.Rebalance.Timeout = config.Rebalance.Timeout
k.Consumer.Group.Rebalance.Retry.Backoff = config.Rebalance.RetryBackoff
k.Consumer.Group.Rebalance.Retry.Max = config.Rebalance.MaxRetries
tls, err := tlscommon.LoadTLSConfig(config.TLS)
if err != nil {
return nil, err
}
if tls != nil {
k.Net.TLS.Enable = true
k.Net.TLS.Config = tls.BuildModuleClientConfig("")
}
if config.Kerberos.IsEnabled() {
cfgwarn.Beta("Kerberos authentication for Kafka is beta.")
k.Net.SASL.Enable = true
k.Net.SASL.Mechanism = sarama.SASLTypeGSSAPI
k.Net.SASL.GSSAPI = sarama.GSSAPIConfig{
AuthType: int(config.Kerberos.AuthType),
KeyTabPath: config.Kerberos.KeyTabPath,
KerberosConfigPath: config.Kerberos.ConfigPath,
ServiceName: config.Kerberos.ServiceName,
Username: config.Kerberos.Username,
Password: config.Kerberos.Password,
Realm: config.Kerberos.Realm,
DisablePAFXFAST: !config.Kerberos.EnableFAST,
}
} else if config.Username != "" {
k.Net.SASL.Enable = true
k.Net.SASL.User = config.Username
k.Net.SASL.Password = config.Password
config.Sasl.ConfigureSarama(k)
}
// configure client ID
k.ClientID = config.ClientID
k.MetricRegistry = adapter.GetGoMetrics(
monitoring.Default,
"filebeat.inputs.kafka",
adapter.Rename("incoming-byte-rate", "bytes_read"),
adapter.Rename("outgoing-byte-rate", "bytes_write"),
adapter.GoMetricsNilify,
)
if err := k.Validate(); err != nil {
return nil, err
}
return k, nil
}
// asSaramaOffset converts an initialOffset enum to the corresponding
// sarama offset value.
func (off initialOffset) asSaramaOffset() int64 {
return map[initialOffset]int64{
initialOffsetOldest: sarama.OffsetOldest,
initialOffsetNewest: sarama.OffsetNewest,
}[off]
}
// Unpack validates and unpack the "initial_offset" config option
func (off *initialOffset) Unpack(value string) error {
initialOffset, ok := initialOffsets[value]
if !ok {
return fmt.Errorf("invalid initialOffset '%s'", value)
}
*off = initialOffset
return nil
}
func (st rebalanceStrategy) asSaramaStrategy() sarama.BalanceStrategy {
return map[rebalanceStrategy]sarama.BalanceStrategy{
rebalanceStrategyRange: sarama.BalanceStrategyRange,
rebalanceStrategyRoundRobin: sarama.BalanceStrategyRoundRobin,
}[st]
}
// Unpack validates and unpack the "rebalance.strategy" config option
func (st *rebalanceStrategy) Unpack(value string) error {
strategy, ok := rebalanceStrategies[value]
if !ok {
return fmt.Errorf("invalid rebalance strategy '%s'", value)
}
*st = strategy
return nil
}
func (is isolationLevel) asSaramaIsolationLevel() sarama.IsolationLevel {
return map[isolationLevel]sarama.IsolationLevel{
isolationLevelReadUncommitted: sarama.ReadUncommitted,
isolationLevelReadCommitted: sarama.ReadCommitted,
}[is]
}
func (is *isolationLevel) Unpack(value string) error {
isolationLevel, ok := isolationLevels[value]
if !ok {
return fmt.Errorf("invalid isolation level '%s'", value)
}
*is = isolationLevel
return nil
}