trace

package module
v0.0.0-...-4f038e6 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2017 License: MIT Imports: 6 Imported by: 0

README

http-trace

Helper for golang's httptrace.ClientTrace

How to use

import (
    trace "github.com/olegfedoseev/http-trace"
)

// ...

    req, _ := http.NewRequest(...)
    httpClient := &http.Client{}

    // insted of 
    // resp, err := httpClient.Do(req)
    // you need to do
    clientTrace, resp, err := trace.DoRequestWithTrace(httpClient, req)
    if err != nil {
        // handle err...
    }
    defer func() {
        resp.Body.Close()
        fmt.Printf("[TRACE] %s\n", clientTrace.GetResult())
    }()
    
// ...

And you'll get something like than:

[TRACE] http://example.com [DNS: 0s TCP: 7.778836ms Server: 85.851965ms Transfer: 136.281µs Total: 93.767082ms]

Or you can format it the way you like, clientTrace.GetResult() gives you access to all data.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Result

type Result struct {
	URL              string
	DNSLookup        time.Duration
	TCPConnection    time.Duration
	ServerProcessing time.Duration
	ContentTransfer  time.Duration
	Total            time.Duration
}

Result will hold results of request tracing

func (Result) String

func (r Result) String() string

type Trace

type Trace struct {
	URL                      string
	Created                  time.Time
	RequestStart             time.Time
	DNSStartTime             time.Time
	DNSDoneTime              time.Time
	GotConnTime              time.Time
	GotFirstResponseByteTime time.Time
	BodyReadTime             time.Time
}

Trace will hold time of trace events of request lifecycle

func DoRequestWithTrace

func DoRequestWithTrace(client *http.Client, req *http.Request) (*Trace, *http.Response, error)

DoRequestWithTrace is helper to make a request with trace

func NewTrace

func NewTrace(url string) (*Trace, context.Context)

NewTrace will create new Trace and context with httptrace.ClientTrace

func (*Trace) ConnectStart

func (t *Trace) ConnectStart(_, _ string)

ConnectStart will be used in httptrace.ClientTrace ConnectStart

func (*Trace) DNSDone

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

DNSDone will be used in httptrace.ClientTrace DNSDone

func (*Trace) DNSStart

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

DNSStart will be used in httptrace.ClientTrace DNSStart

func (*Trace) GetResult

func (t *Trace) GetResult() Result

GetResult will return Result struct with easy to use fields with measurements

func (*Trace) GotConn

func (t *Trace) GotConn(info httptrace.GotConnInfo)

GotConn will be used in httptrace.ClientTrace GotConn

func (*Trace) GotFirstResponseByte

func (t *Trace) GotFirstResponseByte()

GotFirstResponseByte will be used in httptrace.ClientTrace GotFirstResponseByte

Jump to

Keyboard shortcuts

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