-
Notifications
You must be signed in to change notification settings - Fork 17
/
metrics.go
236 lines (203 loc) · 5.67 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
package fastclient
import (
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
)
var (
connOpen prometheus.Gauge
statusCodes *prometheus.CounterVec
errorMessages *prometheus.CounterVec
requestDuration prometheus.Summary
timeouts prometheus.Counter
errors prometheus.Counter
requestSum prometheus.Counter
requestSuccess prometheus.Counter
connError prometheus.Counter
bytesWritten prometheus.Counter
bytesRead prometheus.Counter
writeError prometheus.Counter
readError prometheus.Counter
)
func initMetrics() {
statusCodes = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "status_codes",
Help: "Distribution by status codes counter",
},
[]string{"code"},
)
errorMessages = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "errors",
Help: "Distribution by error messages",
},
[]string{"message"},
)
timeouts = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "request_timeouts",
Help: "Number of timeouts returned by server",
},
)
errors = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "request_errors",
Help: "Number of errors returned by server. Including amount of timeouts",
},
)
requestSum = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "request_sum",
Help: "Total number of sent requests",
},
)
requestSuccess = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "request_success",
Help: "Total number of sent success requests",
},
)
requestDuration = prometheus.NewSummary(
prometheus.SummaryOpts{
Name: "request_duration",
Help: "Latency of sent requests",
Objectives: map[float64]float64{0.5: 0.05, 0.75: 0.025, 0.8: 0.02, 0.9: 0.01, 0.99: 0.001},
},
)
connOpen = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "conn_open",
Help: "Number of open connections",
},
)
connError = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "conn_errors",
Help: "Number of connections ended with error",
},
)
bytesWritten = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "bytes_written",
Help: "Amount of written bytes",
},
)
bytesRead = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "bytes_read",
Help: "Amount of read bytes",
},
)
writeError = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "request_write_errors",
Help: "Number of errors while writing",
},
)
readError = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "request_read_errors",
Help: "Number of errors while reading",
},
)
}
func registerMetrics() {
initMetrics()
prometheus.MustRegister(timeouts)
prometheus.MustRegister(errors)
prometheus.MustRegister(requestSum)
prometheus.MustRegister(requestDuration)
prometheus.MustRegister(connOpen)
prometheus.MustRegister(connError)
prometheus.MustRegister(bytesWritten)
prometheus.MustRegister(bytesRead)
prometheus.MustRegister(writeError)
prometheus.MustRegister(readError)
prometheus.MustRegister(statusCodes)
prometheus.MustRegister(errorMessages)
}
func unregisterMetrics() {
prometheus.Unregister(timeouts)
prometheus.Unregister(errors)
prometheus.Unregister(requestSum)
prometheus.Unregister(requestSuccess)
prometheus.Unregister(requestDuration)
prometheus.Unregister(connOpen)
prometheus.Unregister(connError)
prometheus.Unregister(bytesWritten)
prometheus.Unregister(bytesRead)
prometheus.Unregister(writeError)
prometheus.Unregister(readError)
prometheus.Unregister(statusCodes)
prometheus.Unregister(errorMessages)
}
func flushMetrics() {
unregisterMetrics()
registerMetrics()
}
var m = &dto.Metric{}
// Errors returns value of errors-metric
func (*Client) Errors() uint64 {
errors.Write(m)
return uint64(*m.Counter.Value)
}
// Timeouts returns value of timeouts-metric
func (*Client) Timeouts() uint64 {
timeouts.Write(m)
return uint64(*m.Counter.Value)
}
// RequestSum returns value of requestSum-metric
func (*Client) RequestSum() uint64 {
requestSum.Write(m)
return uint64(*m.Counter.Value)
}
// RequestSuccess returns value of requestSuccess-metric
func (*Client) RequestSuccess() uint64 {
requestSuccess.Write(m)
return uint64(*m.Counter.Value)
}
// BytesWritten returns value of bytesWritten-metric
func (*Client) BytesWritten() uint64 {
bytesWritten.Write(m)
return uint64(*m.Counter.Value)
}
// BytesRead returns value of bytesRead-metric
func (*Client) BytesRead() uint64 {
bytesRead.Write(m)
return uint64(*m.Counter.Value)
}
// ConnOpen returns value of connOpen-metric
func (*Client) ConnOpen() uint64 {
connOpen.Write(m)
return uint64(*m.Gauge.Value)
}
// RequestDuration returns map quantile:value for requestDuration-metric
func (*Client) RequestDuration() map[float64]float64 {
requestDuration.Write(m)
result := make(map[float64]float64, len(m.Summary.Quantile))
for _, v := range m.Summary.Quantile {
result[*v.Quantile] = *v.Value
}
return result
}
// StatusCodes returns map statusCode:value for statusCodes-metric
// where value is an percent of total number of requests
func (c *Client) StatusCodes() map[string]float64 {
result := make(map[string]float64)
total := float64(c.RequestSum())
for _, label := range c.statusCodeLabels {
statusCodes.With(label).Write(m)
result[m.GetLabel()[0].GetValue()] = (*m.Counter.Value / total) * 100
}
return result
}
// ErrorMessages returns map errorMessage:value for errorMessages-metric
// where value is a number of errors with same message
func (c *Client) ErrorMessages() map[string]int {
result := make(map[string]int)
for _, label := range c.errorMessages {
errorMessages.With(label).Write(m)
result[m.GetLabel()[0].GetValue()] = int(*m.Counter.Value)
}
return result
}