httptrace

import “net/http/httptrace”
httptrace包提供跟踪HTTP客户端请求中的事件的机制。

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



https://cloud.tencent.com/developer/section/1143642


Category golang