-
Notifications
You must be signed in to change notification settings - Fork 53
/
metrics.go
184 lines (157 loc) · 5.37 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
package http
import (
"context"
"crypto/tls"
"net/http"
"net/http/httptrace"
"time"
"github.com/aws/smithy-go/metrics"
)
var now = time.Now
// withMetrics instruments an HTTP client and context to collect HTTP metrics.
func withMetrics(parent context.Context, client ClientDo, meter metrics.Meter) (
context.Context, ClientDo, error,
) {
hm, err := newHTTPMetrics(meter)
if err != nil {
return nil, nil, err
}
ctx := httptrace.WithClientTrace(parent, &httptrace.ClientTrace{
DNSStart: hm.DNSStart,
ConnectStart: hm.ConnectStart,
TLSHandshakeStart: hm.TLSHandshakeStart,
GotConn: hm.GotConn(parent),
PutIdleConn: hm.PutIdleConn(parent),
ConnectDone: hm.ConnectDone(parent),
DNSDone: hm.DNSDone(parent),
TLSHandshakeDone: hm.TLSHandshakeDone(parent),
GotFirstResponseByte: hm.GotFirstResponseByte(parent),
})
return ctx, &timedClientDo{client, hm}, nil
}
type timedClientDo struct {
ClientDo
hm *httpMetrics
}
func (c *timedClientDo) Do(r *http.Request) (*http.Response, error) {
c.hm.doStart = now()
resp, err := c.ClientDo.Do(r)
c.hm.DoRequestDuration.Record(r.Context(), elapsed(c.hm.doStart))
return resp, err
}
type httpMetrics struct {
DNSLookupDuration metrics.Float64Histogram // client.http.connections.dns_lookup_duration
ConnectDuration metrics.Float64Histogram // client.http.connections.acquire_duration
TLSHandshakeDuration metrics.Float64Histogram // client.http.connections.tls_handshake_duration
ConnectionUsage metrics.Int64UpDownCounter // client.http.connections.usage
DoRequestDuration metrics.Float64Histogram // client.http.do_request_duration
TimeToFirstByte metrics.Float64Histogram // client.http.time_to_first_byte
doStart time.Time
dnsStart time.Time
connectStart time.Time
tlsStart time.Time
}
func newHTTPMetrics(meter metrics.Meter) (*httpMetrics, error) {
hm := &httpMetrics{}
var err error
hm.DNSLookupDuration, err = meter.Float64Histogram("client.http.connections.dns_lookup_duration", func(o *metrics.InstrumentOptions) {
o.UnitLabel = "s"
o.Description = "The time it takes a request to perform DNS lookup."
})
if err != nil {
return nil, err
}
hm.ConnectDuration, err = meter.Float64Histogram("client.http.connections.acquire_duration", func(o *metrics.InstrumentOptions) {
o.UnitLabel = "s"
o.Description = "The time it takes a request to acquire a connection."
})
if err != nil {
return nil, err
}
hm.TLSHandshakeDuration, err = meter.Float64Histogram("client.http.connections.tls_handshake_duration", func(o *metrics.InstrumentOptions) {
o.UnitLabel = "s"
o.Description = "The time it takes an HTTP request to perform the TLS handshake."
})
if err != nil {
return nil, err
}
hm.ConnectionUsage, err = meter.Int64UpDownCounter("client.http.connections.usage", func(o *metrics.InstrumentOptions) {
o.UnitLabel = "{connection}"
o.Description = "Current state of connections pool."
})
if err != nil {
return nil, err
}
hm.DoRequestDuration, err = meter.Float64Histogram("client.http.do_request_duration", func(o *metrics.InstrumentOptions) {
o.UnitLabel = "s"
o.Description = "Time spent performing an entire HTTP transaction."
})
if err != nil {
return nil, err
}
hm.TimeToFirstByte, err = meter.Float64Histogram("client.http.time_to_first_byte", func(o *metrics.InstrumentOptions) {
o.UnitLabel = "s"
o.Description = "Time from start of transaction to when the first response byte is available."
})
if err != nil {
return nil, err
}
return hm, nil
}
func (m *httpMetrics) DNSStart(httptrace.DNSStartInfo) {
m.dnsStart = now()
}
func (m *httpMetrics) ConnectStart(string, string) {
m.connectStart = now()
}
func (m *httpMetrics) TLSHandshakeStart() {
m.tlsStart = now()
}
func (m *httpMetrics) GotConn(ctx context.Context) func(httptrace.GotConnInfo) {
return func(httptrace.GotConnInfo) {
m.addConnAcquired(ctx, 1)
}
}
func (m *httpMetrics) PutIdleConn(ctx context.Context) func(error) {
return func(error) {
m.addConnAcquired(ctx, -1)
}
}
func (m *httpMetrics) DNSDone(ctx context.Context) func(httptrace.DNSDoneInfo) {
return func(httptrace.DNSDoneInfo) {
m.DNSLookupDuration.Record(ctx, elapsed(m.dnsStart))
}
}
func (m *httpMetrics) ConnectDone(ctx context.Context) func(string, string, error) {
return func(string, string, error) {
m.ConnectDuration.Record(ctx, elapsed(m.connectStart))
}
}
func (m *httpMetrics) TLSHandshakeDone(ctx context.Context) func(tls.ConnectionState, error) {
return func(tls.ConnectionState, error) {
m.TLSHandshakeDuration.Record(ctx, elapsed(m.tlsStart))
}
}
func (m *httpMetrics) GotFirstResponseByte(ctx context.Context) func() {
return func() {
m.TimeToFirstByte.Record(ctx, elapsed(m.doStart))
}
}
func (m *httpMetrics) addConnAcquired(ctx context.Context, incr int64) {
m.ConnectionUsage.Add(ctx, incr, func(o *metrics.RecordMetricOptions) {
o.Properties.Set("state", "acquired")
})
}
// Not used: it is recommended to track acquired vs idle conn, but we can't
// determine when something is truly idle with the current HTTP client hooks
// available to us.
func (m *httpMetrics) addConnIdle(ctx context.Context, incr int64) {
m.ConnectionUsage.Add(ctx, incr, func(o *metrics.RecordMetricOptions) {
o.Properties.Set("state", "idle")
})
}
func elapsed(start time.Time) float64 {
end := now()
elapsed := end.Sub(start)
return float64(elapsed) / 1e9
}