package module
v0.3.0 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2017 License: MIT Imports: 8 Imported by: 0


go-httpstat Go Documentation Build Status MIT License

go-httpstat is a golang package to trace golang HTTP request latency (DNSLookup, TCP Connection and so on). Because it uses httptrace internally, just creating go-httpstat powered context and giving it your http.Request kicks tracing (no big code modification is required). The original idea came from httpstat command ( and Dave Cheney's golang implementation) 👏. This package now traces same latency infomation as them.

See usage and example on GoDoc.

NOTE: Since httptrace was introduced after go1.7, this package may not work with old HTTP client. Especially, if you don't use net.DialContext it can not trace DNS and connection.


Use go get,

$ go get


Taichi Nakashima



Package httpstat traces HTTP latency infomation (DNSLookup, TCP Connection and so on) on any golang HTTP request. It uses `httptrace` package. Just create `go-httpstat` powered `context.Context` and give it your `http.Request` (no big code modification is required).

req, err := http.NewRequest("GET", "", nil)
if err != nil {

// Create go-httpstat powered context and pass it to http.Request
var result httpstat.Result
ctx := httpstat.WithHTTPStat(req.Context(), &result)
req = req.WithContext(ctx)

client := http.DefaultClient
res, err := client.Do(req)
if err != nil {

if _, err := io.Copy(ioutil.Discard, res.Body); err != nil {

// Show results
log.Printf("%+v", result)




This section is empty.


This section is empty.


func WithHTTPStat

func WithHTTPStat(ctx context.Context, r *Result) context.Context

WithHTTPStat is a wrapper of httptrace.WithClientTrace. It records the time of each httptrace hooks.


type Result

type Result struct {
	// The following are duration for each phase
	DNSLookup        time.Duration
	TCPConnection    time.Duration
	TLSHandshake     time.Duration
	ServerProcessing time.Duration

	// The followings are timeline of request
	NameLookup    time.Duration
	Connect       time.Duration
	Pretransfer   time.Duration
	StartTransfer time.Duration
	// contains filtered or unexported fields

Result stores httpstat info.

func (*Result) ContentTransfer

func (r *Result) ContentTransfer(t time.Time) time.Duration

ContentTransfer returns the duration of content transfer time. It is from first response byte to the given time. The time must be time after read body (go-httpstat can not detect that time).

func (*Result) End added in v0.2.0

func (r *Result) End(t time.Time)

End sets the time when reading response is done. This must be called after reading response body.

func (Result) Format added in v0.2.0

func (r Result) Format(s fmt.State, verb rune)

Format formats stats result.

func (*Result) Total

func (r *Result) Total(t time.Time) time.Duration

Total returns the duration of total http request. It is from dns lookup start time to the given time. The time must be time after read body (go-httpstat can not detect that time).


Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL