-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
69 lines (56 loc) · 2.07 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
package thc
import (
"context"
"crypto/tls"
"net/http/httptrace"
"time"
"github.com/paulbellamy/ratecounter"
)
// Metrics exported via expvar. The times are in nanoseconds and are
// averages per minute.
type Metrics struct {
// Time to perform a DNS Lookup.
DNSLookup *ratecounter.AvgRateCounter
// Time to open a new TCP connection.
TCPConnection *ratecounter.AvgRateCounter
// Time to perform a TLS handshake.
TLSHandshake *ratecounter.AvgRateCounter
// Total time to get a ready-to-use connection. This includes the
// previous 3 metrics. It may be zero if you use a connection pool.
GetConnection *ratecounter.AvgRateCounter
// Total time taken to send the HTTP request, including the time
// to get a connection.
WriteRequest *ratecounter.AvgRateCounter
// Total time elapsed until we got the first byte of the response.
// This includes the previous metric.
GetResponse *ratecounter.AvgRateCounter
// Counter indicating how many times per hour was the client out of service.
OutOfService *ratecounter.RateCounter
}
func withTracing(ctx context.Context, m *Metrics) context.Context {
var t0, dnsStart, tcpStart, tlsStart time.Time
return httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{
GetConn: func(_ string) { t0 = time.Now() },
DNSStart: func(_ httptrace.DNSStartInfo) { dnsStart = time.Now() },
DNSDone: func(_ httptrace.DNSDoneInfo) {
m.DNSLookup.Incr(time.Since(dnsStart).Nanoseconds())
},
ConnectStart: func(_, _ string) { tcpStart = time.Now() },
ConnectDone: func(_, _ string, _ error) {
m.TCPConnection.Incr(time.Since(tcpStart).Nanoseconds())
},
TLSHandshakeStart: func() { tlsStart = time.Now() },
TLSHandshakeDone: func(_ tls.ConnectionState, _ error) {
m.TLSHandshake.Incr(time.Since(tlsStart).Nanoseconds())
},
GotConn: func(_ httptrace.GotConnInfo) {
m.GetConnection.Incr(time.Since(t0).Nanoseconds())
},
WroteRequest: func(info httptrace.WroteRequestInfo) {
m.WriteRequest.Incr(time.Since(t0).Nanoseconds())
},
GotFirstResponseByte: func() {
m.GetResponse.Incr(time.Since(t0).Nanoseconds())
},
})
}