/
tracer.go
75 lines (63 loc) Β· 1.65 KB
/
tracer.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
package main
import (
"context"
"crypto/tls"
"fmt"
"net/http/httptrace"
"time"
)
type tracer struct {
tDNS time.Time
dDNS time.Duration
tTCP time.Time
dTCP time.Duration
tTLS time.Time
dTLS time.Duration
tHTTP time.Time
dHTTP time.Duration
}
func (t *tracer) GetConn(hostPort string) {
// fmt.Printf("Get Conn: %+v\n", hostPort)
}
func (t *tracer) DNSStart(dnsInfo httptrace.DNSStartInfo) {
t.tDNS = time.Now()
}
func (t *tracer) DNSDone(dnsInfo httptrace.DNSDoneInfo) {
t.dDNS = time.Now().Sub(t.tDNS)
fmt.Printf("dns : %s\n", t.dDNS)
}
func (t *tracer) ConnectStart(network, addr string) {
t.tTCP = time.Now()
}
func (t *tracer) ConnectDone(network, addr string, err error) {
t.dTCP = time.Now().Sub(t.tTCP)
fmt.Printf("tcp : %s\n", t.dTCP)
}
func (t *tracer) TLSHandshakeStart() {
t.tTLS = time.Now()
}
func (t *tracer) TLSHandshakeDone(info tls.ConnectionState, err error) {
t.dTLS = time.Now().Sub(t.tTLS)
fmt.Printf("tls : %s\n", t.dTCP)
}
func (t *tracer) GotConn(connInfo httptrace.GotConnInfo) {
t.tHTTP = time.Now()
}
func (t *tracer) GotFirstResponseByte() {
t.dHTTP = time.Now().Sub(t.tHTTP)
fmt.Printf("htt : %s\n", t.dHTTP)
}
func (t *tracer) Context(ctx context.Context) context.Context {
trace := &httptrace.ClientTrace{
GetConn: t.GetConn,
DNSStart: t.DNSStart,
DNSDone: t.DNSDone,
ConnectStart: t.ConnectStart,
ConnectDone: t.ConnectDone,
TLSHandshakeStart: t.TLSHandshakeStart,
TLSHandshakeDone: t.TLSHandshakeDone,
GotConn: t.GotConn,
GotFirstResponseByte: t.GotFirstResponseByte,
}
return httptrace.WithClientTrace(ctx, trace)
}