Documentation
¶
Overview ¶
Package httptrace provides mechanisms to trace the events within HTTP client requests.
Example ¶
package main import ( "fmt" "log" "net/http" "net/http/httptrace" ) func main() { 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) } }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithClientTrace ¶
func WithClientTrace(ctx context.Context, trace *ClientTrace) context.Context
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.
Types ¶
type ClientTrace ¶
type ClientTrace struct { GetConn func(hostPort string) GotConn func(GotConnInfo) PutIdleConn func(err error) GotFirstResponseByte func() Got100Continue func() Got1xxResponse func(code int, header textproto.MIMEHeader) error DNSStart func(DNSStartInfo) DNSDone func(DNSDoneInfo) ConnectStart func(network, addr string) ConnectDone func(network, addr string, err error) TLSHandshakeStart func() TLSHandshakeDone func(tls.ConnectionState, error) WroteHeaderField func(key string, value []string) WroteHeaders func() Wait100Continue func() WroteRequest func(WroteRequestInfo) }
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.
func ContextClientTrace ¶
func ContextClientTrace(ctx context.Context) *ClientTrace
ContextClientTrace returns the ClientTrace associated with the provided context. If none, it returns nil.
type DNSDoneInfo ¶
DNSDoneInfo contains information about the results of a DNS lookup.
type DNSStartInfo ¶
type DNSStartInfo struct {
Host string
}
DNSStartInfo contains information about a DNS request.
type GotConnInfo ¶
GotConnInfo is the argument to the ClientTrace.GotConn function and contains information about the obtained connection.
type WroteRequestInfo ¶
type WroteRequestInfo struct {
Err error
}
WroteRequestInfo contains information provided to the WroteRequest hook.