forked from davecheney/httpstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
querier.go
304 lines (249 loc) · 8.67 KB
/
querier.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
package main
import (
"context"
"crypto/tls"
"fmt"
"io"
"log"
"math/rand"
"net"
"net/http"
"net/http/httptrace"
"net/url"
"strconv"
"strings"
"time"
"github.com/VictoriaMetrics/metrics"
"golang.org/x/exp/slog"
)
const (
lookupTotalName = "httpstat_lookup_total"
errorsTotalName = "httpstat_errors_total"
dnsLookupDurationName = "httpstat_dns_lookup_duration_seconds"
tcpConnDurationName = "httpstat_tcp_connection_duration_seconds"
tlsHandshakeDurationName = "httpstat_tls_handshake_duration_seconds"
serverProcessingDurationName = "httpstat_server_processing_duration_seconds"
contentTransferDurationName = "httpstat_content_transfer_duration_seconds"
totalDurationDurationName = "httpstat_total_duration_seconds"
)
// Querier A querier will periodically measure the host specified in its
// config, and will export the observations as prometheus metrics
type Querier struct {
ctx context.Context
httpServerConfig *HTTPServerConfig
labels string
url url.URL
trsp *http.Transport
lgr *slog.Logger
mS *metrics.Set
}
func newQuerier(ctx context.Context, config HTTPServerConfig, lgr *slog.Logger, insecure bool, tlsCerts []tls.Certificate) (*Querier, error) {
var q = Querier{
ctx: ctx,
httpServerConfig: &config,
url: *parseURL(config.URL),
lgr: lgr,
}
q.lgr = q.lgr.With(
"url", config.URL,
"scheme", q.url.Scheme,
"ip_version", q.httpServerConfig.IPVersion,
)
if len(config.Host) > 0 {
q.lgr = q.lgr.With(
"host", config.Host,
)
}
q.trsp = &http.Transport{
Proxy: http.ProxyFromEnvironment,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
ForceAttemptHTTP2: true,
}
if q.url.Scheme == "https" {
var host string
var err error
if len(config.Host) > 0 {
host = config.Host
} else {
host, _, err = net.SplitHostPort(q.url.Host)
}
if err != nil {
host = q.url.Host
}
q.trsp.TLSClientConfig = &tls.Config{
ServerName: host,
InsecureSkipVerify: insecure, //nolint:gosec // not a security concern as we are not actually sending/reading data
Certificates: tlsCerts,
RootCAs: caCertPool,
MinVersion: tls.VersionTLS12,
}
}
var network string
switch q.httpServerConfig.IPVersion {
case "4":
network = "tcp4"
case "6":
network = "tcp6"
case "any":
network = "tcp"
default:
return nil, fmt.Errorf("ip version not configured properly. must be either 4, 6, any")
}
q.trsp.DialContext = func(ctx context.Context, _, addr string) (net.Conn, error) {
return (&net.Dialer{
Timeout: dialerTimeout,
DualStack: false,
}).DialContext(ctx, network, addr)
}
labelsMap := q.httpServerConfig.ExtraLabels
if len(q.httpServerConfig.Host) > 0 {
labelsMap["host"] = q.httpServerConfig.Host
}
labelsMap["url"] = q.httpServerConfig.URL
labelsMap["scheme"] = q.url.Scheme
labelsMap["ip_version"] = q.httpServerConfig.IPVersion
for label, value := range labelsMap {
q.labels += fmt.Sprintf("%s=%q,", label, value)
}
if len(additionalLabels) > 0 {
q.labels += fmt.Sprintf("%s,", additionalLabels)
}
q.labels = q.labels[0 : len(q.labels)-1]
q.mS = metrics.NewSet()
q.mS.GetOrCreateCounter(fmt.Sprintf("%s{%s}", lookupTotalName, q.labels)).Set(0)
q.mS.GetOrCreateCounter(fmt.Sprintf("%s{%s}", errorsTotalName, q.labels)).Set(0)
metrics.RegisterSet(q.mS)
return &q, nil
}
// Run starts the querier at the specified interval, with a random jitter of 0-interval
func (q *Querier) Run(interval *time.Duration) {
//nolint:gosec // No need for a cryptographic secure random number since this is only used for a jitter.
jitter := time.Duration(rand.Float64() * float64(*interval))
q.lgr.Info("start a new querier with some delay",
"jitter", jitter,
)
time.Sleep(jitter)
ticker := time.NewTicker(*interval)
defer ticker.Stop()
loop:
for {
select {
case <-ticker.C:
q.visit()
case <-q.ctx.Done():
break loop
}
}
q.mS.UnregisterAllMetrics()
}
// visit visits a url and times the interaction.
// If the response is a 30x, visit follows the redirect.
//
//nolint:funlen // the httpTrace declaration takes a lot of lines, that cannot be moved away from this function
func (q *Querier) visit() {
req, _ := http.NewRequest("GET", q.url.String(), http.NoBody) // we ignore the err as the URL has already been parsed and is therefore valid
req.Header = q.httpServerConfig.HTTPHeaders
if q.httpServerConfig.Host != "" {
req.Host = q.httpServerConfig.Host
}
var getConnTime, dnsStartTime, dnsDoneTime, connectStartTime, connectDoneTime,
gotConnTime, lastGotConnTime, gotFirstResponseByteTime, tlsHandshakeStartTime,
tlsHandshakeStopTime time.Time
trace := &httptrace.ClientTrace{
GetConn: func(_ string) {
if getConnTime.IsZero() {
getConnTime = time.Now()
}
},
DNSStart: func(_ httptrace.DNSStartInfo) { dnsStartTime = time.Now() },
DNSDone: func(_ httptrace.DNSDoneInfo) { dnsDoneTime = time.Now() },
ConnectStart: func(_, _ string) {
if connectStartTime.IsZero() {
connectStartTime = time.Now()
}
},
ConnectDone: func(_, _ string, _ error) {
if connectDoneTime.IsZero() {
connectDoneTime = time.Now()
}
lastGotConnTime = time.Now()
},
TLSHandshakeStart: func() { tlsHandshakeStartTime = time.Now() },
TLSHandshakeDone: func(_ tls.ConnectionState, _ error) { tlsHandshakeStopTime = time.Now() },
GotConn: func(_ httptrace.GotConnInfo) {
if gotConnTime.IsZero() {
gotConnTime = time.Now()
}
lastGotConnTime = time.Now()
},
GotFirstResponseByte: func() {
// we only care about the last time it happens (in case there were redirects for example)
gotFirstResponseByteTime = time.Now()
},
}
l := q.labels
client := &http.Client{
Transport: q.trsp,
}
req = req.WithContext(httptrace.WithClientTrace(q.ctx, trace))
resp, err := client.Do(req)
defer q.trsp.CloseIdleConnections()
if q.ctx.Err() != nil { // the context was most likely canceled, no need to take the measurements into account
return
}
if err != nil {
q.mS.GetOrCreateCounter(fmt.Sprintf("%s{%s}", lookupTotalName, l)).Inc()
q.mS.GetOrCreateCounter(fmt.Sprintf("%s{%s}", errorsTotalName, l)).Inc()
q.lgr.Info("error during query", "error", err)
return
}
l += fmt.Sprintf(",%s=%q", "status_code", strconv.Itoa(resp.StatusCode))
q.mS.GetOrCreateCounter(fmt.Sprintf("%s{%s}", lookupTotalName, l)).Inc()
bodySize, _ := io.Copy(io.Discard, resp.Body)
resp.Body.Close()
postBodyReadTime := time.Now()
l += fmt.Sprintf("%s=%q", ",body_size", strconv.FormatInt(bodySize, 10))
dnsLookupDuration := dnsDoneTime.Sub(dnsStartTime)
tcpConnectDuration := connectDoneTime.Sub(connectStartTime)
tlsHandshakeDuration := tlsHandshakeStopTime.Sub(tlsHandshakeStartTime)
serverProcessingDuration := gotFirstResponseByteTime.Sub(lastGotConnTime)
contentTransferDuration := postBodyReadTime.Sub(gotFirstResponseByteTime)
totalDuration := postBodyReadTime.Sub(getConnTime)
if !dnsStartTime.IsZero() { // we only record this metric if a DNS lookup was actually made
q.mS.GetOrCreateHistogram(fmt.Sprintf("%s{%s}", dnsLookupDurationName, l)).Update(dnsLookupDuration.Seconds())
}
q.mS.GetOrCreateHistogram(fmt.Sprintf("%s{%s}", tcpConnDurationName, l)).Update(tcpConnectDuration.Seconds())
if !tlsHandshakeStartTime.IsZero() { // we only record this q.mS when a TLS handshake was done
q.mS.GetOrCreateHistogram(fmt.Sprintf("%s{%s}", tlsHandshakeDurationName, l)).Update(tlsHandshakeDuration.Seconds())
}
q.mS.GetOrCreateHistogram(fmt.Sprintf("%s{%s}", serverProcessingDurationName, l)).Update(serverProcessingDuration.Seconds())
q.mS.GetOrCreateHistogram(fmt.Sprintf("%s{%s}", contentTransferDurationName, l)).Update(contentTransferDuration.Seconds())
q.mS.GetOrCreateHistogram(fmt.Sprintf("%s{%s}", totalDurationDurationName, l)).Update(totalDuration.Seconds())
q.lgr.Debug("new measurement",
dnsLookupDurationName, dnsLookupDuration,
tcpConnDurationName, tcpConnectDuration,
tlsHandshakeDurationName, tlsHandshakeDuration,
serverProcessingDurationName, serverProcessingDuration,
contentTransferDurationName, contentTransferDuration,
totalDurationDurationName, totalDuration,
)
}
func parseURL(uri string) *url.URL {
if !strings.Contains(uri, "://") && !strings.HasPrefix(uri, "//") {
uri = "//" + uri
}
parsedURL, err := url.Parse(uri)
if err != nil {
log.Fatalf("could not parse url %q: %v", uri, err)
}
if parsedURL.Scheme == "" {
parsedURL.Scheme = "http"
if !strings.HasSuffix(parsedURL.Host, ":80") {
parsedURL.Scheme += "s"
}
}
return parsedURL
}