-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
190 lines (156 loc) · 6.58 KB
/
metrics.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
package main
import (
"github.com/prometheus/client_golang/prometheus"
)
type serverState float64
const (
// hard codes metric names from mntr command output
zkAvgLatency = "zk_avg_latency"
zkMinLatency = "zk_min_latency"
zkMaxLatency = "zk_max_latency"
zkPacketsReceived = "zk_packets_received"
zkPacketsSent = "zk_packets_sent"
zkNumAliveConnections = "zk_num_alive_connections"
zkOutstandingRequests = "zk_outstanding_requests"
zkZnodeCount = "zk_znode_count"
zkWatchCount = "zk_watch_count"
zkEphemeralsCount = "zk_ephemerals_count"
zkApproximateDataSize = "zk_approximate_data_size"
zkOpenFileDescriptorCount = "zk_open_file_descriptor_count"
zkMaxFileDescriptorCount = "zk_max_file_descriptor_count"
zkFollowers = "zk_followers"
zkSyncedFollowers = "zk_synced_followers"
zkPendingSyncs = "zk_pending_syncs"
zkServerState = "zk_server_state"
zkFsyncThresholdExceeded = "zk_fsync_threshold_exceed_count"
zkVersion = "zk_version"
pollerFailuresTotal = "polling_failures_total"
zkOK = "zk_ok"
// server states
unknown serverState = -1
follower serverState = 1
leader serverState = 2
standalone serverState = 3
)
type zkMetrics struct {
gauges map[string]*prometheus.GaugeVec
pollingFailureCounter *prometheus.CounterVec
}
func newMetrics() *zkMetrics {
// Create an internal metric to count polling failures
failureCounter := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: prependNamespace(pollerFailuresTotal),
Help: "Polling failure count",
}, []string{"zk_instance"})
prometheus.MustRegister(failureCounter)
return &zkMetrics{
gauges: initGauges(),
pollingFailureCounter: failureCounter,
}
}
func getState(s string) serverState {
switch s {
case "follower":
return follower
case "leader":
return leader
case "standalone":
return standalone
default:
return unknown
}
}
// prepends the namespace in front of all metric names
func prependNamespace(rawMetricName string) string {
return *metricsNamespace + rawMetricName
}
// Creates a map of all known metrics exposed by zookeeper's mntr command
// literal metric name maps to a prometheus Gauge with label zk_instance set to zk's address
func initGauges() map[string]*prometheus.GaugeVec {
allMetrics := make(map[string]*prometheus.GaugeVec)
allMetrics[zkAvgLatency] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: prependNamespace(zkAvgLatency),
Help: "Average Latency for ZooKeeper network requests",
}, []string{"zk_instance"})
allMetrics[zkMinLatency] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: prependNamespace(zkMinLatency),
Help: "Minimum latency for Zookeeper network requests.",
}, []string{"zk_instance"})
allMetrics[zkMaxLatency] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: prependNamespace(zkMaxLatency),
Help: "Maximum latency for ZooKeeper network requests",
}, []string{"zk_instance"})
allMetrics[zkPacketsReceived] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: prependNamespace(zkPacketsReceived),
Help: "Number of network packets received by the ZooKeeper instance.",
}, []string{"zk_instance"})
allMetrics[zkPacketsSent] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: prependNamespace(zkPacketsSent),
Help: "Number of network packets sent by the ZooKeeper instance.",
}, []string{"zk_instance"})
allMetrics[zkNumAliveConnections] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: prependNamespace(zkNumAliveConnections),
Help: "Number of currently alive connections to the ZooKeeper instance.",
}, []string{"zk_instance"})
allMetrics[zkOutstandingRequests] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: prependNamespace(zkOutstandingRequests),
Help: "Number of requests currently waiting in the queue.",
}, []string{"zk_instance"})
allMetrics[zkZnodeCount] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: prependNamespace(zkZnodeCount),
Help: "Znode count",
}, []string{"zk_instance"})
allMetrics[zkWatchCount] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: prependNamespace(zkWatchCount),
Help: "Watch count",
}, []string{"zk_instance"})
allMetrics[zkEphemeralsCount] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: prependNamespace(zkEphemeralsCount),
Help: "Ephemerals Count",
}, []string{"zk_instance"})
allMetrics[zkApproximateDataSize] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: prependNamespace(zkApproximateDataSize),
Help: "Approximate data size",
}, []string{"zk_instance"})
allMetrics[zkOpenFileDescriptorCount] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: prependNamespace(zkOpenFileDescriptorCount),
Help: "Number of currently open file descriptors",
}, []string{"zk_instance"})
allMetrics[zkMaxFileDescriptorCount] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: prependNamespace(zkMaxFileDescriptorCount),
Help: "Maximum number of open file descriptors",
}, []string{"zk_instance"})
allMetrics[zkServerState] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: prependNamespace(zkServerState),
Help: "Current state of the zk instance: 1 = follower, 2 = leader, 3 = standalone, -1 if unknown",
}, []string{"zk_instance"})
allMetrics[zkFollowers] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: prependNamespace(zkFollowers),
Help: "Leader only: number of followers.",
}, []string{"zk_instance"})
allMetrics[zkSyncedFollowers] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: prependNamespace(zkSyncedFollowers),
Help: "Leader only: number of followers currently in sync",
}, []string{"zk_instance"})
allMetrics[zkPendingSyncs] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: prependNamespace(zkPendingSyncs),
Help: "Current number of pending syncs",
}, []string{"zk_instance"})
allMetrics[zkOK] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: prependNamespace(zkOK),
Help: "Is ZooKeeper currently OK",
}, []string{"zk_instance"})
allMetrics[zkFsyncThresholdExceeded] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: prependNamespace(zkFsyncThresholdExceeded),
Help: "Number of times File sync exceeded fsyncWarningThresholdMS",
}, []string{"zk_instance"})
allMetrics[zkVersion] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: prependNamespace(zkVersion),
Help: "Zookeeper version",
}, []string{"zk_instance", "zk_version"})
// Register all gauges with prometheus registry so they're exposed by promhttp Handler
for _, metric := range allMetrics {
prometheus.MustRegister(metric)
}
return allMetrics
}