-
Notifications
You must be signed in to change notification settings - Fork 37
/
kafka.go
339 lines (298 loc) · 9.47 KB
/
kafka.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
339
package main
import (
"errors"
"math/rand"
"regexp"
"strconv"
"sync"
"time"
"github.com/Shopify/sarama"
log "github.com/Sirupsen/logrus"
"github.com/prometheus/client_golang/prometheus"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
type scrapeConfig struct {
FetchMinInterval time.Duration
FetchMaxInterval time.Duration
MetadataRefreshInterval time.Duration
TopicsFilter *regexp.Regexp
GroupsFilter *regexp.Regexp
}
func mustNewScrapeConfig(refresh time.Duration, fetchMin time.Duration, fetchMax time.Duration, topics string, groups string) scrapeConfig {
topicsFilter, err := regexp.Compile(topics)
if err != nil {
log.Fatal(err)
}
// empty group should match nothing instead of everything
if groups == "" {
groups = ".^"
}
groupsFilter, err := regexp.Compile(groups)
if err != nil {
log.Fatal(err)
}
return scrapeConfig{
FetchMinInterval: fetchMin,
FetchMaxInterval: fetchMax,
MetadataRefreshInterval: refresh,
TopicsFilter: topicsFilter,
GroupsFilter: groupsFilter,
}
}
func startKafkaScraper(wg *sync.WaitGroup, shutdown chan struct{}, kafka sarama.Client, cfg scrapeConfig) {
go refreshMetadataPeriodically(wg, shutdown, kafka, cfg)
for _, broker := range kafka.Brokers() {
go manageBroker(wg, shutdown, broker, kafka, cfg)
}
}
func refreshMetadataPeriodically(wg *sync.WaitGroup, shutdown chan struct{}, kafka sarama.Client, cfg scrapeConfig) {
wg.Add(1)
defer wg.Done()
log.WithField("interval", cfg.MetadataRefreshInterval.String()).
Info("Starting metadata refresh thread")
wait := time.After(0)
for {
select {
case <-wait:
log.Debug("Refreshing cluster metadata")
if err := kafka.RefreshMetadata(); err != nil {
log.WithField("error", err).Warn("Failed to update cluster metadata")
}
case <-shutdown:
log.Info("Shutting down metadata refresh thread")
return
}
wait = time.After(cfg.MetadataRefreshInterval)
}
}
func manageBroker(wg *sync.WaitGroup, shutdown chan struct{}, broker *sarama.Broker, kafka sarama.Client, cfg scrapeConfig) {
wg.Add(1)
defer wg.Done()
log.WithField("broker", broker.Addr()).
WithField("interval.min", cfg.FetchMinInterval.String()).
WithField("interval.max", cfg.FetchMaxInterval.String()).
Info("Starting handler for broker")
wait := time.After(0)
for {
select {
case <-wait:
log.WithField("broker", broker.Addr()).Debug("Updating metrics")
// ensure broker is connected
if err := connect(broker); err != nil {
log.WithField("broker", broker.Addr()).
WithField("error", err).
Error("Failed to connect to broker")
break
}
// fetch groups coordinated by this broker
var groups []string
groupsResponse, err := broker.ListGroups(&sarama.ListGroupsRequest{})
if err != nil {
log.WithField("broker", broker.Addr()).
WithField("error", err).
Error("Failed to retrieve consumer groups")
} else if groupsResponse.Err != sarama.ErrNoError {
log.WithField("broker", broker.Addr()).
WithField("error", groupsResponse.Err).
Error("Failed to retrieve consumer groups")
} else {
for group := range groupsResponse.Groups {
if !cfg.GroupsFilter.MatchString(group) {
log.WithField("group", group).Info("not found group")
continue
}
groupCoordinator, err := kafka.Coordinator(group)
if err != nil {
log.WithField("broker", broker.Addr()).
WithField("group", group).
WithField("error", err).
Warn("Failed to identify broker for consumer group")
continue
}
if broker == groupCoordinator {
groups = append(groups, group)
}
}
}
if len(groups) == 0 {
log.WithField("broker", broker.Addr()).Debug("No consumer groups to fetch offsets for")
}
// build requests
partitionCount := 0
oldestRequest := sarama.OffsetRequest{}
newestRequest := sarama.OffsetRequest{}
var groupRequests []sarama.OffsetFetchRequest
for _, group := range groups {
groupRequests = append(groupRequests, sarama.OffsetFetchRequest{ConsumerGroup: group, Version: 1})
}
// fetch partitions led by this broker, and add all partitions to group requests
topics, err := kafka.Topics()
if err != nil {
log.WithField("broker", broker.Addr()).
WithField("error", err).
Error("Failed to get topics")
break
}
for _, topic := range topics {
if !cfg.TopicsFilter.MatchString(topic) {
continue
}
partitions, err := kafka.Partitions(topic)
if err != nil {
log.WithField("broker", broker.Addr()).
WithField("topic", topic).
WithField("error", err).
Warn("Failed to get partitions for topic")
continue
}
for _, partition := range partitions {
// all partitions need to be added to group requests
for i := range groupRequests {
groupRequests[i].AddPartition(topic, partition)
}
partitionLeader, err := kafka.Leader(topic, partition)
if err != nil {
log.WithField("broker", broker.Addr()).
WithField("topic", topic).
WithField("partition", partition).
WithField("error", err).
Warn("Failed to identify broker for partition")
continue
}
if broker == partitionLeader {
oldestRequest.AddBlock(topic, partition, sarama.OffsetOldest, 1)
newestRequest.AddBlock(topic, partition, sarama.OffsetNewest, 1)
partitionCount++
}
}
}
if partitionCount == 0 {
log.WithField("broker", broker.Addr()).Debug("No partitions for broker to fetch")
}
log.WithField("broker", broker.Addr()).
WithField("partition.count", partitionCount).
WithField("group.count", len(groupRequests)).
Debug("Sending requests")
requestWG := &sync.WaitGroup{}
requestWG.Add(2 + len(groupRequests))
go func() {
defer requestWG.Done()
handleTopicOffsetRequest(broker, &oldestRequest, "oldest", metricOffsetOldest)
}()
go func() {
defer requestWG.Done()
handleTopicOffsetRequest(broker, &newestRequest, "newest", metricOffsetNewest)
}()
for i := range groupRequests {
go func(request *sarama.OffsetFetchRequest) {
defer requestWG.Done()
handleGroupOffsetRequest(broker, request, metricOffsetConsumer)
}(&groupRequests[i])
}
requestWG.Wait()
case <-shutdown:
log.WithField("broker", broker.Addr()).Info("Shutting down handler for broker")
return
}
min := int64(cfg.FetchMinInterval)
max := int64(cfg.FetchMaxInterval)
duration := time.Duration(rand.Int63n(max-min) + min)
wait = time.After(duration)
log.WithField("broker", broker.Addr()).
WithField("interval.rand", duration.String()).
Debug("Updated metrics and waiting for next run")
}
}
func connect(broker *sarama.Broker) error {
if ok, _ := broker.Connected(); ok {
return nil
}
cfg := sarama.NewConfig()
cfg.Version = sarama.V0_10_0_0
if err := broker.Open(cfg); err != nil {
return err
}
if connected, err := broker.Connected(); err != nil {
return err
} else if !connected {
return errors.New("Unknown failure")
}
return nil
}
func handleGroupOffsetRequest(broker *sarama.Broker, request *sarama.OffsetFetchRequest, metric *prometheus.GaugeVec) {
response, err := broker.FetchOffset(request)
if err != nil {
log.WithField("broker", broker.Addr()).
WithField("group", request.ConsumerGroup).
WithField("error", err).
Error("Failed to request group offsets")
return
}
for topic, partitions := range response.Blocks {
for partition, block := range partitions {
if block == nil {
log.WithField("broker", broker.Addr()).
WithField("group", request.ConsumerGroup).
WithField("topic", topic).
WithField("partition", partition).
Warn("Failed to get data for group")
continue
} else if block.Err != sarama.ErrNoError {
log.WithField("broker", broker.Addr()).
WithField("group", request.ConsumerGroup).
WithField("topic", topic).
WithField("partition", partition).
WithField("error", block.Err).
Warn("Failed getting data for group")
continue
} else if block.Offset < 0 {
continue
}
metric.With(prometheus.Labels{
"topic": topic,
"partition": strconv.Itoa(int(partition)),
"group": request.ConsumerGroup,
}).Set(float64(block.Offset))
}
}
}
func handleTopicOffsetRequest(broker *sarama.Broker, request *sarama.OffsetRequest, offsetType string, metric *prometheus.GaugeVec) {
response, err := broker.GetAvailableOffsets(request)
if err != nil {
log.WithField("broker", broker.Addr()).
WithField("offset.type", offsetType).
WithField("error", err).
Error("Failed to request topic offsets")
return
}
for topic, partitions := range response.Blocks {
for partition, block := range partitions {
if block == nil {
log.WithField("broker", broker.Addr()).
WithField("topic", topic).
WithField("partition", partition).
Warn("Failed to get data for partition")
continue
} else if block.Err != sarama.ErrNoError {
log.WithField("broker", broker.Addr()).
WithField("topic", topic).
WithField("partition", partition).
WithField("error", block.Err).
Warn("Failed getting offsets for partition")
continue
} else if len(block.Offsets) != 1 {
log.WithField("broker", broker.Addr()).
WithField("topic", topic).
WithField("partition", partition).
Warn("Got unexpected offset data for partition")
continue
}
metric.With(prometheus.Labels{
"topic": topic,
"partition": strconv.Itoa(int(partition)),
}).Set(float64(block.Offsets[0]))
}
}
}