github.com/network-quality/goresponsiveness@v0.0.0-20240129151524-343954285090/traceable/traceable.go (about) 1 /* 2 * This file is part of Go Responsiveness. 3 * 4 * Go Responsiveness is free software: you can redistribute it and/or modify it under 5 * the terms of the GNU General Public License as published by the Free Software Foundation, 6 * either version 2 of the License, or (at your option) any later version. 7 * Go Responsiveness is distributed in the hope that it will be useful, but WITHOUT ANY 8 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 9 * PARTICULAR PURPOSE. See the GNU General Public License for more details. 10 * 11 * You should have received a copy of the GNU General Public License along 12 * with Go Responsiveness. If not, see <https://www.gnu.org/licenses/>. 13 */ 14 15 package traceable 16 17 import ( 18 "crypto/tls" 19 "net/http/httptrace" 20 "time" 21 22 "github.com/network-quality/goresponsiveness/debug" 23 ) 24 25 type Traceable interface { 26 SetDnsStartTimeInfo(time.Time, httptrace.DNSStartInfo) 27 SetDnsDoneTimeInfo(time.Time, httptrace.DNSDoneInfo) 28 SetConnectStartTime(time.Time) 29 SetConnectDoneTimeError(time.Time, error) 30 SetGetConnTime(time.Time) 31 SetGotConnTimeInfo(time.Time, httptrace.GotConnInfo) 32 SetTLSHandshakeStartTime(time.Time) 33 SetTLSHandshakeDoneTimeState(time.Time, tls.ConnectionState) 34 SetHttpWroteRequestTimeInfo(time.Time, httptrace.WroteRequestInfo) 35 SetHttpResponseReadyTime(time.Time) 36 } 37 38 func GenerateHttpTimingTracer( 39 traceable Traceable, 40 debug debug.DebugLevel, 41 ) *httptrace.ClientTrace { 42 tracer := httptrace.ClientTrace{ 43 DNSStart: func(dnsStartInfo httptrace.DNSStartInfo) { 44 traceable.SetDnsStartTimeInfo(time.Now(), dnsStartInfo) 45 }, 46 DNSDone: func(dnsDoneInfo httptrace.DNSDoneInfo) { 47 traceable.SetDnsDoneTimeInfo(time.Now(), dnsDoneInfo) 48 }, 49 ConnectStart: func(network, address string) { 50 traceable.SetConnectStartTime(time.Now()) 51 }, 52 ConnectDone: func(network, address string, err error) { 53 traceable.SetConnectDoneTimeError(time.Now(), err) 54 }, 55 GetConn: func(hostPort string) { 56 traceable.SetGetConnTime(time.Now()) 57 }, 58 GotConn: func(connInfo httptrace.GotConnInfo) { 59 traceable.SetGotConnTimeInfo(time.Now(), connInfo) 60 }, 61 TLSHandshakeStart: func() { 62 traceable.SetTLSHandshakeStartTime(time.Now()) 63 }, 64 TLSHandshakeDone: func(tlsConnState tls.ConnectionState, err error) { 65 traceable.SetTLSHandshakeDoneTimeState(time.Now(), tlsConnState) 66 }, 67 WroteRequest: func(wroteRequest httptrace.WroteRequestInfo) { 68 traceable.SetHttpWroteRequestTimeInfo(time.Now(), wroteRequest) 69 }, 70 GotFirstResponseByte: func() { 71 traceable.SetHttpResponseReadyTime(time.Now()) 72 }, 73 } 74 return &tracer 75 }