/
metrics.go
283 lines (233 loc) · 8.54 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
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
package metrics
import (
"fmt"
"net/http"
"runtime"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/yyyar/gobetween/config"
"github.com/yyyar/gobetween/core"
"github.com/yyyar/gobetween/info"
"github.com/yyyar/gobetween/logging"
"github.com/yyyar/gobetween/stats/counters"
)
const (
namespace = "gobetween"
)
var (
metricsDisabled bool = false
log = logging.For("metrics")
buildInfo *prometheus.GaugeVec
version string
revision string
branch string
serverCount *prometheus.GaugeVec
serverActiveConnections *prometheus.GaugeVec
serverRxTotal *prometheus.GaugeVec
serverTxTotal *prometheus.GaugeVec
serverRxSecond *prometheus.GaugeVec
serverTxSecond *prometheus.GaugeVec
backendActiveConnections *prometheus.GaugeVec
backendRefusedConnections *prometheus.GaugeVec
backendTotalConnections *prometheus.GaugeVec
backendRxBytes *prometheus.GaugeVec
backendTxBytes *prometheus.GaugeVec
backendRxSecond *prometheus.GaugeVec
backendTxSecond *prometheus.GaugeVec
backendLive *prometheus.GaugeVec
)
func defineMetrics() {
buildInfo = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "build_info",
Help: fmt.Sprintf(
"A metric with a constant '1' value labeled by version, revision, branch, and goversion from which %s was built.",
namespace,
),
}, []string{"version", "revision", "branch", "goversion"})
serverCount = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "server",
Name: "count",
Help: "Server Count.",
}, []string{"server"})
serverActiveConnections = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "server",
Name: "active_connections",
Help: "Server Actice Connections.",
}, []string{"server"})
serverRxTotal = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "server",
Name: "rx_total",
Help: "Server Rx Total.",
}, []string{"server"})
serverTxTotal = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "server",
Name: "tx_total",
Help: "Server Tx Total.",
}, []string{"server"})
serverRxSecond = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "server",
Name: "rx_second",
Help: "Server Rx per Second.",
}, []string{"server"})
serverTxSecond = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "server",
Name: "tx_second",
Help: "Server Tx per Second.",
}, []string{"server"})
backendActiveConnections = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "backend",
Name: "active_connections",
Help: "Backend Actice Connections.",
}, []string{"server", "host", "port"})
backendRefusedConnections = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "backend",
Name: "refused_connections",
Help: "Backend Refused Connections.",
}, []string{"server", "host", "port"})
backendTotalConnections = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "backend",
Name: "total_connections",
Help: "Backend Total Connections.",
}, []string{"server", "host", "port"})
backendRxBytes = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "backend",
Name: "rx_bytes",
Help: "Backend Rx Bytes.",
}, []string{"server", "host", "port"})
backendTxBytes = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "backend",
Name: "tx_bytes",
Help: "Backend Tx Bytes.",
}, []string{"server", "host", "port"})
backendRxSecond = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "backend",
Name: "rx_second",
Help: "Backend Rx per Second.",
}, []string{"server", "host", "port"})
backendTxSecond = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "backend",
Name: "tx_second",
Help: "Backend Tx per Second.",
}, []string{"server", "host", "port"})
backendLive = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "backend",
Name: "live",
Help: "Backend Alive.",
}, []string{"server", "host", "port"})
}
func Start(cfg config.MetricsConfig) {
if !cfg.Enabled {
log.Info("Metrics disabled")
metricsDisabled = true
return
}
log.Info("Starting up Metrics server ", cfg.Bind)
defineMetrics()
prometheus.MustRegister(buildInfo)
buildInfo.WithLabelValues(info.Version, info.Revision, info.Branch, runtime.Version()).Set(1)
prometheus.MustRegister(serverCount)
prometheus.MustRegister(serverActiveConnections)
prometheus.MustRegister(serverRxTotal)
prometheus.MustRegister(serverTxTotal)
prometheus.MustRegister(serverRxSecond)
prometheus.MustRegister(serverTxSecond)
prometheus.MustRegister(backendActiveConnections)
prometheus.MustRegister(backendRefusedConnections)
prometheus.MustRegister(backendTotalConnections)
prometheus.MustRegister(backendRxBytes)
prometheus.MustRegister(backendTxBytes)
prometheus.MustRegister(backendRxSecond)
prometheus.MustRegister(backendTxSecond)
prometheus.MustRegister(backendLive)
http.Handle("/metrics", promhttp.Handler())
go func() {
fmt.Errorf("%s", http.ListenAndServe(cfg.Bind, nil))
}()
}
func RemoveServer(server string, backends map[core.Target]*core.Backend) {
if metricsDisabled {
return
}
serverCount.DeleteLabelValues(server)
serverActiveConnections.DeleteLabelValues(server)
serverRxTotal.DeleteLabelValues(server)
serverTxTotal.DeleteLabelValues(server)
serverRxSecond.DeleteLabelValues(server)
serverTxSecond.DeleteLabelValues(server)
for _, backend := range backends {
RemoveBackend(server, backend)
}
}
func RemoveBackend(server string, backend *core.Backend) {
if metricsDisabled {
return
}
backendActiveConnections.DeleteLabelValues(server, backend.Host, backend.Port)
backendRefusedConnections.DeleteLabelValues(server, backend.Host, backend.Port)
backendTotalConnections.DeleteLabelValues(server, backend.Host, backend.Port)
backendRxBytes.DeleteLabelValues(server, backend.Host, backend.Port)
backendTxBytes.DeleteLabelValues(server, backend.Host, backend.Port)
backendRxSecond.DeleteLabelValues(server, backend.Host, backend.Port)
backendTxSecond.DeleteLabelValues(server, backend.Host, backend.Port)
backendLive.DeleteLabelValues(server, backend.Host, backend.Port)
}
func ReportHandleBackendLiveChange(server string, target core.Target, live bool) {
if metricsDisabled {
return
}
intLive := int(0)
if live {
intLive = 1
}
backendLive.WithLabelValues(server, target.Host, target.Port).Set(float64(intLive))
}
func ReportHandleConnectionsChange(server string, connections uint) {
if metricsDisabled {
return
}
serverActiveConnections.WithLabelValues(server).Set(float64(connections))
}
func ReportHandleStatsChange(server string, bs counters.BandwidthStats) {
if metricsDisabled {
return
}
serverRxTotal.WithLabelValues(server).Set(float64(bs.RxTotal))
serverTxTotal.WithLabelValues(server).Set(float64(bs.TxTotal))
serverRxSecond.WithLabelValues(server).Set(float64(bs.RxSecond))
serverTxSecond.WithLabelValues(server).Set(float64(bs.TxSecond))
}
func ReportHandleBackendStatsChange(server string, target core.Target, backends map[core.Target]*core.Backend) {
if metricsDisabled {
return
}
backend, _ := backends[target]
serverCount.WithLabelValues(server).Set(float64(len(backends)))
backendRxBytes.WithLabelValues(server, target.Host, target.Port).Set(float64(backend.Stats.RxBytes))
backendTxBytes.WithLabelValues(server, target.Host, target.Port).Set(float64(backend.Stats.TxBytes))
backendRxSecond.WithLabelValues(server, target.Host, target.Port).Set(float64(backend.Stats.RxSecond))
backendTxSecond.WithLabelValues(server, target.Host, target.Port).Set(float64(backend.Stats.TxSecond))
}
func ReportHandleOp(server string, target core.Target, backends map[core.Target]*core.Backend) {
if metricsDisabled {
return
}
backend, _ := backends[target]
backendActiveConnections.WithLabelValues(server, target.Host, target.Port).Set(float64(backend.Stats.ActiveConnections))
backendRefusedConnections.WithLabelValues(server, target.Host, target.Port).Set(float64(backend.Stats.RefusedConnections))
backendTotalConnections.WithLabelValues(server, target.Host, target.Port).Set(float64(backend.Stats.TotalConnections))
}