httpstat

package module
v0.0.0-...-dafa51e Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2024 License: MIT Imports: 8 Imported by: 0

README

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.

Install

Use go get,

$ go get github.com/tcnksm/go-httpstat

Author

Taichi Nakashima

Documentation

Overview

Package httpstat traces HTTP latency infomation (DNSLookup, TCP Connection and so on) on any Golang HTTP request. It uses the `httptrace` package, just create a `go-httpstat` powered `context.Context` and give it your `http.Request` to gain access to the latency information of the request.

Example
req, err := http.NewRequest("GET", "http://deeeet.com", nil)
if err != nil {
	log.Fatal(err)
}

// 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 {
	log.Fatal(err)
}

if _, err := io.Copy(ioutil.Discard, res.Body); err != nil {
	log.Fatal(err)
}
res.Body.Close()
result.End(time.Now())

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

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 hook.

Types

type Result

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

	// The following is the timeline of a request
	NameLookup    time.Duration
	Connect       time.Duration
	Pretransfer   time.Duration
	StartTransfer time.Duration
	// contains filtered or unexported fields
}

Result stores httpstat information.

func (*Result) ContentTransfer

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

ContentTransfer returns the duration of content transfer time. If the request is finished it returns the content transfer time, otherwise it returns the duration from the first response byte until when the function was called.

func (*Result) End

func (r *Result) End()

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

func (Result) Format

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

Format formats stats result.

func (*Result) Total

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

Total returns the duration of the total http request. If the request is finished it returns the total time, otherwise it returns the duration from the DNS lookup start time until when the function was called.

func (*Result) Until

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

Until returns the duration of the http request until time t. Measured from the DNS lookup start time to the given time.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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