import "net/http/httptrace"
Package httptrace provides mechanisms to trace the events within HTTP client requests.
Code:play
req, _ := http.NewRequest("GET", "http://example.com", nil)
trace := &httptrace.ClientTrace{
GotConn: func(connInfo httptrace.GotConnInfo) {
fmt.Printf("Got Conn: %+v\n", connInfo)
},
DNSDone: func(dnsInfo httptrace.DNSDoneInfo) {
fmt.Printf("DNS Info: %+v\n", dnsInfo)
},
}
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
_, err := http.DefaultTransport.RoundTrip(req)
if err != nil {
log.Fatal(err)
}
WithClientTrace returns a new context based on the provided parent ctx. HTTP client requests made with the returned context will use the provided trace hooks, in addition to any previous hooks registered with ctx. Any hooks defined in the provided trace will be called first.
ClientTrace is a set of hooks to run at various stages of an outgoing HTTP request. Any particular hook may be nil. Functions may be called concurrently from different goroutines and some may be called after the request has completed or failed.
ClientTrace currently traces a single HTTP request & response during a single round trip and has no hooks that span a series of redirected requests.
See https://blog.golang.org/http-tracing for more.
ContextClientTrace returns the ClientTrace associated with the provided context. If none, it returns nil.
DNSDoneInfo contains information about the results of a DNS lookup.
DNSStartInfo contains information about a DNS request.
GotConnInfo is the argument to the ClientTrace.GotConn function and contains information about the obtained connection.
WroteRequestInfo contains information provided to the WroteRequest hook.