-
Notifications
You must be signed in to change notification settings - Fork 14
/
collector.go
167 lines (153 loc) · 4.7 KB
/
collector.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
package main
import (
"strings"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
kazoo "github.com/wvanbergen/kazoo-go"
)
type zkMetrics struct {
topicPartitions *prometheus.Desc
partitionUsesPreferredReplica *prometheus.Desc
partitionLeader *prometheus.Desc
partitionReplicaCount *prometheus.Desc
partitionISR *prometheus.Desc
controller *prometheus.Desc
consumersOffsets *prometheus.Desc
zookeeperScrapeError *prometheus.Desc
}
type collector struct {
zookeeper string
chroot string
topics []string
consumers []string
timeout time.Duration
zkErr int32
metrics zkMetrics
}
func newCollector(zookeeper string, chroot string, topics []string, consumers []string) *collector {
return &collector{
zookeeper: zookeeper,
chroot: chroot,
topics: topics,
consumers: consumers,
timeout: *zkTimeout,
zkErr: 0,
metrics: zkMetrics{
topicPartitions: prometheus.NewDesc(
"kafka_topic_partition_count",
"Number of partitions on this topic",
[]string{"topic"},
prometheus.Labels{},
),
partitionUsesPreferredReplica: prometheus.NewDesc(
"kafka_topic_partition_leader_is_preferred",
"1 if partition is using the preferred broker",
[]string{"topic", "partition"},
prometheus.Labels{},
),
partitionLeader: prometheus.NewDesc(
"kafka_topic_partition_leader",
"1 if the node is the leader of this partition",
[]string{"topic", "partition", "replica"},
prometheus.Labels{},
),
partitionReplicaCount: prometheus.NewDesc(
"kafka_topic_partition_replica_count",
"Total number of replicas for this partition",
[]string{"topic", "partition"},
prometheus.Labels{},
),
partitionISR: prometheus.NewDesc(
"kafka_topic_partition_replica_in_sync",
"1 if replica is in sync",
[]string{"topic", "partition", "replica"},
prometheus.Labels{},
),
controller: prometheus.NewDesc(
"kafka_broker_is_controller",
"1 if the broker is the controller of this cluster",
[]string{"broker"},
prometheus.Labels{},
),
consumersOffsets: prometheus.NewDesc(
"kafka_consumers_offsets",
"Last offset consumed",
[]string{"consumer", "topic", "partition"},
prometheus.Labels{},
),
zookeeperScrapeError: prometheus.NewDesc(
"kafka_zookeeper_scrape_error",
"1 if there were any errors retrieving data for this scrape, 0 otherwise",
[]string{},
prometheus.Labels{},
),
},
}
}
func (c *collector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.metrics.topicPartitions
ch <- c.metrics.partitionUsesPreferredReplica
ch <- c.metrics.partitionLeader
ch <- c.metrics.partitionReplicaCount
ch <- c.metrics.partitionISR
ch <- c.metrics.controller
ch <- c.metrics.consumersOffsets
ch <- c.metrics.zookeeperScrapeError
}
func (c *collector) Collect(ch chan<- prometheus.Metric) {
config := kazoo.Config{
Chroot: c.chroot,
Timeout: c.timeout,
}
log.Debugf("Connecting to %s, chroot=%s timeout=%s", c.zookeeper, config.Chroot, config.Timeout)
client, err := kazoo.NewKazoo(strings.Split(c.zookeeper, ","), &config)
if err != nil {
log.Errorf("Connection error: %s", err)
// If we can't connect, there's nothing left to do but return the error to the client
ch <- prometheus.MustNewConstMetric(c.metrics.zookeeperScrapeError, prometheus.GaugeValue, 1)
return
}
defer client.Close()
c.clusterMetrics(ch, client)
wg := sync.WaitGroup{}
topics, err := client.Topics()
if err != nil {
log.Errorf("Error collecting list of topics: %s", err)
c.zkErr = 1
} else {
for _, topic := range topics {
if len(c.topics) > 0 && !stringInSlice(topic.Name, c.topics) {
// skip topic if it's not on the list of topic to collect
log.Debugf("Skipping topic '%s', not in list: %s [%d]", topic.Name, c.topics, len(c.topics))
} else {
wg.Add(1)
go func(t *kazoo.Topic) {
defer wg.Done()
c.topicMetrics(ch, t)
}(topic)
}
}
}
consumers, err := client.Consumergroups()
if err != nil {
log.Errorf("Error collecting list of consumers: %s", err)
c.zkErr = 1
} else {
for _, consumer := range consumers {
if len(c.consumers) > 0 && !stringInSlice(consumer.Name, c.consumers) {
// skip consumer if it's not on the list of consumers to collect
log.Debugf("Skipping consumer '%s', not in list: %s [%d]", consumer.Name, c.consumers, len(c.consumers))
} else {
wg.Add(1)
go func(cg *kazoo.Consumergroup) {
defer wg.Done()
c.consumerMetrics(ch, cg)
}(consumer)
}
}
}
wg.Wait()
ch <- prometheus.MustNewConstMetric(c.metrics.zookeeperScrapeError, prometheus.GaugeValue, float64(c.zkErr))
}