httpstat

package module
v0.0.0-...-ddc4f16 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2018 License: MIT Imports: 12 Imported by: 0

README

httpstat godoc goreport

httpstat is a net/http handler for Go, which reports various metrics about a given http server. It natively supports exporting stats to expvar (this is actually how it tracks all of it's stats). httpstat also has support for taking historical snapshots of the data. An example usecase and feature of this is the statgraph subpackage, as documented here.

An example usecase (also see _examples/) is shown below:

package main

import (
	_ "expvar"
	"fmt"
	"net/http"

	"github.com/lrstanley/httpstat"
)

func main() {
	stats := httpstat.New("", nil)
	defer stats.Close()

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintln(w, "Hello Gopher")
    })

	http.ListenAndServe(":8080", stats.Record(http.DefaultServeMux))
}

Example output, using the expvar's default endpoint, /debug/vars:

{
    "cmdline": [
        "/tmp/go-build807700005/command-line-arguments/_obj/exe/main"
    ],
    "httpstat_invoked": "2018-01-11T07:58:40-05:00",
    "httpstat_invoked_seconds": 1170,
    "httpstat_invoked_unix": 1515675520,
    "httpstat_pid": 17135,
    "httpstat_request_bytes_total": 8894,
    "httpstat_request_errors_total": 0,
    "httpstat_request_total": 23,
    "httpstat_request_total_seconds": 0.004517135,
    "httpstat_response_bytes_total": 50748,
    "httpstat_status_total": {
        "200": 22,
        "404": 1
    },
    "memstats": "..."
    }
}

You can also mount the ServeHTTP endpoint (here) to a custom location, which will only return the expvar variables that were created by this application.

Statgraph

There is an optional subpackage you can use, which will allow you to mount a net/http handler, which can return svg/png rendered graphs of the historical snapshots. Below is an example of going to the mounted http handlers /:

Notes

  • Make sure you register the handler/middleware as far up the stack that you want to track metrics on. Also make sure that your handlers do not return early, and continue writing to the ResponseWriter after they have returned, as httpstat cannot monitor those writes.
  • Using this library will introduce a ~550ns overhead to the total request processing time, however it shouldn't introduce any measurable delay during the server->client response times, since tracking measurement compilation occurs after the child middleware/handler has finished being invoked.
  • Using History will add a minor amount of additional overhead during snapshot pauses. This may be reduced in future iterations.
  • Each invocation of a new HTTPStats struct must be under it's own namespace, as expvar only allows variables with a given name to be registered once. See httpstat.New for details.
  • The request size ("bytes in") can only be roughly calculated, as net/http strips some of the data of the request as it is being processed.

Why?

There are a few packages similar to this one (thoas/stats and felixge/httpsnoop to name a few), however I wanted one with builtin support for historical snapshots, as well as one which was built with expvar in mind. With support for expvar, this can allow other packages to introspect the data that this package exports, in a standardized way. Not all of these packages support concurrent writes to the ResponseWriter, which httpstat does.

License

LICENSE: The MIT License (MIT)
Copyright (c) 2017 Liam Stanley <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HTTPStats

type HTTPStats struct {
	PID         *expvar.Int
	Invoked     *expvar.String
	InvokedUnix *expvar.Int
	Uptime      *UptimeVar

	TimeTotal          *expvar.Float
	RequestErrorsTotal *expvar.Int
	RequestsTotal      *expvar.Int
	BytesInTotal       *expvar.Int
	BytesOutTotal      *expvar.Int
	StatusTotal        *expvar.Map

	History History
	// contains filtered or unexported fields
}

HTTPStats holds the state of the current recorder middleware.

func New

func New(namespace string, histOpts *HistoryOptions) *HTTPStats

New creates a new middleware http stat recorder. Note that because httpstat uses expvar to track data, expvar variables can only be created ONCE, with the same namespace. If you know you are only using one invokation of httpstat, leave namespace blank. Otherwise use it to identify which thing it is recording (e.g. auth, frontend, etc).

histOpts are options which you can use to enable history snapshots (see History.Elems(), and HistoryElem) which can be used to track historical records on how the request count and similar is increasing over time. History is disabled by default as it has an almost negligible performance hit. Make sure if History is being used, that HTTPStats.Close() is called when closing the server.

func (*HTTPStats) Close

func (s *HTTPStats) Close()

Close is required if using History, it will close the goroutine which manages taking snapshots. Should only be called once.

func (*HTTPStats) MarshalJSON

func (s *HTTPStats) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface, allowing all httpstats expvars that match the configured namespace of the current HTTPStats, are returned in JSON form.

func (*HTTPStats) Record

func (s *HTTPStats) Record(next http.Handler) http.Handler

Record is the handler wrapper method used to invoke tracking of all child handlers. Note that this should be invoked early in the handler chain, otherwise the handlers invoked before this, will not be recorded/tracked. Also note that if one of the children handlers writes to the ResponseWriter after the handler is returned (e.g. from a goroutine), the time and bytes written will not be updated after the handler returns.

func (*HTTPStats) ServeHTTP

func (s *HTTPStats) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is a way of invoking/showing the JSON version of httpstats without mounting an expvar endpoint (e.g. if you don't want all of the other expvar stats). If you mount /debug/vars via expvar, this isn't needed.

type History

type History struct {
	Opts HistoryOptions
	// contains filtered or unexported fields
}

History holds the previous historical elements, and options for how long and how much to store.

func (*History) Elems

func (h *History) Elems() []HistoryElem

Elems returns the list previous history iterations.

type HistoryElem

type HistoryElem struct {
	Born          time.Time
	TimeTotal     float64
	TimeDiff      float64
	RequestErrors int64
	RequestsTotal int64
	RequestsDiff  int64
	RPS           int64
}

HistoryElem is a snapshot of the http stats from a previous point of time.

type HistoryOptions

type HistoryOptions struct {
	// Enabled enables history collection. If history isn't needed, you may
	// want to disable it to improve performance.
	Enabled bool
	// MaxResolution is how far back you would like to store in history. For
	// example, a MaxResolution of 5 minutes means any datapoints older than
	// 5 minutes, will be truncated from the snapshot list. Defaults to 5
	// minutes.
	MaxResolution time.Duration
	// Resolution is the time between each snapshot. If your resolution is
	// 10 seconds and MaxResolution is 5 minutes, that would be (5*60)/10 (or
	// 30) datapoints. Defaults to 5 seconds.
	Resolution time.Duration
}

HistoryOptions define a set of options for how many history snapshots to store, and when they should expire.

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	http.Flusher

	// Status returns the status code of the response or 0 if the response
	// has not been written.
	Status() int
	// Written returns whether or not the ResponseWriter has been written to.
	Written() bool
	// BytesWritten returns the amount of bytes written to the response body.
	BytesWritten() int
}

ResponseWriter is a custom implementation of the http.ResponseWriter interface.

func NewResponseRecorder

func NewResponseRecorder(w http.ResponseWriter) ResponseWriter

NewResponseRecorder returns a new instance of a responseRecorder.

type UptimeVar

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

UptimeVar is a type which implements the expvar.Var interface, and shows the (or time since invokation) of the struct when calling String().

func (*UptimeVar) String

func (u *UptimeVar) String() string

String returns the amount of seconds that UptimeVar has recorded, in integer form.

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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