tracer

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

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

Go to latest
Published: Aug 10, 2018 License: MIT Imports: 5 Imported by: 0

README

tracer

Annotate a golang http request to trace time taken at various steps.

Heavily inspired by https://github.com/davecheney/httpstat :)

Note: Just as any other instrumentation tool, don't keep this running in production forever without understanding the cost. If you are doing a lot of requests, this might have some (tiny) impact.

Usage:

import "github.com/urjitbhatia/tracer"

req, _ := http.NewRequest(http.MethodGet, "http://golang.org", body)
req, tr = tracer.AsTraceableReq(req)
// ...
resp, err := client.Do(req)
if err != nil {
    log.Fatalf("failed to read response: %v", err)
}
body := readResponseBody(req, resp)
resp.Body.Close()
// Important to call this to calculate total time taken, otherwise it will be negative
tr.Finish()

// ..
log.Printlf("Tracer stats: %v", tr)
2018/08/10 06:23:48 Tracer stats: {"total":1.917301,"dnsLookup":0.526224, "tcpDialed":0.170330, "connSetup":0.684807, "preTransfer":1.211031, "ttfb":1.917138, "serverProcessing":0.706107, "contentTransfer":0.000162}
If you see bugs, please feel free to post issues.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Tracer

type Tracer struct {
	// contains filtered or unexported fields
}

Tracer records timings for an http request

func AsTraceableReq

func AsTraceableReq(req *http.Request) (*http.Request, *Tracer)

AsTraceableReq wraps a given *http.Request with tracing timers and returns the wrapped request Use the returned Tracer value to query for various timing values that were recorded

func (*Tracer) ConnSetup

func (t *Tracer) ConnSetup() time.Duration

ConnSetup returns time taken to fully create a connection that is ready to perform an http request

func (*Tracer) ContentTransfer

func (t *Tracer) ContentTransfer() time.Duration

ContentTransfer returns time taken from first byte till body is fully read

func (*Tracer) DNSLookup

func (t *Tracer) DNSLookup() time.Duration

DNSLookup returns dns lookup time taken

func (*Tracer) Finish

func (t *Tracer) Finish()

Finish should be called as soon as a request body is read This method is important to calculate the total time taken

func (*Tracer) PreTransfer

func (t *Tracer) PreTransfer() time.Duration

PreTransfer returns total time taken to setup everything before the server has a connection ready to go.

func (*Tracer) ServerProcessing

func (t *Tracer) ServerProcessing() time.Duration

ServerProcessing returns time taken by the server to receive the request and start replying (TimeToFirstByte since connection setup)

func (*Tracer) String

func (t *Tracer) String() string

String representation

func (*Tracer) TCPDialed

func (t *Tracer) TCPDialed() time.Duration

TCPDialed returns time taken to dial the connection over tcp

func (*Tracer) TimeToFirstByte

func (t *Tracer) TimeToFirstByte() time.Duration

TimeToFirstByte returns time since start till FirstResponse Byte

func (*Tracer) Total

func (t *Tracer) Total() time.Duration

Total returns time take from very start to when tracer.Finish() was called

Jump to

Keyboard shortcuts

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