clienttiming

package module
Version: v0.0.0-...-693d229 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2018 License: MIT Imports: 5 Imported by: 0

README

client-timing

Build Status codecov GoDoc Go Report Card

An HTTP client for go-server-timing middleware.

Features:

  • An HTTP Client or RoundTripper, fully compatible with Go's standard library.
  • Automatically time HTTP requests sent from an HTTP handler.
  • Collects all timing headers from upstream servers.
  • Customize timing headers according to the request, response and error of the HTTP round trip.

Install

go get -u github.com/posener/client-timing

Usage

  1. Add a *clienttiming.Timer to your server handler, or create it in the handler function itself.

  2. Wrap the http.Handler with servertiming.Middleware.

  3. In the handler function, having timer of type *clienttiming.Timer and req is the *http.Request:

    a. Create an *http.Client using timer.Client(req.Context())

    b. Or create an http.RoundTripper using timer.Transport(req.Context())

  4. Use option a or b directly or inject it to a library that accepts them, in your outgoing HTTP request from the handler.

  5. That is it! the timing header will appear in the response from the handler.

Example

Suppose we have an HTTP handler:

type handler struct {
	timer *clienttiming.Timer
}

Our usage of that handler will be:

func main() {
	h := &handler{
		timer: clienttiming.New(clienttiming.WithName("my-server")),
	}
	log.Fatal(http.ListenAndServe(":8080", servertiming.Middleware(h)))
}
Example for Client function:
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// Create an http client using the request context
	c := h.timer.Client(r.Context())

	// Perform HTTP requests, as many as you like
	resp, err := c.Get("https://golang.org/")

	...
}
Example for Transport function:
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// Instrument an http client with a timing transport
	c := &http.Client{
		Transport: h.timer.Transport(r.Context()),
	}

	// Perform HTTP requests, as many as you like
	resp, err := c.Get("https://golang.org/")

	...
}
Run the example

go run ./example/main.go

Documentation

Overview

Package clienttiming gives an HTTP client for go-server-timing middleware.

It provides:

An HTTP `Client` or `RoundTripper`, fully compatible with Go's standard library. Automatically time HTTP requests sent from an HTTP handler. Collects all timing headers from upstream servers. Customize timing headers according to the request, response and error of the HTTP round trip.

Index

Constants

View Source
const KeySource = "source"

KeySource is the key in the metric in which the source name will be stored

Variables

This section is empty.

Functions

func DefaultDesc

func DefaultDesc(req *http.Request) string

DefaultDesc set the metric description as the request method and path

func DefaultMetric

func DefaultMetric(req *http.Request) string

DefaultMetric set the metric name as the request host

func DefaultUpdate

func DefaultUpdate(m *servertiming.Metric, resp *http.Response, err error)

DefaultUpdate sets status code in metric if there was no error, otherwise it sets the error text.

func InsertMetrics

func InsertMetrics(h *servertiming.Header, headers http.Header)

InsertMetrics inserts to servertiming header metrics from an HTTP header of another request They are prepended since they happened before the metrics of the header itself

Types

type Option

type Option func(*Timer)

Option is timer-timing mockTransport option function

func WithDesc

func WithDesc(desc func(*http.Request) string) Option

WithDesc sets the desc function which defines the metric description from the request

func WithMetric

func WithMetric(metric func(*http.Request) string) Option

WithMetric sets the metric function which defines the metric name from the request

func WithName

func WithName(name string) Option

WithName updates the source key in the metric to this name It is used to give a name for the timer

func WithTransport

func WithTransport(inner http.RoundTripper) Option

WithTransport sets the inner Client for the request

func WithUpdate

func WithUpdate(update func(*servertiming.Metric, *http.Response, error)) Option

WithUpdate sets the update function which updates the metric according to response and error received from completing the round trip

type Timer

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

Timer constructs instrumented clients for server-timing

func New

func New(opts ...Option) *Timer

New returns a instrumented constructor for http timer and mockTransport.

func (*Timer) Client

func (t *Timer) Client(ctx context.Context, opts ...Option) *http.Client

Client returns a server-timing instrumented http timer for the current context

func (*Timer) Transport

func (t *Timer) Transport(ctx context.Context, opts ...Option) http.RoundTripper

Transport returns a server-timing instrumented round tripper for the current context

Source Files

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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