/
trace.go
95 lines (83 loc) · 2.67 KB
/
trace.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
package main
import (
"context"
"crypto/tls"
"net/http/httptrace"
"time"
)
// RequestTrace provides the trace time stamps of the HTTP request's segments.
type RequestTrace struct {
context.Context
Start, Finish time.Time
Reused bool
DNSStart, DNSDone time.Time
ConnectStart, ConnectDone time.Time
TLSHandshakeStart, TLSHandshakeDone time.Time
RequestWritten time.Time
FirstResponseByte time.Time
}
// NewRequestTrace returns a initialized RequestTrace for an
// httptrace.ClientTrace, based on the context passed.
func NewRequestTrace(ctx context.Context) *RequestTrace {
rt := &RequestTrace{
Start: time.Now(),
}
trace := &httptrace.ClientTrace{
GetConn: rt.getConn,
GotConn: rt.gotConn,
PutIdleConn: rt.putIdleConn,
GotFirstResponseByte: rt.gotFirstResponseByte,
Got100Continue: rt.got100Continue,
DNSStart: rt.dnsStart,
DNSDone: rt.dnsDone,
ConnectStart: rt.connectStart,
ConnectDone: rt.connectDone,
TLSHandshakeStart: rt.tlsHandshakeStart,
TLSHandshakeDone: rt.tlsHandshakeDone,
WroteHeaders: rt.wroteHeaders,
Wait100Continue: rt.wait100Continue,
WroteRequest: rt.wroteRequest,
}
rt.Context = httptrace.WithClientTrace(ctx, trace)
return rt
}
// TotalLatency returns the total time the request took.
func (rt *RequestTrace) TotalLatency() time.Duration {
return rt.Finish.Sub(rt.Start)
}
// RequestDone completes the request trace.
func (rt *RequestTrace) RequestDone() {
rt.Finish = time.Now()
}
func (rt *RequestTrace) getConn(hostPort string) {}
func (rt *RequestTrace) gotConn(info httptrace.GotConnInfo) {
rt.Reused = info.Reused
}
func (rt *RequestTrace) putIdleConn(err error) {}
func (rt *RequestTrace) gotFirstResponseByte() {
rt.FirstResponseByte = time.Now()
}
func (rt *RequestTrace) got100Continue() {}
func (rt *RequestTrace) dnsStart(info httptrace.DNSStartInfo) {
rt.DNSStart = time.Now()
}
func (rt *RequestTrace) dnsDone(info httptrace.DNSDoneInfo) {
rt.DNSDone = time.Now()
}
func (rt *RequestTrace) connectStart(network, addr string) {
rt.ConnectStart = time.Now()
}
func (rt *RequestTrace) connectDone(network, addr string, err error) {
rt.ConnectDone = time.Now()
}
func (rt *RequestTrace) tlsHandshakeStart() {
rt.TLSHandshakeStart = time.Now()
}
func (rt *RequestTrace) tlsHandshakeDone(state tls.ConnectionState, err error) {
rt.TLSHandshakeDone = time.Now()
}
func (rt *RequestTrace) wroteHeaders() {}
func (rt *RequestTrace) wait100Continue() {}
func (rt *RequestTrace) wroteRequest(info httptrace.WroteRequestInfo) {
rt.RequestWritten = time.Now()
}