Movatterモバイル変換


[0]ホーム

URL:


httptrace

packagestandard library
go1.25.2Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 7, 2025 License:BSD-3-ClauseImports:7Imported by:2,989

Details

Repository

cs.opensource.google/go/go

Links

Documentation

Overview

Package httptrace provides mechanisms to trace the events withinHTTP client requests.

Example
package mainimport ("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)}}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

funcWithClientTrace

func WithClientTrace(ctxcontext.Context, trace *ClientTrace)context.Context

WithClientTrace returns a new context based on the provided parentctx. HTTP client requests made with the returned context will usethe provided trace hooks, in addition to any previous hooksregistered with ctx. Any hooks defined in the provided trace willbe called first.

Types

typeClientTrace

type ClientTrace struct {// GetConn is called before a connection is created or// retrieved from an idle pool. The hostPort is the// "host:port" of the target or proxy. GetConn is called even// if there's already an idle cached connection available.GetConn func(hostPortstring)// GotConn is called after a successful connection is// obtained. There is no hook for failure to obtain a// connection; instead, use the error from// Transport.RoundTrip.GotConn func(GotConnInfo)// PutIdleConn is called when the connection is returned to// the idle pool. If err is nil, the connection was// successfully returned to the idle pool. If err is non-nil,// it describes why not. PutIdleConn is not called if// connection reuse is disabled via Transport.DisableKeepAlives.// PutIdleConn is called before the caller's Response.Body.Close// call returns.// For HTTP/2, this hook is not currently used.PutIdleConn func(errerror)// GotFirstResponseByte is called when the first byte of the response// headers is available.GotFirstResponseByte func()// Got100Continue is called if the server replies with a "100// Continue" response.Got100Continue func()// Got1xxResponse is called for each 1xx informational response header// returned before the final non-1xx response. Got1xxResponse is called// for "100 Continue" responses, even if Got100Continue is also defined.// If it returns an error, the client request is aborted with that error value.Got1xxResponse func(codeint, headertextproto.MIMEHeader)error// DNSStart is called when a DNS lookup begins.DNSStart func(DNSStartInfo)// DNSDone is called when a DNS lookup ends.DNSDone func(DNSDoneInfo)// ConnectStart is called when a new connection's Dial begins.// If net.Dialer.DualStack (IPv6 "Happy Eyeballs") support is// enabled, this may be called multiple times.ConnectStart func(network, addrstring)// ConnectDone is called when a new connection's Dial// completes. The provided err indicates whether the// connection completed successfully.// If net.Dialer.DualStack ("Happy Eyeballs") support is// enabled, this may be called multiple times.ConnectDone func(network, addrstring, errerror)// TLSHandshakeStart is called when the TLS handshake is started. When// connecting to an HTTPS site via an HTTP proxy, the handshake happens// after the CONNECT request is processed by the proxy.TLSHandshakeStart func()// TLSHandshakeDone is called after the TLS handshake with either the// successful handshake's connection state, or a non-nil error on handshake// failure.TLSHandshakeDone func(tls.ConnectionState,error)// WroteHeaderField is called after the Transport has written// each request header. At the time of this call the values// might be buffered and not yet written to the network.WroteHeaderField func(keystring, value []string)// WroteHeaders is called after the Transport has written// all request headers.WroteHeaders func()// Wait100Continue is called if the Request specified// "Expect: 100-continue" and the Transport has written the// request headers but is waiting for "100 Continue" from the// server before writing the request body.Wait100Continue func()// WroteRequest is called with the result of writing the// request and any body. It may be called multiple times// in the case of retried requests.WroteRequest func(WroteRequestInfo)}

ClientTrace is a set of hooks to run at various stages of an outgoingHTTP request. Any particular hook may be nil. Functions may becalled concurrently from different goroutines and some may be calledafter the request has completed or failed.

ClientTrace currently traces a single HTTP request & responseduring a single round trip and has no hooks that span a seriesof redirected requests.

Seehttps://blog.golang.org/http-tracing for more.

funcContextClientTrace

func ContextClientTrace(ctxcontext.Context) *ClientTrace

ContextClientTrace returns theClientTrace associated with theprovided context. If none, it returns nil.

typeDNSDoneInfo

type DNSDoneInfo struct {// Addrs are the IPv4 and/or IPv6 addresses found in the DNS// lookup. The contents of the slice should not be mutated.Addrs []net.IPAddr// Err is any error that occurred during the DNS lookup.Errerror// Coalesced is whether the Addrs were shared with another// caller who was doing the same DNS lookup concurrently.Coalescedbool}

DNSDoneInfo contains information about the results of a DNS lookup.

typeDNSStartInfo

type DNSStartInfo struct {Hoststring}

DNSStartInfo contains information about a DNS request.

typeGotConnInfo

type GotConnInfo struct {// Conn is the connection that was obtained. It is owned by// the http.Transport and should not be read, written or// closed by users of ClientTrace.Connnet.Conn// Reused is whether this connection has been previously// used for another HTTP request.Reusedbool// WasIdle is whether this connection was obtained from an// idle pool.WasIdlebool// IdleTime reports how long the connection was previously// idle, if WasIdle is true.IdleTimetime.Duration}

GotConnInfo is the argument to the [ClientTrace.GotConn] function andcontains information about the obtained connection.

typeWroteRequestInfo

type WroteRequestInfo struct {// Err is any error encountered while writing the Request.Errerror}

WroteRequestInfo contains information provided to the WroteRequesthook.

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp