-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
http_trace.go
37 lines (32 loc) · 1.04 KB
/
http_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
package colly
import (
"net/http"
"net/http/httptrace"
"time"
)
// HTTPTrace provides a datastructure for storing an http trace.
type HTTPTrace struct {
start, connect time.Time
ConnectDuration time.Duration
FirstByteDuration time.Duration
}
// trace returns a httptrace.ClientTrace object to be used with an http
// request via httptrace.WithClientTrace() that fills in the HttpTrace.
func (ht *HTTPTrace) trace() *httptrace.ClientTrace {
trace := &httptrace.ClientTrace{
ConnectStart: func(network, addr string) { ht.connect = time.Now() },
ConnectDone: func(network, addr string, err error) {
ht.ConnectDuration = time.Since(ht.connect)
},
GetConn: func(hostPort string) { ht.start = time.Now() },
GotFirstResponseByte: func() {
ht.FirstByteDuration = time.Since(ht.start)
},
}
return trace
}
// WithTrace returns the given HTTP Request with this HTTPTrace added to its
// context.
func (ht *HTTPTrace) WithTrace(req *http.Request) *http.Request {
return req.WithContext(httptrace.WithClientTrace(req.Context(), ht.trace()))
}