Created
December 15, 2024 17:26
-
-
Save Abdulsametileri/b682d1895e69f2c42bf56aa20a6cfab9 to your computer and use it in GitHub Desktop.
httptrace.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a new request for the call. | |
req, err := http.NewRequest("GET", "https://google.com.tr", nil) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
// Create a ClientTrace value for the events we care about. | |
trace := httptrace.ClientTrace{ | |
GetConn: func(hostPort string) { | |
log.Printf("Get Conn: %s\n", hostPort) | |
}, | |
GotConn: func(connInfo httptrace.GotConnInfo) { | |
log.Printf("Got Conn: %+v\n", connInfo) | |
}, | |
DNSStart: func(dnsInfo httptrace.DNSStartInfo) { | |
log.Printf("DNS Start Info: %+v\n", dnsInfo) | |
}, | |
DNSDone: func(dnsInfo httptrace.DNSDoneInfo) { | |
log.Printf("DNS Done Info: %+v\n", dnsInfo) | |
}, | |
ConnectStart: func(network, addr string) { | |
log.Printf("Connect Start: %s, %s\n", network, addr) | |
}, | |
ConnectDone: func(network, addr string, err error) { | |
log.Printf("Connect Done: %s, %s, %v\n", network, addr, err) | |
}, | |
WroteRequest: func(wri httptrace.WroteRequestInfo) { | |
log.Printf("Wrote Request Info: %+v\n", wri) | |
}, | |
} | |
// Bind to the request context this new context for tracing. | |
req = req.WithContext(httptrace.WithClientTrace(req.Context(), &trace)) | |
// Make the request call and get the tracing information. | |
if _, err := http.DefaultTransport.RoundTrip(req); err != nil { | |
log.Fatal(err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment