-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
218 lines (187 loc) · 5.92 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
package main
import (
"bytes"
"context"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const templateMetrics string = `Active connections: %d
server accepts handled requests
%d %d %d
Reading: %d Writing: %d Waiting: %d
`
// StubStats represents NGINX stub_status metrics.
type StubStats struct {
Connections StubConnections
Requests int64
}
// StubConnections represents connections related metrics.
type StubConnections struct {
Active int64
Accepted int64
Handled int64
Reading int64
Writing int64
Waiting int64
}
// GetStubStats fetches the stub_status metrics.
func GetStubStats(endpoint string) (*StubStats, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
req, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
endpoint,
http.NoBody,
)
if err != nil {
return nil, fmt.Errorf("failed to create a get request: %w", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to get %v: %w", endpoint, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf(
"expected %v response, got %v",
http.StatusOK,
resp.StatusCode,
)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read the response body: %w", err)
}
r := bytes.NewReader(body)
stats, err := parseStubStats(r)
if err != nil {
return nil, fmt.Errorf(
"failed to parse response body %q: %w",
string(body),
err,
)
}
return stats, nil
}
func parseStubStats(r io.Reader) (*StubStats, error) {
var s StubStats
if _, err := fmt.Fscanf(r, templateMetrics,
&s.Connections.Active,
&s.Connections.Accepted,
&s.Connections.Handled,
&s.Requests,
&s.Connections.Reading,
&s.Connections.Writing,
&s.Connections.Waiting); err != nil {
return nil, fmt.Errorf("failed to scan template metrics: %w", err)
}
return &s, nil
}
// Metrics holds descriptions for NGINX-related metrics.
type metrics struct {
ActiveConnectionsDesc *prometheus.Desc
ConnectionsReadingDesc *prometheus.Desc
ConnectionsAcceptedDesc *prometheus.Desc
ConnectionsHandledDesc *prometheus.Desc
ConnectionsWaitingDesc *prometheus.Desc
ConnectionsWritingDesc *prometheus.Desc
HTTPRequestsTotalDesc *prometheus.Desc
}
// NewMetrics initializes all metric descriptions.
func NewMetrics(namespace string) *metrics {
return &metrics{
ActiveConnectionsDesc: prometheus.NewDesc(
namespace+"_connections_active",
"Active client connections",
nil, nil,
),
ConnectionsReadingDesc: prometheus.NewDesc(
namespace+"_connections_reading",
"Connections currently reading client request headers",
nil, nil,
),
ConnectionsAcceptedDesc: prometheus.NewDesc(
namespace+"_connections_accepted_total",
"Total accepted client connections",
nil, nil,
),
ConnectionsHandledDesc: prometheus.NewDesc(
namespace+"_connections_handled_total",
"Total handled client connections",
nil, nil,
),
ConnectionsWaitingDesc: prometheus.NewDesc(
namespace+"_connections_waiting",
"Idle client connections",
nil, nil,
),
ConnectionsWritingDesc: prometheus.NewDesc(
namespace+"_connections_writing",
"Connections where NGINX is currently writing responses to clients",
nil, nil,
),
HTTPRequestsTotalDesc: prometheus.NewDesc(
namespace+"_http_requests_total",
"Total number of HTTP requests handled",
nil, nil,
),
}
}
// CollectMetrics is a struct that collects metrics dynamically.
type CollectMetrics struct {
metrics *metrics
}
// NewCollector creates a new instance of CollectMetrics.
func NewCollector(namespace string, reg prometheus.Registerer) *CollectMetrics {
m := NewMetrics(namespace)
c := &CollectMetrics{metrics: m}
reg.MustRegister(c)
return c
}
// Describe sends metric descriptions to the provided channel.
func (c *CollectMetrics) Describe(ch chan<- *prometheus.Desc) {
ch <- c.metrics.ActiveConnectionsDesc
ch <- c.metrics.ConnectionsReadingDesc
ch <- c.metrics.ConnectionsAcceptedDesc
ch <- c.metrics.ConnectionsHandledDesc
ch <- c.metrics.ConnectionsWaitingDesc
ch <- c.metrics.ConnectionsWritingDesc
ch <- c.metrics.HTTPRequestsTotalDesc
}
// Collect dynamically collects metrics and sends them to Prometheus.
func (c *CollectMetrics) Collect(ch chan<- prometheus.Metric) {
endpoint := os.Getenv("NGINX_STATUS_ENDPOINT")
nginxStats, err := GetStubStats(endpoint)
if err != nil {
log.Println(err)
return
}
activeConnections := float64(nginxStats.Connections.Active)
connectionsReading := float64(nginxStats.Connections.Reading)
connectionsAccepted := float64(nginxStats.Connections.Accepted)
connectionsHandled := float64(nginxStats.Connections.Handled)
connectionsWaiting := float64(nginxStats.Connections.Waiting)
connectionsWriting := float64(nginxStats.Connections.Writing)
httpRequestsTotal := float64(nginxStats.Requests)
ch <- prometheus.MustNewConstMetric(c.metrics.ActiveConnectionsDesc, prometheus.GaugeValue, activeConnections)
ch <- prometheus.MustNewConstMetric(c.metrics.ConnectionsReadingDesc, prometheus.GaugeValue, connectionsReading)
ch <- prometheus.MustNewConstMetric(c.metrics.ConnectionsAcceptedDesc, prometheus.CounterValue, connectionsAccepted)
ch <- prometheus.MustNewConstMetric(c.metrics.ConnectionsHandledDesc, prometheus.CounterValue, connectionsHandled)
ch <- prometheus.MustNewConstMetric(c.metrics.ConnectionsWaitingDesc, prometheus.GaugeValue, connectionsWaiting)
ch <- prometheus.MustNewConstMetric(c.metrics.ConnectionsWritingDesc, prometheus.GaugeValue, connectionsWriting)
ch <- prometheus.MustNewConstMetric(c.metrics.HTTPRequestsTotalDesc, prometheus.CounterValue, httpRequestsTotal)
}
func main() {
mux := http.NewServeMux()
reg := prometheus.NewRegistry()
NewCollector("nginx", reg)
handler := promhttp.HandlerFor(reg, promhttp.HandlerOpts{})
mux.Handle("/metrics", handler)
http.ListenAndServe(":9113", mux)
}