-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
319 lines (295 loc) · 11.3 KB
/
main.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
package main
import (
"bufio"
"encoding/json"
"flag"
"net"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
)
const (
namespace = "twemproxy_exporter"
)
func main() {
var (
timeout = flag.Duration("twemproxy.timeout", 2*time.Second, "Timeout for request to twemproxy.")
twemproxyAddress = flag.String("twemproxy.stats-address", "localhost:22222", "Stats address of twemproxy.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
listenAddress = flag.String("web.listen-address", ":9151", "Address to listen on for web interface and telemetry.")
)
flag.Parse()
if *twemproxyAddress == "" {
log.Fatalln("-twemproxy.stats-address must not be blank")
}
if *timeout == 0 {
*timeout = 2 * time.Second
}
prometheus.MustRegister(newExporter(*twemproxyAddress, *timeout))
http.Handle(*metricsPath, prometheus.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Twemproxy Exporter</title></head>
<body>
<h1>Twemproxy Exporter</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
</body>
</html>`))
})
log.Infoln("starting twemproxy_exporter", version.Info())
log.Infoln("build context", version.BuildContext())
log.Infoln("listening on", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}
type exporter struct {
endpoint string
timeout time.Duration
up *prometheus.Desc
totalConnections *prometheus.Desc
currentConnections *prometheus.Desc // How is this different from activeClientConnections?
// Client metrics
clientEOF *prometheus.Desc
clientErr *prometheus.Desc
activeClientConnections *prometheus.Desc // Double check this is a gauge, it sounds like one..
// Number of times a backend server was ejected
backendServerEjections *prometheus.Desc
forwardErrors *prometheus.Desc
// Fragments created from a multi-vector request
fragments *prometheus.Desc
// Server metrics
serverEOF *prometheus.Desc
serverErr *prometheus.Desc
serverTimeouts *prometheus.Desc
activeServerConnections *prometheus.Desc
serverEjectedAt *prometheus.Desc // Not mentioned in README, maybe unix timestamp of when server was ejected?
requestCount *prometheus.Desc
requestBytes *prometheus.Desc
responseCount *prometheus.Desc
responseBytes *prometheus.Desc
incomingQueueGauge *prometheus.Desc // Double check this is a gauge, it sounds like one..
incomingQueueBytesGauge *prometheus.Desc // Double check this is a gauge, it sounds like one..
outgoingQueueGauge *prometheus.Desc // Double check this is a gauge, it sounds like one..
outgoingQueueBytesGauge *prometheus.Desc // Double check this is a gauge, it sounds like one..
}
func newExporter(endpoint string, timeout time.Duration) *exporter {
return &exporter{
endpoint: endpoint,
timeout: timeout,
up: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "up"),
"Could twemproxy be queried.",
nil,
nil,
),
totalConnections: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "connections_total"),
"Total number of connections.",
nil,
nil,
),
currentConnections: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "current_connections"),
"The current number of connections.",
nil,
nil,
),
clientEOF: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "client_eof_total"),
"Total number of client EOFs.",
[]string{"pool"},
nil,
),
clientErr: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "client_err_total"),
"Total number of client errors.",
[]string{"pool"},
nil,
),
activeClientConnections: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "client_connections_active"),
"The current number of active client connections.",
[]string{"pool"},
nil,
),
backendServerEjections: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "backend_server_ejections_total"),
"The number of times a backend has been ejected.",
[]string{"pool"},
nil,
),
forwardErrors: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "forward_errors_total"),
"Total number of forward errors.",
[]string{"pool"},
nil,
),
fragments: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "fragments_total"),
"Total number fragments created from multi-vector requests.",
[]string{"pool"},
nil,
),
serverEOF: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "server_eof_total"),
"Total number of server EOFs.",
[]string{"pool", "server"},
nil,
),
serverErr: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "server_err_total"),
"Total number of server errors.",
[]string{"pool", "server"},
nil,
),
serverTimeouts: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "server_timeouts_total"),
"Total number of times the server has timed out.",
[]string{"pool", "server"},
nil,
),
activeServerConnections: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "server_connections_active"),
"The current number of active server connections.",
[]string{"pool", "server"},
nil,
),
serverEjectedAt: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "server_ejected_at"),
"The time when the server was ejected in UNIX time.", // Confirm ms/sec?
[]string{"pool", "server"},
nil,
),
requestCount: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "server_requests_total"),
"Total number of requests to the server.",
[]string{"pool", "server"},
nil,
),
requestBytes: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "server_requests_bytes_total"),
"Total number of requests bytes sent to the server.",
[]string{"pool", "server"},
nil,
),
responseCount: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "server_responses_total"),
"Total number of responses to the server.",
[]string{"pool", "server"},
nil,
),
responseBytes: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "server_responses_bytes_total"),
"Total number of response bytes sent to the server.",
[]string{"pool", "server"},
nil,
),
incomingQueueGauge: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "incoming_queue"),
"The current number of requests in the incoming queue.",
[]string{"pool", "server"},
nil,
),
incomingQueueBytesGauge: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "incoming_queue_bytes"),
"The current number of bytes in the incoming queue.",
[]string{"pool", "server"},
nil,
),
outgoingQueueGauge: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "outgoing_queue"),
"The current number of requests in the outgoing queue.",
[]string{"pool", "server"},
nil,
),
outgoingQueueBytesGauge: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "outgoing_queue_bytes"),
"The current number of bytes in the outgoing queue.",
[]string{"pool", "server"},
nil,
),
}
}
// Collect implements prometheus.Collector. It fetches the statistics from
// twemproxy, and delivers them as Prometheus metrics.
func (e *exporter) Collect(ch chan<- prometheus.Metric) {
var up float64
defer func() {
ch <- prometheus.MustNewConstMetric(e.up, prometheus.GaugeValue, up)
}()
st, err := e.collect()
if err != nil {
log.Infof("failed to collect stats: %v", err)
return
}
up = 1
ch <- prometheus.MustNewConstMetric(e.totalConnections, prometheus.CounterValue, st.TotalConnections)
ch <- prometheus.MustNewConstMetric(e.currentConnections, prometheus.GaugeValue, st.CurrConnections)
for poolName, pool := range st.Pools {
ch <- prometheus.MustNewConstMetric(e.clientEOF, prometheus.CounterValue, pool.ClientEOF, poolName)
ch <- prometheus.MustNewConstMetric(e.clientErr, prometheus.CounterValue, pool.ClientErr, poolName)
ch <- prometheus.MustNewConstMetric(e.activeClientConnections, prometheus.GaugeValue, pool.ClientConnections, poolName)
ch <- prometheus.MustNewConstMetric(e.backendServerEjections, prometheus.CounterValue, pool.ClientConnections, poolName)
ch <- prometheus.MustNewConstMetric(e.forwardErrors, prometheus.CounterValue, pool.ForwardError, poolName)
ch <- prometheus.MustNewConstMetric(e.fragments, prometheus.CounterValue, pool.Fragments, poolName)
for serverName, server := range pool.Servers {
ch <- prometheus.MustNewConstMetric(e.serverEOF, prometheus.CounterValue, server.ServerEOF, poolName, serverName)
ch <- prometheus.MustNewConstMetric(e.serverErr, prometheus.CounterValue, server.ServerErr, poolName, serverName)
ch <- prometheus.MustNewConstMetric(e.serverTimeouts, prometheus.CounterValue, server.ServerTimedout, poolName, serverName)
ch <- prometheus.MustNewConstMetric(e.activeServerConnections, prometheus.GaugeValue, server.ServerConnections, poolName, serverName)
ch <- prometheus.MustNewConstMetric(e.serverEjectedAt, prometheus.GaugeValue, server.ServerEjectedAt, poolName, serverName)
ch <- prometheus.MustNewConstMetric(e.requestCount, prometheus.CounterValue, server.Requests, poolName, serverName)
ch <- prometheus.MustNewConstMetric(e.requestBytes, prometheus.CounterValue, server.RequestBytes, poolName, serverName)
ch <- prometheus.MustNewConstMetric(e.responseCount, prometheus.CounterValue, server.Responses, poolName, serverName)
ch <- prometheus.MustNewConstMetric(e.responseBytes, prometheus.CounterValue, server.ResponseBytes, poolName, serverName)
ch <- prometheus.MustNewConstMetric(e.incomingQueueGauge, prometheus.GaugeValue, server.InQueue, poolName, serverName)
ch <- prometheus.MustNewConstMetric(e.incomingQueueBytesGauge, prometheus.GaugeValue, server.InQueueBytes, poolName, serverName)
ch <- prometheus.MustNewConstMetric(e.outgoingQueueGauge, prometheus.GaugeValue, server.OutQueue, poolName, serverName)
ch <- prometheus.MustNewConstMetric(e.outgoingQueueBytesGauge, prometheus.GaugeValue, server.OutQueueBytes, poolName, serverName)
}
}
}
func (e *exporter) collect() (*stats, error) {
// TODO: Reuse connection, connect with net.DialTCP?
conn, err := net.DialTimeout("tcp", e.endpoint, e.timeout)
if err != nil {
log.Errorf("failed to dial endpoint: %v", err)
return nil, err
}
conn.SetReadDeadline(time.Now().Add(e.timeout))
defer conn.Close()
r := bufio.NewReader(conn)
st := new(stats)
if err := json.NewDecoder(r).Decode(st); err != nil {
log.Errorf("failed to unmarshal response: %v", err)
return nil, err
}
return st, nil
}
// Describe implements prometheus.Collector. It sends all Descriptors possible
// from this exporter.
func (e *exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- e.up
ch <- e.totalConnections
ch <- e.currentConnections
ch <- e.clientEOF
ch <- e.clientErr
ch <- e.activeClientConnections
ch <- e.backendServerEjections
ch <- e.forwardErrors
ch <- e.fragments
ch <- e.serverEOF
ch <- e.serverErr
ch <- e.serverTimeouts
ch <- e.activeServerConnections
ch <- e.serverEjectedAt
ch <- e.requestCount
ch <- e.requestBytes
ch <- e.responseCount
ch <- e.responseBytes
ch <- e.incomingQueueGauge
ch <- e.incomingQueueBytesGauge
ch <- e.outgoingQueueGauge
ch <- e.outgoingQueueBytesGauge
}