package
0.0.0-20181115115248-6e6dd03ff0b1
Repository: https://github.com/solution/go-httpwares.git
Documentation: pkg.go.dev

# README

http_metrics

import "github.com/improbable-eng/go-httpwares/metrics"

Overview

http_metrics provides client and server side reporting of HTTP stats.

The middleware (server-side) or tripperware (client-side) must be given a reporter to record the stats for each request.

Prometheus-based reporter implementations for client and server metrics are included. The user may choose what level of detail is included using options to these reporters.

Imported Packages

Index

Examples

Package files

doc.go middleware.go reporter.go tripperware.go wrapped_body.go wrapped_writer.go

func Middleware

func Middleware(reporter Reporter) httpwares.Middleware

Middleware returns a http.Handler middleware that exports request metrics. If the tags middleware is used, this should be placed after tags to pick up metadata. This middleware assumes HTTP/1.x-style requests/response behaviour. It will not work with servers that use hijacking, pushing, or other similar features.

Example:

Click to expand code.
r := chi.NewRouter()
r.Use(http_ctxtags.Middleware("default"))
r.Use(http_metrics.Middleware(http_prometheus.ServerMetrics(http_prometheus.WithLatency())))
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(200)
})
http.ListenAndServe(":8888", r)

func Tripperware

func Tripperware(reporter Reporter) httpwares.Tripperware

Tripperware returns a new client-side ware that exports request metrics. If the tags tripperware is used, this should be placed after tags to pick up metadata.

Example:

Click to expand code.
c := httpwares.WrapClient(
    http.DefaultClient,
    http_ctxtags.Tripperware(),
    http_metrics.Tripperware(http_prometheus.ClientMetrics(http_prometheus.WithName("testclient"))),
)
c.Get("example.org/foo")

type Reporter

type Reporter interface {
    // Start tracking a new request.
    Track(req *http.Request) Tracker
}

Called when a new request is to be tracked.

type Tracker

type Tracker interface {
    // The exchange has started. This is called immediately after Reporter.Track.
    // On the client, this is called before any data is sent.
    // On the server, this is called after headers have been parsed.
    RequestStarted()
    // The request body has been read to EOF or closed, whichever comes first.
    // On the client, this is called when the transport completes sending the request.
    // On the server, this is called when the handler completes reading the request, and may be omitted.
    RequestRead(duration time.Duration, size int)
    // The handling of the response has started.
    // On the client, this is called after the response headers have been parsed.
    // On the server, this is called before any data is written.
    ResponseStarted(duration time.Duration, status int, header http.Header)
    // The response has completed.
    // On the client, this is called when the body is read to EOF or closed, whichever comes first, and may be omitted.
    // On the server, this is called when the handler returns and has therefore completed writing the response.
    ResponseDone(duration time.Duration, status int, size int)
}

Receives events about a tracked request.


Generated by godoc2ghmd

# Packages

No description provided by the author

# Functions

Middleware returns a http.Handler middleware that exports request metrics.
Tripperware returns a new client-side ware that exports request metrics.

# Interfaces

Called when a new request is to be tracked.
Receives events about a tracked request.