-
Notifications
You must be signed in to change notification settings - Fork 1
/
instrumented.go
288 lines (256 loc) · 6.4 KB
/
instrumented.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
package cmhttp
import (
"net/http"
"strconv"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
)
// InstrumentedRequestDurations instruments the client by tracking the request
// durations as histogram vector partitioned by HTTP method (label name
// "method") and response code (label name "code").
//
// InstrumentedRequestDurations will set the Name and Help fields of opts.
func InstrumentedRequestDurations(opts prometheus.HistogramOpts) Decorator {
opts.Name = "request_duration_seconds"
opts.Help = "The HTTP request duration in seconds."
durations := prometheus.NewHistogramVec(opts, []string{"method", "code"})
prometheus.MustRegister(durations)
return func(c Client) Client {
return ClientFunc(func(r *http.Request) (*http.Response, error) {
begin := time.Now()
resp, err := c.Do(r)
if err != nil {
return resp, err
}
elapsed := float64(time.Since(begin)) / float64(time.Second)
method := sanitizeMethod(r.Method)
code := sanitizeCode(resp.StatusCode)
durations.WithLabelValues(method, code).Observe(elapsed)
return resp, err
})
}
}
var metrics struct {
reqCnt *prometheus.CounterVec
reqDur prometheus.Summary
reqSz prometheus.Summary
resSz prometheus.Summary
}
// Instrumented registers and collects the following four metrics with the
// default Registerer:
//
// - http_client_requests_total (CounterVec)
// - http_client_request_duration_seconds (Summary),
// - http_client_request_size_bytes (Summary)
// - http_client_response_size_bytes (Summary)
//
// Each has a constant label named "name" with the provided name as value.
// http_client_requests_total is a metric vector partitioned by HTTP method
// ("method" label) and HTTP status code ("code" label).
//
// This code closely resembles the HTTP server side
// prometheus.InstrumentHandler function.
func Instrumented(name string) Decorator {
return InstrumentedWithOpts(
prometheus.SummaryOpts{
Subsystem: "http_client",
ConstLabels: prometheus.Labels{"name": name},
},
)
}
// InstrumentedWithOpts is like Instrumented but allows changing Namespace (""
// by default) and/or Subsystem ("http_client" by default) and adding
// ConstLabels. All other fields of opts are ignored.
func InstrumentedWithOpts(opts prometheus.SummaryOpts) Decorator {
if metrics.reqCnt == nil {
// make sure we register metrics only once
metrics.reqCnt = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: opts.Namespace,
Subsystem: opts.Subsystem,
Name: "requests_total",
Help: "Total number of HTTP requests made.",
ConstLabels: opts.ConstLabels,
},
[]string{"method", "code"},
)
opts.Name = "request_duration_seconds"
opts.Help = "The HTTP request latencies in seconds."
metrics.reqDur = prometheus.NewSummary(opts)
opts.Name = "request_size_bytes"
opts.Help = "The HTTP request sizes in bytes."
metrics.reqSz = prometheus.NewSummary(opts)
opts.Name = "response_size_bytes"
opts.Help = "The HTTP response sizes in bytes."
metrics.resSz = prometheus.NewSummary(opts)
prometheus.MustRegister(metrics.reqCnt)
prometheus.MustRegister(metrics.reqDur)
prometheus.MustRegister(metrics.reqSz)
prometheus.MustRegister(metrics.resSz)
}
return func(c Client) Client {
return ClientFunc(func(r *http.Request) (*http.Response, error) {
begin := time.Now()
resp, err := c.Do(r)
if err != nil {
return resp, err
}
elapsed := float64(time.Since(begin)) / float64(time.Second)
go func() {
requestSize := computeApproximateRequestSize(r)
method := sanitizeMethod(r.Method)
code := sanitizeCode(resp.StatusCode)
respLengthHeader := resp.Header.Get("Content-Length")
respLength, _ := strconv.Atoi(respLengthHeader) // we can't do anything in case this is not an integer so we ignore this case
metrics.reqCnt.WithLabelValues(method, code).Inc()
metrics.reqDur.Observe(elapsed)
metrics.reqSz.Observe(float64(respLength))
metrics.resSz.Observe(float64(requestSize))
}()
return resp, err
})
}
}
// computeApproximateRequestSize has been mostly copied from the
// prometheus.InstrumentHandler logic.
func computeApproximateRequestSize(r *http.Request) int {
var s int
if r.URL != nil {
s = len(r.URL.String())
}
s += len(r.Method)
s += len(r.Proto)
for name, values := range r.Header {
s += len(name)
for _, value := range values {
s += len(value)
}
}
s += len(r.Host)
// N.B. r.Form and r.MultipartForm are assumed to be included in r.URL.
if r.ContentLength != -1 {
s += int(r.ContentLength)
}
return s
}
// sanitizeMethod normalizes HTTP methods to lowercase. This was copied from
// prometheus.InstrumentHandler logic.
func sanitizeMethod(m string) string {
switch m {
case "GET", "get":
return "get"
case "PUT", "put":
return "put"
case "HEAD", "head":
return "head"
case "POST", "post":
return "post"
case "DELETE", "delete":
return "delete"
case "CONNECT", "connect":
return "connect"
case "OPTIONS", "options":
return "options"
case "NOTIFY", "notify":
return "notify"
default:
return strings.ToLower(m)
}
}
// sanitizeCode maps HTTP response status codes to a string representation.
// This was copied from prometheus.InstrumentHandler logic.
func sanitizeCode(s int) string {
switch s {
case 100:
return "100"
case 101:
return "101"
case 200:
return "200"
case 201:
return "201"
case 202:
return "202"
case 203:
return "203"
case 204:
return "204"
case 205:
return "205"
case 206:
return "206"
case 300:
return "300"
case 301:
return "301"
case 302:
return "302"
case 304:
return "304"
case 305:
return "305"
case 307:
return "307"
case 400:
return "400"
case 401:
return "401"
case 402:
return "402"
case 403:
return "403"
case 404:
return "404"
case 405:
return "405"
case 406:
return "406"
case 407:
return "407"
case 408:
return "408"
case 409:
return "409"
case 410:
return "410"
case 411:
return "411"
case 412:
return "412"
case 413:
return "413"
case 414:
return "414"
case 415:
return "415"
case 416:
return "416"
case 417:
return "417"
case 418:
return "418"
case 500:
return "500"
case 501:
return "501"
case 502:
return "502"
case 503:
return "503"
case 504:
return "504"
case 505:
return "505"
case 428:
return "428"
case 429:
return "429"
case 431:
return "431"
case 511:
return "511"
default:
return strconv.Itoa(s)
}
}