httptracer

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2020 License: MIT Imports: 9 Imported by: 0

README

Another yet simple HTTP tracer

Install

go get https://github.com/vitpelekhaty/httptracer

Usage

Just wrap your http client with a Trace function from package:

...
client := &http.Client{Timeout: time.Second * 5}
client = httptracer.Trace(client)
...    

Specify callback function as an option, if you want to use your own entry handler:

...
callback := func(entry *Entry) {
    // Do something
}

client = httptracer.Trace(client, WithCallback(callback))

Specify the appropriate option to keep a request/response body in the dump:

...
client = httptracer.Trace(client, WithBodies(true))
...

Use option WithWriter(...) to write a dump, in a file, for example:

...
f, err := os.Create(path)
...
client = httptracer.Trace(client, WithWriter(f))

Example:

empty := true

f, err := os.Create(path)

if err != nil {
	return err
}

defer func() {
	f.WriteString("]")
	f.Close()
}()

f.WriteString("[")

callback := func(entry *Entry) {
	if !empty {
		f.WriteString(",")
    }

	empty = false
}

client = Trace(client, WithWriter(f), WithCallback(callback), WithBodies(true))

form := url.Values{}
form.Set("username", "John Doe")

req, err := http.NewRequest("POST", uri, strings.NewReader(form.Encode()))

req.Header.Set("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")

if err != nil {
	return err
}

_, err = client.Do(req)

if err != nil {
	return err
}

return nil

Output:

[
  {
    "time":"2020-04-10T14:58:31.518891597+07:00",
    "request":[
      "POST /echo HTTP/1.1",
      "Host: 127.0.0.1:38251",
      "Content-Type: application/x-www-form-urlencoded;charset=utf-8",
      "",
      "username=John+Doe"
    ],
    "response":[
      "HTTP/1.1 200 OK",
      "Content-Length: 15",
      "Content-Type: text/plain; charset=utf-8",
      "Date: Fri, 10 Apr 2020 07:58:31 GMT",
      "",
      "Hello, John Doe"
    ],
    "metric":{
      "dns-lookup":0,
      "tcp-connection":579193,
      "tls-handshake":0,
      "server-processing":1411986,
      "content-transfer":262879,
      "name-lookup":0,
      "connect":579193,
      "pre-transfer":0,
      "start-transfer":1991179,
      "total":2254058
    },
    "error":null
  }
]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Trace

func Trace(client *http.Client, options ...Option) *http.Client

Trace wraps a client for tracing

Types

type CallbackFunc

type CallbackFunc func(entry *Entry)

CallbackFunc Tracer callback function. Function called when another entry was added

type Entry

type Entry struct {
	// Time of request
	Time time.Time `json:"time"`
	// Request dump
	Request []string `json:"request"`
	// Response dump
	Response []string `json:"response,omitempty"`
	// Metric request statistic
	Metric Metric `json:"metric"`
	// Error connection error
	Error error `json:"error,omitempty"`
}

Entry trace entry

type Metric

type Metric struct {
	DNSLookup        time.Duration `json:"dns-lookup"`
	TCPConnection    time.Duration `json:"tcp-connection"`
	TLSHandshake     time.Duration `json:"tls-handshake"`
	ServerProcessing time.Duration `json:"server-processing"`
	ContentTransfer  time.Duration `json:"content-transfer"`
	NameLookup       time.Duration `json:"name-lookup"`
	Connect          time.Duration `json:"connect"`
	PreTransfer      time.Duration `json:"pre-transfer"`
	StartTransfer    time.Duration `json:"start-transfer"`
	Total            time.Duration `json:"total"`
}

Metric request duration metrics (time in nanoseconds)

type Option

type Option func(t *Tracer)

Option tracer option

func WithBodies

func WithBodies(value bool) Option

WithBodies indicates the need to read a request/response body

func WithCallback

func WithCallback(callback CallbackFunc) Option

WithCallback sets a callback function

func WithWriter

func WithWriter(writer io.Writer) Option

WithWriter sets a writer

type Tracer

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

Tracer http tracer

func New

func New(transport http.RoundTripper, options ...Option) *Tracer

New constructor of Tracer

func (*Tracer) ConnectDone

func (t *Tracer) ConnectDone(network, addr string, err error)

func (*Tracer) ConnectStart

func (t *Tracer) ConnectStart(network, addr string)

func (*Tracer) DNSDone

func (t *Tracer) DNSDone(_ httptrace.DNSDoneInfo)

func (*Tracer) DNSStart

func (t *Tracer) DNSStart(_ httptrace.DNSStartInfo)

func (*Tracer) GotConn

func (t *Tracer) GotConn(_ httptrace.GotConnInfo)

func (*Tracer) GotFirstResponseByte

func (t *Tracer) GotFirstResponseByte()

func (*Tracer) HTTPMetric

func (t *Tracer) HTTPMetric() Metric

func (*Tracer) HTTPSMetric

func (t *Tracer) HTTPSMetric() Metric

func (*Tracer) RoundTrip

func (t *Tracer) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implementation of http.RoundTripper interface

func (*Tracer) TLSHandshakeDone

func (t *Tracer) TLSHandshakeDone(_ tls.ConnectionState, err error)

func (*Tracer) TLSHandshakeStart

func (t *Tracer) TLSHandshakeStart()

Jump to

Keyboard shortcuts

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