httpstat

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2020 License: MIT Imports: 15 Imported by: 1

README

httpstat

WIP Go HTTP tracing pkg. Not yet in production for Apex Ping, but likely will be at some point.

Example

res, _ := httpstat.Request("GET", "http://apex.sh", nil, nil)
enc := json.NewEncoder(os.Stderr)
enc.SetIndent("", "  ")
enc.Encode(res.Stats())
{
  "status": 200,
  "redirects": 1,
  "tls": true,
  "header": {
    "Age": [
      "2687"
    ],
    "Content-Length": [
      "9095"
    ],
    "Content-Type": [
      "text/html"
    ],
    "Date": [
      "Wed, 28 Jun 2017 03:13:39 GMT"
    ],
    "Etag": [
      "\"3889de5294fd56ac2596668b3943b58e\""
    ],
    "Last-Modified": [
      "Tue, 27 Jun 2017 18:32:26 GMT"
    ],
    "Server": [
      "AmazonS3"
    ],
    "Vary": [
      "Accept-Encoding"
    ],
    "Via": [
      "1.1 6a1e4dd9fa29c61c4b71a53d6bf94267.cloudfront.net (CloudFront)"
    ],
    "X-Amz-Cf-Id": [
      "zFqohdPbqpqeXvMdB2mr8m9JiUIBCw_tMfDUY_RfaOzvdvShxEub-Q=="
    ],
    "X-Cache": [
      "Hit from cloudfront"
    ]
  },
  "header_size": 396,
  "body_size": 9095,
  "time_dns": 1244723,
  "time_connect": 19610427,
  "time_tls": 101961103,
  "time_wait": 21167169,
  "time_response": 21330931,
  "time_download": 163762,
  "time_total": 145177108,
  "time_total_with_redirects": 193749062,
  "time_redirects": 48571954,
  "traces": [
    {
      "tls": false,
      "time_dns": 1789711,
      "time_connect": 21597753,
      "time_tls": 0,
      "time_wait": 24391045,
      "time_response": 169912459,
      "time_download": 145521414,
      "time_total": 193749608
    },
    {
      "tls": true,
      "time_dns": 1244723,
      "time_connect": 19610427,
      "time_tls": 101961103,
      "time_wait": 21167169,
      "time_response": 21338000,
      "time_download": 170831,
      "time_total": 145184177
    }
  ]
}

GoDoc

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMaxRedirectsExceeded = errors.New("max redirects exceeded")
	ErrTimeoutExceeded      = errors.New("timeout exceeded")
)

Errors.

View Source
var DefaultClient = &http.Client{
	CheckRedirect: checkRedirect,
	Timeout:       10 * time.Second,
	Transport: &http.Transport{
		DisableCompression: true,
		Proxy:              http.ProxyFromEnvironment,
		DialContext: (&net.Dialer{
			Timeout:   5 * time.Second,
			KeepAlive: 0,
		}).DialContext,
		DisableKeepAlives:   true,
		MaxIdleConns:        10,
		TLSHandshakeTimeout: 5 * time.Second,
	},
}

DefaultClient used for requests.

View Source
var DefaultMaxRedirects = 5

DefaultMaxRedirects is the max number of redirects.

Functions

func WithTraces

func WithTraces(ctx context.Context, traces *[]Trace) context.Context

WithTraces traces request timings.

Types

type Response

type Response interface {
	Status() int
	Redirects() int
	TLS() bool
	Header() http.Header
	HeaderSize() int
	BodySize() int
	TimeDNS() time.Duration
	TimeConnect() time.Duration
	TimeTLS() time.Duration
	TimeWait() time.Duration
	TimeResponse(time.Time) time.Duration
	TimeDownload(time.Time) time.Duration
	TimeTotal(time.Time) time.Duration
	TimeTotalWithRedirects(time.Time) time.Duration
	TimeRedirects() time.Duration
	Traces() []Trace
	Stats() *Stats
}

Response interface.

func Request

func Request(method, uri string, header http.Header, body io.Reader) (Response, error)

Request performs a traced request.

func RequestWithClient

func RequestWithClient(client *http.Client, method, uri string, header http.Header, body io.Reader) (Response, error)

RequestWithClient performs a traced request.

type Stats

type Stats struct {
	Status                 int           `json:"status,omitempty"`
	Redirects              int           `json:"redirects,omitempty"`
	TLS                    bool          `json:"tls"`
	Header                 http.Header   `json:"header,omitempty"`
	HeaderSize             int           `json:"header_size,omitempty"`
	BodySize               int           `json:"body_size,omitempty"`
	TimeDNS                time.Duration `json:"time_dns"`
	TimeConnect            time.Duration `json:"time_connect"`
	TimeTLS                time.Duration `json:"time_tls"`
	TimeWait               time.Duration `json:"time_wait"`
	TimeResponse           time.Duration `json:"time_response"`
	TimeDownload           time.Duration `json:"time_download"`
	TimeTotal              time.Duration `json:"time_total"`
	TimeTotalWithRedirects time.Duration `json:"time_total_with_redirects,omitempty"`
	TimeRedirects          time.Duration `json:"time_redirects,omitempty"`
	Traces                 []*Stats      `json:"traces,omitempty"`
}

Stats is an opaque struct which can be useful for JSON marshaling.

type Trace

type Trace interface {
	Address() string
	TLS() bool
	Start() time.Time
	TimeDNS() time.Duration
	TimeConnect() time.Duration
	TimeTLS() time.Duration
	TimeWait() time.Duration
	TimeResponse(time.Time) time.Duration
	TimeDownload(time.Time) time.Duration
	TimeTotal(time.Time) time.Duration
	Stats() *Stats
}

Trace results.

Jump to

Keyboard shortcuts

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