statc

package
v0.0.0-...-f5c3557 Latest Latest
Warning

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

Go to latest
Published: May 2, 2018 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package statc implements runtime process stats and status reporting.

It provides the basics (timers, gauges, counters), stats sinks, interfaces for fetching current stats, and so on.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanPath

func CleanPath(path string) string

CleanPath cleans extra dots and such from the path

func EscapePath

func EscapePath(path string) string

EscapePath escapes all reserved characters in the given path. For example, if you have a path like "url/img.jpg", the "." presents a special problem, so the path becomes "url/img_jpg".

This might do more escapes in the future, so use this instead of strings.Replace().

func JoinNoEscape

func JoinNoEscape(parts ...string) string

JoinNoEscape joins all parts together without escaping each part. This means that parts may contain ".".

func JoinPath

func JoinPath(parent string, parts ...string) string

JoinPath creates a stats path from multiple path elements. The parent element is left unescaped to allow for tacking on child paths.

func RegisterFormatter

func RegisterFormatter(name string, nf NewFormatter)

RegisterFormatter adds a Filter to the list of filters

Types

type Adder

type Adder interface {
	AddBool(name Name, val bool)
	AddFloat(name Name, val float64)
	AddInt(name Name, val int64)
	AddString(name Name, val string)
}

An Adder is used to add stats to a snapshot. If no name is given, (that is Name{}), then the stat's name is used. If any other name is given, that will be used instead.

type BoolGauge

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

A BoolGauge is a single bool value that can be set at will

func (*BoolGauge) Get

func (g *BoolGauge) Get() (v bool)

Get gets the current value of the gauge

func (*BoolGauge) Set

func (g *BoolGauge) Set(v bool)

Set sets the current value of this gauge

func (*BoolGauge) Snapshot

func (g *BoolGauge) Snapshot(a Adder)

Snapshot implements Snapshotter

type Config

type Config struct {
	// How often to take snapshots. Defaults to 10s.
	SnapshotInterval ctime.HumanDuration

	// Percent of HTTP requests to sample. Defaults to 10%. Range (0-100) for
	// 0% - 100%.
	HTTPSamplePercent int

	// If memory stats should be included in reported statistics. Defaults to
	// 60s; gathering these stats stops the world, so don't do this too often.
	// Set to <0 to disable.
	MemStatsInterval ctime.HumanDuration

	// StatusKey is the key used to secure the HTTPMuxer's /_status endpoint
	StatusKey string

	// Where stats should be put
	Outputs []OutputConfig
	// contains filtered or unexported fields
}

Config sets up stats. It can be read in from a config file to allow for simpler configing.

type Counter

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

A Counter is used to count things

func NewCounter

func NewCounter(reset bool) *Counter

NewCounter creates a new, unbound counter

func (*Counter) Add

func (c *Counter) Add(i int64)

Add adds the given value to the current counter

func (*Counter) Dec

func (c *Counter) Dec()

Dec is the equivalent of Add(-1)

func (*Counter) Get

func (c *Counter) Get() int64

Get gets the current value of the counter

func (*Counter) Inc

func (c *Counter) Inc()

Inc is the equivalent of Add(1)

func (*Counter) Snapshot

func (c *Counter) Snapshot(a Adder)

Snapshot implements Snapshotter

type FloatGauge

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

A FloatGauge is a single float value that can be set at will

func (*FloatGauge) Get

func (g *FloatGauge) Get() (v float64)

Get gets the current value of the gauge

func (*FloatGauge) Set

func (g *FloatGauge) Set(v float64)

Set sets the current value of this gauge

func (*FloatGauge) Snapshot

func (g *FloatGauge) Snapshot(a Adder)

Snapshot implements Snapshotter

type Formatter

type Formatter interface {
	// Format a snapshot
	FormatSnap(snap Snapshot) ([]byte, error)

	// Get the MimeType of data produced by this Formatter
	MimeType() string
}

A Formatter formats a snapshot

type Gauge

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

A Gauge is a single value that can be set at will

func (*Gauge) Get

func (g *Gauge) Get() int64

Get gets the current value of the gauge

func (*Gauge) Set

func (g *Gauge) Set(v int64)

Set sets the current value of this gauge

func (*Gauge) Snapshot

func (g *Gauge) Snapshot(a Adder)

Snapshot implements Snapshotter

type HTTPEndpoint

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

An HTTPEndpoint is used for timing requests to an endpoint.

func (*HTTPEndpoint) Start

func (he *HTTPEndpoint) Start(w http.ResponseWriter) *HTTPResp

Start timing the request

type HTTPMuxer

type HTTPMuxer struct {
	// Exposed so that you can set Options on it, use it as the server
	// handler, etc. Any routes added directly here will have no stats
	// recorded for them.
	R *httprouter.Router
	// contains filtered or unexported fields
}

HTTPMuxer is a wrapper around httprouter.Router. It intercepts all requests and reports useful information about them.

func (*HTTPMuxer) Endpoint

func (m *HTTPMuxer) Endpoint(method, path string) HTTPEndpoint

Endpoint is used to define an HTTP endpoint. If you're not using the provided handlers, you can use this to time requests.

func (*HTTPMuxer) Handle

func (m *HTTPMuxer) Handle(method, path string, handle httprouter.Handle)

Handle wraps the given Handle with stats reporting

func (*HTTPMuxer) Handler

func (m *HTTPMuxer) Handler(method, path string, handler http.Handler)

Handler wraps the given Handler with stats reporting

func (*HTTPMuxer) HandlerFunc

func (m *HTTPMuxer) HandlerFunc(method, path string, handler http.HandlerFunc)

HandlerFunc wraps the given HandlerFunc with stats reporting

type HTTPResp

type HTTPResp struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

An HTTPResp wraps an http.ResponseWriter, providing timing and tracking support.

func (*HTTPResp) Finish

func (hr *HTTPResp) Finish(paniced bool) time.Duration

Finish records stats about this request.

func (*HTTPResp) Free

func (hr *HTTPResp) Free()

Free releases this object and puts it back into the pool. This is optional and may only be used when you're sure no one is hanging onto the *HTTPResp.

func (*HTTPResp) Hijack

func (hr *HTTPResp) Hijack() (net.Conn, *bufio.ReadWriter, error)

Hijack implements http.Hijacker

func (*HTTPResp) Status

func (hr *HTTPResp) Status() int

Status returns the http status that was sent, or 0 if none was sent

func (*HTTPResp) WriteHeader

func (hr *HTTPResp) WriteHeader(status int)

WriteHeader implements http.ResponseWriter.WriteHeader

type JSONFormat

type JSONFormat struct {
	Args struct {
		// If the JSON should be pretty-printed
		Pretty bool
	}
}

JSONFormat formats snapshots as JSON

func (JSONFormat) FormatSnap

func (j JSONFormat) FormatSnap(snap Snapshot) ([]byte, error)

FormatSnap implements Formatter.FormatSnap

func (JSONFormat) MimeType

func (JSONFormat) MimeType() string

MimeType implements Formatter.MimeType

type LogstashFormat

type LogstashFormat struct{}

LogstashFormat formats snapshots as JSON, ready for shipping to logstash

func (LogstashFormat) FormatSnap

func (LogstashFormat) FormatSnap(snap Snapshot) ([]byte, error)

FormatSnap implements Formatter.FormatSnap

func (LogstashFormat) MimeType

func (LogstashFormat) MimeType() string

MimeType implements Formatter.MimeType

type Name

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

A Name is your ticket to naming a stat. Names are, by default, prefixed with the *S's prefix from whence they came. They may also be appended to to create new Names.

You should keep this around whenever you create a new Snapshotter since you'll need it to add the stat.

This is really just a wrapper for a string, but when used right, it forces proper path caching and discourages generating garbage.

func (Name) Append

func (n Name) Append(name string) Name

Append combines this name with the given name, without escaping anything

func (Name) Join

func (n Name) Join(parts ...string) Name

Join combines this name with the given parts, escapes each part, and returns the new Name

func (Name) Str

func (n Name) Str() string

Str returns the name as a string

type NewFormatter

type NewFormatter func(args eio.Args) (Formatter, error)

NewFormatter creates a new, configured Formatter.

type OutputConfig

type OutputConfig struct {
	Prod     string   // Which eio Producer to use
	ProdArgs eio.Args // Args to pass to the sink
	Fmt      string   // Name of formatter
	FmtArgs  eio.Args // Args to pass to the formatter
}

OutputConfig is used to configure outputs

type S

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

S is a stats aggregator.

func NewS

func NewS(cfg Config, log *clog.Log, exit *cog.GExit) (*S, error)

NewS creates a new stats aggregator

func (*S) AddLog

func (s *S) AddLog(name string, l *clog.Ctx)

AddLog adds log stats at the given path

func (*S) AddSnapshotter

func (s *S) AddSnapshotter(name Name, snapper Snapshotter)

AddSnapshotter binds a snapshotter to this S.

func (*S) Name

func (s *S) Name(name string) Name

Name returns a new name, appended to this S's prefix. The given name is not escaped.

func (*S) Names

func (s *S) Names(names ...string) Name

Names returns a new Name with the given names joined with this S's prefix. The names are escaped individually.

func (*S) NewBoolGauge

func (s *S) NewBoolGauge(name string) *BoolGauge

NewBoolGauge creates a bool gauge that's bound to this S

func (*S) NewCounter

func (s *S) NewCounter(name string, reset bool) *Counter

NewCounter creates a counter that's bound to this S

func (*S) NewFloatGauge

func (s *S) NewFloatGauge(name string) *FloatGauge

NewFloatGauge creates a float gauge that's bound to this S

func (*S) NewGauge

func (s *S) NewGauge(name string) *Gauge

NewGauge creates a gauge that's bound to this S

func (*S) NewHTTPMuxer

func (s *S) NewHTTPMuxer(name string) *HTTPMuxer

NewHTTPMuxer creates a new HTTP muxer that exposes status information for all endpoints.

At /_status, information from the last snapshot is accessible with the given key (ie. GET "/_status?key=<key>").

func (*S) NewStringGauge

func (s *S) NewStringGauge(name string) *StringGauge

NewStringGauge creates a string gauge that's bound to this S

func (*S) NewTimer

func (s *S) NewTimer(name string, sampPercent int) *Timer

NewTimer creates a timer that's bound to this S

func (*S) Prefixed

func (s *S) Prefixed(prefix string) *S

Prefixed returns a new S that prefixes all stats with the given prefix

func (*S) Snapshot

func (s *S) Snapshot() (snap Snapshot)

Snapshot gets the last snapshot. If len(snap) == 0, then no snapshot has been taken yet.

type Snapshot

type Snapshot []Stat

A Snapshot is a slice of stats sorted by name.

func (*Snapshot) Add

func (s *Snapshot) Add(name Name, val interface{})

Add a new value to this snapshot. Must be one of [bool, int64, float64, string].

func (Snapshot) Dup

func (s Snapshot) Dup() (c Snapshot)

Dup makes a copy of the snapshot

func (Snapshot) Get

func (s Snapshot) Get(name Name) Stat

Get gets the Stat with the given name

func (*Snapshot) Take

func (s *Snapshot) Take(name Name, sr Snapshotter)

Take takes a snapshot of a snapshotter

type Snapshotter

type Snapshotter interface {
	Snapshot(a Adder)
}

A Snapshotter takes a snapshot of itself, resets as necessary, and pushes the snapshotted value(s) to the given Adder.

type Stat

type Stat struct {
	// String here, not Name: once in a snapshot, name should already have
	// been checked
	Name string
	Val  interface{}
}

A Stat is a single stat value

type StringGauge

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

A StringGauge is a single string value that can be set at will

func (*StringGauge) Get

func (g *StringGauge) Get() (v string)

Get gets the current value of the gauge

func (*StringGauge) Set

func (g *StringGauge) Set(v string)

Set sets the current value of this gauge

func (*StringGauge) Snapshot

func (g *StringGauge) Snapshot(a Adder)

Snapshot implements Snapshotter

type Timer

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

A Timer is used to time how long things take to run

func NewTimer

func NewTimer(name Name, sampPercent int) (t *Timer)

NewTimer creates a new timer that outputs stats prefixed with the given name. The names are cached internally to limit allocations.

The timer also reports percentiles of data by sampling. The given `sampPercent` controls what percent of samples to save for percentile calculations (0 - 100).

func (*Timer) Add

func (t *Timer) Add(dd time.Duration)

Add adds timing information

func (*Timer) Snapshot

func (t *Timer) Snapshot(a Adder)

Snapshot implements Snapshotter

func (*Timer) TimeFunc

func (t *Timer) TimeFunc(cb func())

TimeFunc times how long it takes the given function to run

Jump to

Keyboard shortcuts

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