-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpoller.go
70 lines (59 loc) · 1.4 KB
/
poller.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
package main
import (
"log"
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
)
type zkPoller struct {
interval time.Duration
metrics map[string]*prometheus.GaugeVec
zk *zooKeeper
}
func newPoller(interval time.Duration, metrics map[string]*prometheus.GaugeVec, zk *zooKeeper) *zkPoller {
return &zkPoller{
interval: interval,
metrics: metrics,
zk: zk,
}
}
func (p *zkPoller) pollForMetrics() {
for {
log.Printf("poller: polling zookeeper [%v] for metrics\n", p.zk.addr)
m, err := p.zk.fetchStats()
if err != nil {
log.Printf("poller: failed to fetch stats, err=%v\n", err)
}
p.refreshMetrics(m)
<-time.After(p.interval)
}
}
func (p *zkPoller) refreshMetrics(updated map[string]string) {
for name, value := range updated {
metric, ok := p.metrics[name]
if !ok {
log.Printf("poller: couldn't find metric for stat=%v\n", name)
continue
}
if name == zkOK {
switch value {
case "imok":
metric.WithLabelValues(p.zk.addr).Set(1)
default:
metric.WithLabelValues(p.zk.addr).Set(0)
}
continue
}
if name == zkServerState {
state := getState(value)
metric.WithLabelValues(p.zk.addr).Set(float64(state))
continue
}
f, err := strconv.ParseFloat(value, 64)
if err != nil {
log.Printf("poller: failed to convert string value to float, value=%v\n", value)
continue
}
metric.WithLabelValues(p.zk.addr).Set(f)
}
}