forked from QubitProducts/kafka_lag_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzookeeper.go
182 lines (157 loc) · 5.37 KB
/
zookeeper.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
/* Copyright 2015 LinkedIn Corp. Licensed 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.
*/
package main
import (
"net"
"strconv"
"sync"
"time"
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
"github.com/samuel/go-zookeeper/zk"
)
type ZookeeperExporter struct {
cfg *zkConfig
cluster string
conn *zk.Conn
zkGroupLock sync.RWMutex
zkGroupList map[string]bool
coffsetDesc *prometheus.Desc
}
func NewZookeeperExporter(cluster string, desc *prometheus.Desc, cfg *zkConfig) (*ZookeeperExporter, error) {
dialer := func(network, address string, timeout time.Duration) (net.Conn, error) {
conn, err := net.DialTimeout(network, address, timeout)
if tconn, ok := conn.(*net.TCPConn); conn != nil && ok {
tconn.SetKeepAlive(true)
tconn.SetKeepAlivePeriod(keepAlive)
}
return conn, err
}
to := defaultTimeout
if cfg.Timeout != nil {
to = *cfg.Timeout
}
zkconn, _, err := zk.ConnectWithDialer(cfg.Quorum, to, dialer)
if err != nil {
return nil, err
}
client := &ZookeeperExporter{
cfg: cfg,
cluster: cluster,
conn: zkconn,
zkGroupLock: sync.RWMutex{},
zkGroupList: make(map[string]bool),
coffsetDesc: desc,
}
return client, nil
}
func (zkClient *ZookeeperExporter) Collect(ch chan<- prometheus.Metric) {
zkClient.refreshConsumerGroups()
// Make sure this group still exists
zkClient.zkGroupLock.RLock()
defer zkClient.zkGroupLock.RUnlock()
wg := sync.WaitGroup{}
for g := range zkClient.zkGroupList {
wg.Add(1)
go func(g string) {
defer wg.Done()
zkClient.collectOffsetsForConsumerGroup(ch, g)
}(g)
}
wg.Wait()
}
func (zkClient *ZookeeperExporter) refreshConsumerGroups() {
zkClient.zkGroupLock.Lock()
defer zkClient.zkGroupLock.Unlock()
consumerGroups, _, err := zkClient.conn.Children(zkClient.cfg.Path + "/consumers")
if err != nil {
// Can't read the consumers path. Bail for now
glog.Errorf("Cannot get consumer group list for cluster %s: %s %s", zkClient.cluster, zkClient.cfg.Path+"/consumers", err)
return
}
// Mark all existing groups false
for consumerGroup := range zkClient.zkGroupList {
zkClient.zkGroupList[consumerGroup] = false
}
// Check for new groups, mark existing groups true
for _, consumerGroup := range consumerGroups {
// Don't bother adding groups in the blacklist
// if !zkClient.app.Storage.AcceptConsumerGroup(consumerGroup) {
// continue
// }
zkClient.zkGroupList[consumerGroup] = true
}
// Delete groups that are still false
for consumerGroup := range zkClient.zkGroupList {
if !zkClient.zkGroupList[consumerGroup] {
glog.Infof("Remove ZK consumer group %s from cluster %s", consumerGroup, zkClient.cluster)
delete(zkClient.zkGroupList, consumerGroup)
}
}
}
func (zkClient *ZookeeperExporter) collectOffsetsForConsumerGroup(ch chan<- prometheus.Metric, consumerGroup string) {
topics, _, err := zkClient.conn.Children(zkClient.cfg.Path + "/consumers/" + consumerGroup + "/offsets")
switch {
case err == nil:
wg := sync.WaitGroup{}
for _, topic := range topics {
wg.Add(1)
go func(t string) {
defer wg.Done()
zkClient.getOffsetsForTopic(ch, consumerGroup, t)
}(topic)
}
wg.Wait()
case err == zk.ErrNoNode:
// If the node doesn't exist, it may be because the group is using Kafka-committed offsets. Skip it
glog.Infof("Skip checking ZK offsets for group %s in cluster %s as the offsets path doesn't exist", consumerGroup, zkClient.cluster)
default:
glog.Infof("Cannot read topics for group %s in cluster %s: %s", consumerGroup, zkClient.cluster, err)
}
}
func (zkClient *ZookeeperExporter) getOffsetsForTopic(ch chan<- prometheus.Metric, consumerGroup string, topic string) {
partitions, _, err := zkClient.conn.Children(zkClient.cfg.Path + "/consumers/" + consumerGroup + "/offsets/" + topic)
if err != nil {
glog.Infof("Cannot read partitions for topic %s for group %s in cluster %s: %s", topic, consumerGroup, zkClient.cluster, err)
return
}
wg := sync.WaitGroup{}
for _, partition := range partitions {
wg.Add(1)
go func(p string) {
defer wg.Done()
zkClient.getOffsetForPartition(ch, consumerGroup, topic, p)
}(partition)
}
wg.Wait()
}
func (zkClient *ZookeeperExporter) getOffsetForPartition(ch chan<- prometheus.Metric, consumerGroup string, topic string, partition string) {
offsetStr, zkNodeStat, err := zkClient.conn.Get(zkClient.cfg.Path + "/consumers/" + consumerGroup + "/offsets/" + topic + "/" + partition)
if err != nil {
glog.Infof("Failed to read partition %s:%v for group %s in cluster %s: %s", topic, partition, consumerGroup, zkClient.cluster, err)
return
}
if false {
glog.Infof("%+v", zkNodeStat)
}
offset, err := strconv.ParseInt(string(offsetStr), 10, 64)
if err != nil {
glog.Errorf("Offset value (%s) for partition %s:%v for group %s in cluster %s is not an integer", string(offsetStr), topic, partition, consumerGroup, zkClient.cluster)
return
}
ch <- prometheus.MustNewConstMetric(
zkClient.coffsetDesc,
prometheus.GaugeValue,
float64(offset),
consumerGroup,
topic,
partition,
)
}