forked from winterssy/ghttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace.go
123 lines (108 loc) · 3.64 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
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
package ghttp
import (
"crypto/tls"
"net/http/httptrace"
"time"
)
type (
clientTrace struct {
start time.Time
dnsStart time.Time
dnsDone time.Time
connStart time.Time
connDone time.Time
tlsHandshakeStart time.Time
tlsHandshakeDone time.Time
gotFirstResponseByte time.Time
wroteRequest time.Time
end time.Time
getConn time.Time
gotConn time.Time
gotConnInfo httptrace.GotConnInfo
}
// TraceInfo is used to provide request trace info such as DNS lookup
// duration, Connection obtain duration, Server processing duration, etc.
TraceInfo struct {
// DNSLookupTime is a duration that transport took to perform
// DNS lookup.
DNSLookupTime time.Duration `json:"dns_lookup_time"`
// TCPConnTime is a duration that TCP connection took place.
TCPConnTime time.Duration `json:"tcp_conn_time"`
// TLSHandshakeTime is a duration that TLS handshake took place.
TLSHandshakeTime time.Duration `json:"tls_handshake_time,omitempty"`
// ConnTime is a duration that took to obtain a successful connection.
ConnTime time.Duration `json:"conn_time"`
// ServerTime is a duration that server took to respond first byte.
ServerTime time.Duration `json:"server_time"`
// ResponseTime is a duration since first response byte from server to
// request completion.
ResponseTime time.Duration `json:"response_time"`
// TotalTime is a duration that total request took end-to-end.
TotalTime time.Duration `json:"total_time"`
// ConnReused reports whether this connection has been previously
// used for another HTTP request.
ConnReused bool `json:"conn_reused"`
// ConnWasIdle reports whether this connection was obtained from an
// idle pool.
ConnWasIdle bool `json:"conn_was_idle"`
// ConnIdleTime is a duration how long the connection was previously
// idle, if ConnWasIdle is true.
ConnIdleTime time.Duration `json:"conn_idle_time"`
}
)
func (ct *clientTrace) modifyRequest(req *Request) {
ctx := httptrace.WithClientTrace(
req.Context(),
&httptrace.ClientTrace{
GetConn: func(_ string) {
ct.getConn = time.Now()
},
GotConn: func(gotConnInfo httptrace.GotConnInfo) {
ct.gotConn = time.Now()
ct.gotConnInfo = gotConnInfo
},
GotFirstResponseByte: func() {
ct.gotFirstResponseByte = time.Now()
},
DNSStart: func(_ httptrace.DNSStartInfo) {
ct.dnsStart = time.Now()
},
DNSDone: func(_ httptrace.DNSDoneInfo) {
ct.dnsDone = time.Now()
},
ConnectStart: func(network, addr string) {
ct.connStart = time.Now()
},
ConnectDone: func(network, addr string, err error) {
ct.connDone = time.Now()
},
TLSHandshakeStart: func() {
ct.tlsHandshakeStart = time.Now()
},
TLSHandshakeDone: func(_ tls.ConnectionState, _ error) {
ct.tlsHandshakeDone = time.Now()
},
WroteRequest: func(_ httptrace.WroteRequestInfo) {
ct.wroteRequest = time.Now()
},
},
)
req.Request = req.WithContext(ctx)
}
func (ct *clientTrace) done() {
ct.end = time.Now()
}
func (ct *clientTrace) traceInfo() *TraceInfo {
return &TraceInfo{
DNSLookupTime: ct.dnsDone.Sub(ct.dnsStart),
TCPConnTime: ct.connDone.Sub(ct.connStart),
TLSHandshakeTime: ct.tlsHandshakeDone.Sub(ct.tlsHandshakeStart),
ConnTime: ct.gotConn.Sub(ct.getConn),
ServerTime: ct.gotFirstResponseByte.Sub(ct.wroteRequest),
ResponseTime: ct.end.Sub(ct.gotFirstResponseByte),
TotalTime: ct.end.Sub(ct.start),
ConnReused: ct.gotConnInfo.Reused,
ConnWasIdle: ct.gotConnInfo.WasIdle,
ConnIdleTime: ct.gotConnInfo.IdleTime,
}
}