-
Notifications
You must be signed in to change notification settings - Fork 340
/
nginx_plus.go
322 lines (302 loc) · 21.1 KB
/
nginx_plus.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
320
321
322
package collector
import (
"log"
"sync"
plusclient "github.com/nginxinc/nginx-plus-go-sdk/client"
"github.com/prometheus/client_golang/prometheus"
)
// NginxPlusCollector collects NGINX Plus metrics. It implements prometheus.Collector interface.
type NginxPlusCollector struct {
nginxClient *plusclient.NginxClient
totalMetrics map[string]*prometheus.Desc
serverZoneMetrics map[string]*prometheus.Desc
upstreamMetrics map[string]*prometheus.Desc
upstreamServerMetrics map[string]*prometheus.Desc
streamServerZoneMetrics map[string]*prometheus.Desc
streamUpstreamMetrics map[string]*prometheus.Desc
streamUpstreamServerMetrics map[string]*prometheus.Desc
mutex sync.Mutex
}
// NewNginxPlusCollector creates an NginxPlusCollector.
func NewNginxPlusCollector(nginxClient *plusclient.NginxClient, namespace string) *NginxPlusCollector {
return &NginxPlusCollector{
nginxClient: nginxClient,
totalMetrics: map[string]*prometheus.Desc{
"connections_accepted": newGlobalMetric(namespace, "connections_accepted", "Accepted client connections"),
"connections_dropped": newGlobalMetric(namespace, "connections_dropped", "Dropped client connections"),
"connections_active": newGlobalMetric(namespace, "connections_active", "Active client connections"),
"connections_idle": newGlobalMetric(namespace, "connections_idle", "Idle client connections"),
"http_requests_total": newGlobalMetric(namespace, "http_requests_total", "Total http requests"),
"http_requests_current": newGlobalMetric(namespace, "http_requests_current", "Current http requests"),
"ssl_handshakes": newGlobalMetric(namespace, "ssl_handshakes", "Successful SSL handshakes"),
"ssl_handshakes_failed": newGlobalMetric(namespace, "ssl_handshakes_failed", "Failed SSL handshakes"),
"ssl_session_reuses": newGlobalMetric(namespace, "ssl_session_reuses", "Session reuses during SSL handshake"),
},
serverZoneMetrics: map[string]*prometheus.Desc{
"processing": newServerZoneMetric(namespace, "processing", "Client requests that are currently being processed", nil),
"requests": newServerZoneMetric(namespace, "requests", "Total client requests", nil),
"responses_1xx": newServerZoneMetric(namespace, "responses", "Total responses sent to clients", prometheus.Labels{"code": "1xx"}),
"responses_2xx": newServerZoneMetric(namespace, "responses", "Total responses sent to clients", prometheus.Labels{"code": "2xx"}),
"responses_3xx": newServerZoneMetric(namespace, "responses", "Total responses sent to clients", prometheus.Labels{"code": "3xx"}),
"responses_4xx": newServerZoneMetric(namespace, "responses", "Total responses sent to clients", prometheus.Labels{"code": "4xx"}),
"responses_5xx": newServerZoneMetric(namespace, "responses", "Total responses sent to clients", prometheus.Labels{"code": "5xx"}),
"discarded": newServerZoneMetric(namespace, "discarded", "Requests completed without sending a response", nil),
"received": newServerZoneMetric(namespace, "received", "Bytes received from clients", nil),
"sent": newServerZoneMetric(namespace, "sent", "Bytes sent to clients", nil),
},
streamServerZoneMetrics: map[string]*prometheus.Desc{
"processing": newStreamServerZoneMetric(namespace, "processing", "Client connections that are currently being processed", nil),
"connections": newStreamServerZoneMetric(namespace, "connections", "Total connections", nil),
"sessions_2xx": newStreamServerZoneMetric(namespace, "sessions", "Total sessions completed", prometheus.Labels{"code": "2xx"}),
"sessions_4xx": newStreamServerZoneMetric(namespace, "sessions", "Total sessions completed", prometheus.Labels{"code": "4xx"}),
"sessions_5xx": newStreamServerZoneMetric(namespace, "sessions", "Total sessions completed", prometheus.Labels{"code": "5xx"}),
"sessions_total": newStreamServerZoneMetric(namespace, "sessions", "Total sessions completed", prometheus.Labels{"code": "total"}),
"discarded": newStreamServerZoneMetric(namespace, "discarded", "Connections completed without creating a session", nil),
"received": newStreamServerZoneMetric(namespace, "received", "Bytes received from clients", nil),
"sent": newStreamServerZoneMetric(namespace, "sent", "Bytes sent to clients", nil),
},
upstreamMetrics: map[string]*prometheus.Desc{
"keepalives": newUpstreamMetric(namespace, "keepalives", "Idle keepalive connections"),
"zombies": newUpstreamMetric(namespace, "zombies", "Servers removed from the group but still processing active client requests"),
},
streamUpstreamMetrics: map[string]*prometheus.Desc{
"zombies": newStreamUpstreamMetric(namespace, "zombies", "Servers removed from the group but still processing active client connections"),
},
upstreamServerMetrics: map[string]*prometheus.Desc{
"state": newUpstreamServerMetric(namespace, "state", "Current state", nil),
"active": newUpstreamServerMetric(namespace, "active", "Active connections", nil),
"requests": newUpstreamServerMetric(namespace, "requests", "Total client requests", nil),
"responses_1xx": newUpstreamServerMetric(namespace, "responses", "Total responses sent to clients", prometheus.Labels{"code": "1xx"}),
"responses_2xx": newUpstreamServerMetric(namespace, "responses", "Total responses sent to clients", prometheus.Labels{"code": "2xx"}),
"responses_3xx": newUpstreamServerMetric(namespace, "responses", "Total responses sent to clients", prometheus.Labels{"code": "3xx"}),
"responses_4xx": newUpstreamServerMetric(namespace, "responses", "Total responses sent to clients", prometheus.Labels{"code": "4xx"}),
"responses_5xx": newUpstreamServerMetric(namespace, "responses", "Total responses sent to clients", prometheus.Labels{"code": "5xx"}),
"sent": newUpstreamServerMetric(namespace, "sent", "Bytes sent to this server", nil),
"received": newUpstreamServerMetric(namespace, "received", "Bytes received to this server", nil),
"fails": newUpstreamServerMetric(namespace, "fails", "Active connections", nil),
"unavail": newUpstreamServerMetric(namespace, "unavail", "How many times the server became unavailable for client requests (state 'unavail') due to the number of unsuccessful attempts reaching the max_fails threshold", nil),
"header_time": newUpstreamServerMetric(namespace, "header_time", "Average time to get the response header from the server", nil),
"response_time": newUpstreamServerMetric(namespace, "response_time", "Average time to get the full response from the server", nil),
"health_checks_checks": newUpstreamServerMetric(namespace, "health_checks_checks", "Total health check requests", nil),
"health_checks_fails": newUpstreamServerMetric(namespace, "health_checks_fails", "Failed health checks", nil),
"health_checks_unhealthy": newUpstreamServerMetric(namespace, "health_checks_unhealthy", "How many times the server became unhealthy (state 'unhealthy')", nil),
},
streamUpstreamServerMetrics: map[string]*prometheus.Desc{
"state": newStreamUpstreamServerMetric(namespace, "state", "Current state"),
"active": newStreamUpstreamServerMetric(namespace, "active", "Active connections"),
"sent": newStreamUpstreamServerMetric(namespace, "sent", "Bytes sent to this server"),
"received": newStreamUpstreamServerMetric(namespace, "received", "Bytes received to this server"),
"fails": newStreamUpstreamServerMetric(namespace, "fails", "Number of unsuccessful attempts to communicate with the server"),
"unavail": newStreamUpstreamServerMetric(namespace, "unavail", "How many times the server became unavailable for client connections (state 'unavail') due to the number of unsuccessful attempts reaching the max_fails threshold"),
"connections": newStreamUpstreamServerMetric(namespace, "connections", "Total number of client connections forwarded to this server"),
"connect_time": newStreamUpstreamServerMetric(namespace, "connect_time", "Average time to connect to the upstream server"),
"first_byte_time": newStreamUpstreamServerMetric(namespace, "first_byte_time", "The average time to receive the first byte of data"),
"response_time": newStreamUpstreamServerMetric(namespace, "response_time", "Average time to get the full response from the server"),
"health_checks_checks": newStreamUpstreamServerMetric(namespace, "health_checks_checks", "Total health check requests"),
"health_checks_fails": newStreamUpstreamServerMetric(namespace, "health_checks_fails", "Failed health checks"),
"health_checks_unhealthy": newStreamUpstreamServerMetric(namespace, "health_checks_unhealthy", "How many times the server became unhealthy (state 'unhealthy')"),
"downtime": newStreamUpstreamServerMetric(namespace, "downtime", "Total time the server was in the 'unavail', 'checking', and 'unhealthy' states"),
},
}
}
// Describe sends the super-set of all possible descriptors of NGINX Plus metrics
// to the provided channel.
func (c *NginxPlusCollector) Describe(ch chan<- *prometheus.Desc) {
for _, m := range c.totalMetrics {
ch <- m
}
for _, m := range c.serverZoneMetrics {
ch <- m
}
for _, m := range c.upstreamMetrics {
ch <- m
}
for _, m := range c.upstreamServerMetrics {
ch <- m
}
for _, m := range c.streamServerZoneMetrics {
ch <- m
}
for _, m := range c.streamUpstreamMetrics {
ch <- m
}
for _, m := range c.streamUpstreamServerMetrics {
ch <- m
}
}
// Collect fetches metrics from NGINX Plus and sends them to the provided channel.
func (c *NginxPlusCollector) Collect(ch chan<- prometheus.Metric) {
c.mutex.Lock() // To protect metrics from concurrent collects
defer c.mutex.Unlock()
stats, err := c.nginxClient.GetStats()
if err != nil {
log.Printf("Error getting stats: %v", err)
return
}
ch <- prometheus.MustNewConstMetric(c.totalMetrics["connections_accepted"],
prometheus.CounterValue, float64(stats.Connections.Accepted))
ch <- prometheus.MustNewConstMetric(c.totalMetrics["connections_dropped"],
prometheus.CounterValue, float64(stats.Connections.Dropped))
ch <- prometheus.MustNewConstMetric(c.totalMetrics["connections_active"],
prometheus.GaugeValue, float64(stats.Connections.Active))
ch <- prometheus.MustNewConstMetric(c.totalMetrics["connections_idle"],
prometheus.GaugeValue, float64(stats.Connections.Idle))
ch <- prometheus.MustNewConstMetric(c.totalMetrics["http_requests_total"],
prometheus.CounterValue, float64(stats.HTTPRequests.Total))
ch <- prometheus.MustNewConstMetric(c.totalMetrics["http_requests_current"],
prometheus.GaugeValue, float64(stats.HTTPRequests.Current))
ch <- prometheus.MustNewConstMetric(c.totalMetrics["ssl_handshakes"],
prometheus.CounterValue, float64(stats.SSL.Handshakes))
ch <- prometheus.MustNewConstMetric(c.totalMetrics["ssl_handshakes_failed"],
prometheus.CounterValue, float64(stats.SSL.HandshakesFailed))
ch <- prometheus.MustNewConstMetric(c.totalMetrics["ssl_session_reuses"],
prometheus.CounterValue, float64(stats.SSL.SessionReuses))
for name, zone := range stats.ServerZones {
ch <- prometheus.MustNewConstMetric(c.serverZoneMetrics["processing"],
prometheus.GaugeValue, float64(zone.Processing), name)
ch <- prometheus.MustNewConstMetric(c.serverZoneMetrics["requests"],
prometheus.CounterValue, float64(zone.Requests), name)
ch <- prometheus.MustNewConstMetric(c.serverZoneMetrics["responses_1xx"],
prometheus.CounterValue, float64(zone.Responses.Responses1xx), name)
ch <- prometheus.MustNewConstMetric(c.serverZoneMetrics["responses_2xx"],
prometheus.CounterValue, float64(zone.Responses.Responses2xx), name)
ch <- prometheus.MustNewConstMetric(c.serverZoneMetrics["responses_3xx"],
prometheus.CounterValue, float64(zone.Responses.Responses3xx), name)
ch <- prometheus.MustNewConstMetric(c.serverZoneMetrics["responses_4xx"],
prometheus.CounterValue, float64(zone.Responses.Responses4xx), name)
ch <- prometheus.MustNewConstMetric(c.serverZoneMetrics["responses_5xx"],
prometheus.CounterValue, float64(zone.Responses.Responses5xx), name)
ch <- prometheus.MustNewConstMetric(c.serverZoneMetrics["discarded"],
prometheus.CounterValue, float64(zone.Discarded), name)
ch <- prometheus.MustNewConstMetric(c.serverZoneMetrics["received"],
prometheus.CounterValue, float64(zone.Received), name)
ch <- prometheus.MustNewConstMetric(c.serverZoneMetrics["sent"],
prometheus.CounterValue, float64(zone.Sent), name)
}
for name, zone := range stats.StreamServerZones {
ch <- prometheus.MustNewConstMetric(c.streamServerZoneMetrics["processing"],
prometheus.GaugeValue, float64(zone.Processing), name)
ch <- prometheus.MustNewConstMetric(c.streamServerZoneMetrics["connections"],
prometheus.CounterValue, float64(zone.Connections), name)
ch <- prometheus.MustNewConstMetric(c.streamServerZoneMetrics["sessions_2xx"],
prometheus.CounterValue, float64(zone.Sessions.Sessions2xx), name)
ch <- prometheus.MustNewConstMetric(c.streamServerZoneMetrics["sessions_4xx"],
prometheus.CounterValue, float64(zone.Sessions.Sessions4xx), name)
ch <- prometheus.MustNewConstMetric(c.streamServerZoneMetrics["sessions_5xx"],
prometheus.CounterValue, float64(zone.Sessions.Sessions5xx), name)
ch <- prometheus.MustNewConstMetric(c.streamServerZoneMetrics["sessions_total"],
prometheus.CounterValue, float64(zone.Sessions.Total), name)
ch <- prometheus.MustNewConstMetric(c.streamServerZoneMetrics["discarded"],
prometheus.CounterValue, float64(zone.Discarded), name)
ch <- prometheus.MustNewConstMetric(c.streamServerZoneMetrics["received"],
prometheus.CounterValue, float64(zone.Received), name)
ch <- prometheus.MustNewConstMetric(c.streamServerZoneMetrics["sent"],
prometheus.CounterValue, float64(zone.Sent), name)
}
for name, upstream := range stats.Upstreams {
for _, peer := range upstream.Peers {
ch <- prometheus.MustNewConstMetric(c.upstreamServerMetrics["state"],
prometheus.GaugeValue, upstreamServerStates[peer.State], name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.upstreamServerMetrics["active"],
prometheus.GaugeValue, float64(peer.Active), name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.upstreamServerMetrics["requests"],
prometheus.CounterValue, float64(peer.Requests), name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.upstreamServerMetrics["responses_1xx"],
prometheus.CounterValue, float64(peer.Responses.Responses1xx), name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.upstreamServerMetrics["responses_2xx"],
prometheus.CounterValue, float64(peer.Responses.Responses2xx), name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.upstreamServerMetrics["responses_3xx"],
prometheus.CounterValue, float64(peer.Responses.Responses3xx), name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.upstreamServerMetrics["responses_4xx"],
prometheus.CounterValue, float64(peer.Responses.Responses4xx), name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.upstreamServerMetrics["responses_5xx"],
prometheus.CounterValue, float64(peer.Responses.Responses5xx), name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.upstreamServerMetrics["sent"],
prometheus.CounterValue, float64(peer.Sent), name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.upstreamServerMetrics["received"],
prometheus.CounterValue, float64(peer.Received), name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.upstreamServerMetrics["fails"],
prometheus.CounterValue, float64(peer.Fails), name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.upstreamServerMetrics["unavail"],
prometheus.CounterValue, float64(peer.Unavail), name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.upstreamServerMetrics["header_time"],
prometheus.GaugeValue, float64(peer.HeaderTime), name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.upstreamServerMetrics["response_time"],
prometheus.GaugeValue, float64(peer.ResponseTime), name, peer.Server)
if peer.HealthChecks != (plusclient.HealthChecks{}) {
ch <- prometheus.MustNewConstMetric(c.upstreamServerMetrics["health_checks_checks"],
prometheus.CounterValue, float64(peer.HealthChecks.Checks), name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.upstreamServerMetrics["health_checks_fails"],
prometheus.CounterValue, float64(peer.HealthChecks.Fails), name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.upstreamServerMetrics["health_checks_unhealthy"],
prometheus.CounterValue, float64(peer.HealthChecks.Unhealthy), name, peer.Server)
}
}
ch <- prometheus.MustNewConstMetric(c.upstreamMetrics["keepalives"],
prometheus.GaugeValue, float64(upstream.Keepalives), name)
ch <- prometheus.MustNewConstMetric(c.upstreamMetrics["zombies"],
prometheus.GaugeValue, float64(upstream.Zombies), name)
}
for name, upstream := range stats.StreamUpstreams {
for _, peer := range upstream.Peers {
ch <- prometheus.MustNewConstMetric(c.streamUpstreamServerMetrics["state"],
prometheus.GaugeValue, upstreamServerStates[peer.State], name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.streamUpstreamServerMetrics["active"],
prometheus.GaugeValue, float64(peer.Active), name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.streamUpstreamServerMetrics["connections"],
prometheus.GaugeValue, float64(peer.Connections), name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.streamUpstreamServerMetrics["connect_time"],
prometheus.GaugeValue, float64(peer.ConnectTime), name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.streamUpstreamServerMetrics["first_byte_time"],
prometheus.GaugeValue, float64(peer.FirstByteTime), name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.streamUpstreamServerMetrics["response_time"],
prometheus.GaugeValue, float64(peer.ResponseTime), name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.streamUpstreamServerMetrics["sent"],
prometheus.CounterValue, float64(peer.Sent), name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.streamUpstreamServerMetrics["received"],
prometheus.CounterValue, float64(peer.Received), name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.streamUpstreamServerMetrics["fails"],
prometheus.CounterValue, float64(peer.Fails), name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.streamUpstreamServerMetrics["unavail"],
prometheus.CounterValue, float64(peer.Unavail), name, peer.Server)
if peer.HealthChecks != (plusclient.HealthChecks{}) {
ch <- prometheus.MustNewConstMetric(c.streamUpstreamServerMetrics["health_checks_checks"],
prometheus.CounterValue, float64(peer.HealthChecks.Checks), name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.streamUpstreamServerMetrics["health_checks_fails"],
prometheus.CounterValue, float64(peer.HealthChecks.Fails), name, peer.Server)
ch <- prometheus.MustNewConstMetric(c.streamUpstreamServerMetrics["health_checks_unhealthy"],
prometheus.CounterValue, float64(peer.HealthChecks.Unhealthy), name, peer.Server)
}
ch <- prometheus.MustNewConstMetric(c.streamUpstreamServerMetrics["downtime"],
prometheus.GaugeValue, float64(peer.Downtime), name, peer.Server)
}
ch <- prometheus.MustNewConstMetric(c.streamUpstreamMetrics["zombies"],
prometheus.GaugeValue, float64(upstream.Zombies), name)
}
}
var upstreamServerStates = map[string]float64{
"up": 1.0,
"draining": 2.0,
"down": 3.0,
"unavail": 4.0,
"checking": 5.0,
"unhealthy": 6.0,
}
func newServerZoneMetric(namespace string, metricName string, docString string, constLabels prometheus.Labels) *prometheus.Desc {
return prometheus.NewDesc(prometheus.BuildFQName(namespace, "server_zone", metricName), docString, []string{"server_zone"}, constLabels)
}
func newStreamServerZoneMetric(namespace string, metricName string, docString string, constLabels prometheus.Labels) *prometheus.Desc {
return prometheus.NewDesc(prometheus.BuildFQName(namespace, "stream_server_zone", metricName), docString, []string{"server_zone"}, constLabels)
}
func newUpstreamMetric(namespace string, metricName string, docString string) *prometheus.Desc {
return prometheus.NewDesc(prometheus.BuildFQName(namespace, "upstream", metricName), docString, []string{"upstream"}, nil)
}
func newStreamUpstreamMetric(namespace string, metricName string, docString string) *prometheus.Desc {
return prometheus.NewDesc(prometheus.BuildFQName(namespace, "stream_upstream", metricName), docString, []string{"upstream"}, nil)
}
func newUpstreamServerMetric(namespace string, metricName string, docString string, constLabels prometheus.Labels) *prometheus.Desc {
return prometheus.NewDesc(prometheus.BuildFQName(namespace, "upstream_server", metricName), docString, []string{"upstream", "server"}, constLabels)
}
func newStreamUpstreamServerMetric(namespace string, metricName string, docString string) *prometheus.Desc {
return prometheus.NewDesc(prometheus.BuildFQName(namespace, "stream_upstream_server", metricName), docString, []string{"upstream", "server"}, nil)
}