目录 搜索
archive archive/tar archive/zip bufio bufio(缓存) builtin builtin(内置包) bytes bytes(包字节) compress compress/bzip2(压缩/bzip2) compress/flate(压缩/flate) compress/gzip(压缩/gzip) compress/lzw(压缩/lzw) compress/zlib(压缩/zlib) container container/heap(容器数据结构heap) container/list(容器数据结构list) container/ring(容器数据结构ring) context context(上下文) crypto crypto(加密) crypto/aes(加密/aes) crypto/cipher(加密/cipher) crypto/des(加密/des) crypto/dsa(加密/dsa) crypto/ecdsa(加密/ecdsa) crypto/elliptic(加密/elliptic) crypto/hmac(加密/hmac) crypto/md5(加密/md5) crypto/rand(加密/rand) crypto/rc4(加密/rc4) crypto/rsa(加密/rsa) crypto/sha1(加密/sha1) crypto/sha256(加密/sha256) crypto/sha512(加密/sha512) crypto/subtle(加密/subtle) crypto/tls(加密/tls) crypto/x509(加密/x509) crypto/x509/pkix(加密/x509/pkix) database database/sql(数据库/sql) database/sql/driver(数据库/sql/driver) debug debug/dwarf(调试/dwarf) debug/elf(调试/elf) debug/gosym(调试/gosym) debug/macho(调试/macho) debug/pe(调试/pe) debug/plan9obj(调试/plan9obj) encoding encoding(编码) encoding/ascii85(编码/ascii85) encoding/asn1(编码/asn1) encoding/base32(编码/base32) encoding/base64(编码/base64) encoding/binary(编码/binary) encoding/csv(编码/csv) encoding/gob(编码/gob) encoding/hex(编码/hex) encoding/json(编码/json) encoding/pem(编码/pem) encoding/xml(编码/xml) errors errors(错误) expvar expvar flag flag(命令行参数解析flag包) fmt fmt go go/ast(抽象语法树) go/build go/constant(常量) go/doc(文档) go/format(格式) go/importer go/parser go/printer go/scanner(扫描仪) go/token(令牌) go/types(类型) hash hash(散列) hash/adler32 hash/crc32 hash/crc64 hash/fnv html html html/template(模板) image image(图像) image/color(颜色) image/color/palette(调色板) image/draw(绘图) image/gif image/jpeg image/png index index/suffixarray io io io/ioutil log log log/syslog(日志系统) math math math/big math/big math/bits math/bits math/cmplx math/cmplx math/rand math/rand mime mime mime/multipart(多部分) mime/quotedprintable net net net/http net/http net/http/cgi net/http/cookiejar net/http/fcgi net/http/httptest net/http/httptrace net/http/httputil net/http/internal net/http/pprof net/mail net/mail net/rpc net/rpc net/rpc/jsonrpc net/smtp net/smtp net/textproto net/textproto net/url net/url os os os/exec os/signal os/user path path path/filepath(文件路径) plugin plugin(插件) reflect reflect(反射) regexp regexp(正则表达式) regexp/syntax runtime runtime(运行时) runtime/debug(调试) runtime/internal/sys runtime/pprof runtime/race(竞争) runtime/trace(执行追踪器) sort sort(排序算法) strconv strconv(转换) strings strings(字符串) sync sync(同步) sync/atomic(原子操作) syscall syscall(系统调用) testing testing(测试) testing/iotest testing/quick text text/scanner(扫描文本) text/tabwriter text/template(定义模板) text/template/parse time time(时间戳) unicode unicode unicode/utf16 unicode/utf8 unsafe unsafe
文字

  • import "net/http/httptrace"

  • 概述

  • 索引

  • 示例

概述

httptrace包提供跟踪HTTP客户端请求中的事件的机制。

示例

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)}}

索引

  • func WithClientTrace(ctx context.Context, trace *ClientTrace) context.Context

  • type ClientTrace

  • func ContextClientTrace(ctx context.Context) *ClientTrace

  • type DNSDoneInfo

  • type DNSStartInfo

  • type GotConnInfo

  • type WroteRequestInfo

示例

文件包

trace.go

func WithClientTrace

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

WithClientTrace根据提供的父ctx返回一个新的上下文。除了使用ctx注册的任何以前的挂钩外,使用返回的上下文创建的HTTP客户端请求将使用提供的跟踪挂钩。在提供的轨迹中定义的任何钩子将首先被调用。

type ClientTrace

ClientTrace是一组挂钩,可以在传出的HTTP请求的各个阶段运行。任何特定的钩子可能是零。函数可以从不同的goroutine同时调用,有些可能在请求完成或失败后调用。

ClientTrace当前在单次往返过程中跟踪单个HTTP请求和响应,并且没有跨越一系列重定向请求的挂钩。

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(hostPort string)        // 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(err error)        // 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()        // 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, addr string)        // ConnectDone is called when a new connection's Dial        // completes. The provided err indicates whether the        // connection completedly successfully.        // If net.Dialer.DualStack ("Happy Eyeballs") support is        // enabled, this may be called multiple times.
        ConnectDone func(network, addr string, err error)        // TLSHandshakeStart is called when the TLS handshake is started. When        // connecting to a HTTPS site via a 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)        // WroteHeaders is called after the Transport has written        // the request headers.
        WroteHeaders func()        // Wait100Continue is called if the Request specified        // "Expected: 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)}

func ContextClientTrace

func ContextClientTrace(ctx context.Context) *ClientTrace

ContextClientTrace返回与提供的上下文关联的ClientTrace。如果没有,则返回零。

type DNSDoneInfo

DNSDoneInfo包含有关DNS查找结果的信息。

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.
        Err error        // Coalesced is whether the Addrs were shared with another        // caller who was doing the same DNS lookup concurrently.
        Coalesced bool}

type DNSStartInfo

DNSStartInfo包含有关DNS请求的信息。

type DNSStartInfo struct {
        Host string}

type GotConnInfo

GotConnInfo是ClientTrace.GotConn函数的参数,并包含有关获取的连接的信息。

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.
        Conn net.Conn        // Reused is whether this connection has been previously        // used for another HTTP request.
        Reused bool        // WasIdle is whether this connection was obtained from an        // idle pool.
        WasIdle bool        // IdleTime reports how long the connection was previously        // idle, if WasIdle is true.
        IdleTime time.Duration}

type WroteRequestInfo

WroteRequestInfo包含提供给WroteRequest钩子的信息。

type WroteRequestInfo struct {        // Err is any error encountered while writing the Request.
        Err error}
上一篇: 下一篇: