github.com/yandex/pandora@v0.5.32/components/guns/http/trace.go (about)

     1  package phttp
     2  
     3  import (
     4  	"net/http/httptrace"
     5  	"time"
     6  )
     7  
     8  type TraceTimings struct {
     9  	GotConnTime          time.Time
    10  	GetConnTime          time.Time
    11  	DNSStartTime         time.Time
    12  	DNSDoneTime          time.Time
    13  	ConnectDoneTime      time.Time
    14  	ConnectStartTime     time.Time
    15  	WroteRequestTime     time.Time
    16  	GotFirstResponseByte time.Time
    17  }
    18  
    19  func (t *TraceTimings) GetReceiveTime() time.Duration {
    20  	return time.Since(t.GotFirstResponseByte)
    21  }
    22  
    23  func (t *TraceTimings) GetConnectTime() time.Duration {
    24  	return t.GotConnTime.Sub(t.GetConnTime)
    25  }
    26  
    27  func (t *TraceTimings) GetSendTime() time.Duration {
    28  	return t.WroteRequestTime.Sub(t.GotConnTime)
    29  }
    30  
    31  func (t *TraceTimings) GetLatency() time.Duration {
    32  	return t.GotFirstResponseByte.Sub(t.WroteRequestTime)
    33  }
    34  
    35  func CreateHTTPTrace() (*httptrace.ClientTrace, *TraceTimings) {
    36  	timings := &TraceTimings{}
    37  	tracer := &httptrace.ClientTrace{
    38  		GetConn: func(_ string) {
    39  			timings.GetConnTime = time.Now()
    40  		},
    41  		GotConn: func(_ httptrace.GotConnInfo) {
    42  			timings.GotConnTime = time.Now()
    43  		},
    44  		DNSStart: func(_ httptrace.DNSStartInfo) {
    45  			timings.DNSStartTime = time.Now()
    46  		},
    47  		DNSDone: func(info httptrace.DNSDoneInfo) {
    48  			timings.DNSDoneTime = time.Now()
    49  		},
    50  		ConnectStart: func(network, addr string) {
    51  			timings.ConnectStartTime = time.Now()
    52  		},
    53  		ConnectDone: func(network, addr string, err error) {
    54  			timings.ConnectDoneTime = time.Now()
    55  		},
    56  		WroteRequest: func(wr httptrace.WroteRequestInfo) {
    57  			timings.WroteRequestTime = time.Now()
    58  		},
    59  		GotFirstResponseByte: func() {
    60  			timings.GotFirstResponseByte = time.Now()
    61  		},
    62  	}
    63  
    64  	return tracer, timings
    65  }