stats

package module
v0.0.0-...-7b4c2c9 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2018 License: BSD-3-Clause Imports: 12 Imported by: 0

README

stats · CircleCI Status GitHub license PRs Welcome

stats - a stats facade for golang

GoDoc

Package stats provides a facade around push based stats instrumentation. It allows you to put simple instrumentation calls in your code:

	stats.Incr("get_request")

Then you can turn this off, change where the stats go, etc at startup time.

See the GoDoc for more details.

Documentation

Overview

Package stats provides a facade around push based stats instrumentation. It allows you to put simple instrumentation calls in your code:

stats.Incr("get_request")

Then you can turn this off, change where the stats go, etc at startup time.

The basic architecture of the stats package has the concept of Brokers, Endpoints and producers. Producers send a stat to a broker which then multiplexes the stats to all registered endpoints.

If no Broker reference is used the DefaultBroker is used, it is started at initialization time.

Example
//stats will be prefixed with com.example.foo
ctx := SetPrefix(context.Background(), "com.example.foo")

//statsd location
ctx = SetStatsdURL(ctx, "stats.example.com:57475")

//data dog location
ctx = SetDatadogURL(ctx, "https://app.datadoghq.com/api/")

// will start forwarding stats to data dog and statsd (as they are in the context) but not graphite
err := RegisterStatsContext(ctx)
if err != nil {
	log.Printf("Error registering stats context you won't get any stats: %v", err)
}

// this will start a goroutine that forwards standard golang runtime stats (like gc/thread counts etc)
err = RegisterRuntimeStatsContext(ctx)
if err != nil {
	log.Printf("Error registering runtime stats context you won't get runtime stats: %v", err)
}

// will send an event named com.example.foo.foo with the bar.baz data.  Will not go to graphite as it isn't set up in the context
// will not go to statsd as it doesn't handle event stats, will go to data dog as it does
Event("foo", "bar.baz")

//Sends com.example.foo.boom|7|c
Count("boom", 7)

//Sends com.example.bam|1|c
Incr("bam")
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrActivityBufferFull = fmt.Errorf("stats activity buffer full")

ErrActivityBufferFull is returned if the brokers buffer is full when attempting to register an endpoint or stop the broker

Functions

func BigGauge

func BigGauge(name string, i uint64)

BigGauge sends a big gauge value for the given name

func Count

func Count(name string, i int)

Count sends a count value for the given name

func Event

func Event(tag string, data string)

Event will send an event

func Finish

func Finish(ctx context.Context) error

Finish will finish the DefaultBroker

func Gauge

func Gauge(name string, i int)

Gauge sends a gauge value for the given name

func GetPrefix

func GetPrefix(ctx context.Context) string

GetPrefix gets the prefix

func GraphiteEvent

func GraphiteEvent(e *graphite.Event)

GraphiteEvent will send a graphite event

func HasStats

func HasStats(ctx context.Context) (hasStatsdURL bool, hasGraphiteURL bool)

HasStats checks if the statsd url and graphite url are set

func Incr

func Incr(name string)

Incr increments a count by 1

func Off

func Off(name string)

Off sends a 0 gauge

func On

func On(name string)

On sends a 1 gauge

func RegisterRuntimeStatsContext

func RegisterRuntimeStatsContext(ctx context.Context) error

RegisterRuntimeStatsContext starts runtime stats reporting based on the context

func RegisterStatsContext

func RegisterStatsContext(ctx context.Context) error

RegisterStatsContext starts endpoints based on what is set in the context

func RegisterStatsLogger

func RegisterStatsLogger(ctx context.Context)

RegisterStatsLogger starts a stats receiver that logs the stats. This is useful for tests but would probably destroy a production system.

func ReportRuntimeStats

func ReportRuntimeStats(ctx context.Context, sleep time.Duration) error

ReportRuntimeStats publishes runtime stats to statsd

func Send

func Send(datum interface{})

Send will send the supplied datum

func SetDatadogURL

func SetDatadogURL(ctx context.Context, url string) context.Context

SetDatadogURL sets the datadog statsd URL

func SetGraphite

func SetGraphite(ctx context.Context, url, user, password string, verbose bool) context.Context

SetGraphite sets the graphite client

func SetPrefix

func SetPrefix(ctx context.Context, prefix string) context.Context

SetPrefix sets the stats prefix

func SetRuntimeInterval

func SetRuntimeInterval(ctx context.Context, interval time.Duration) context.Context

SetRuntimeInterval sets the runtime stats collector interval

func SetStatsdURL

func SetStatsdURL(ctx context.Context, url string) context.Context

SetStatsdURL sets the stats prefix

func Timing

func Timing(name string, i int)

Timing sends a timing value for the given name

func TimingDuration

func TimingDuration(name string, duration time.Duration)

TimingDuration sends a timing value for the duration provided

func TimingPeriod

func TimingPeriod(name string, start time.Time, end time.Time)

TimingPeriod sends a timing value for the given start and end

Types

type Broker

type Broker chan interface{}

Broker is a coordination point for stats and endpoints

var DefaultBroker Broker

DefaultBroker is started by default and runs in the background

func StartBroker

func StartBroker(bufferSize int) Broker

StartBroker starts the background goroutine that listens for stats and forwards them

func (Broker) BigGauge

func (s Broker) BigGauge(name string, value uint64)

BigGauge sends a very big guage value for the given name

func (Broker) Count

func (s Broker) Count(name string, value int)

Count sends a count value for the given name

func (Broker) Event

func (s Broker) Event(tag string, data string)

Event will send an event

func (Broker) Finish

func (s Broker) Finish(ctx context.Context) error

Finish will attempt to shutdown the broker and all endpoints after sending buffered stats

func (Broker) Gauge

func (s Broker) Gauge(name string, value int)

Gauge sends a gauge value for the given name

func (Broker) GraphiteEvent

func (s Broker) GraphiteEvent(e *graphite.Event)

GraphiteEvent will send a graphite event

func (Broker) Incr

func (s Broker) Incr(name string)

Incr increments a count by 1

func (Broker) Off

func (s Broker) Off(name string)

Off sends a 0 gauge

func (Broker) On

func (s Broker) On(name string)

On sends a 1 gauge

func (Broker) RegisterEndpoint

func (s Broker) RegisterEndpoint(e Endpoint) error

RegisterEndpoint will add an endpoint to the list, the provided context will be listened to for cancellation

func (Broker) Send

func (s Broker) Send(datum interface{})

Send will send the supplied datum

func (Broker) Timing

func (s Broker) Timing(name string, value int)

Timing sends a timing value for the given name

func (Broker) TimingDuration

func (s Broker) TimingDuration(name string, duration time.Duration)

TimingDuration sends a timing value for the duration provided

func (Broker) TimingPeriod

func (s Broker) TimingPeriod(name string, start time.Time, end time.Time)

TimingPeriod sends a timing value for the given start and end

type Endpoint

type Endpoint func(<-chan interface{})

Endpoint is a function that takes a channel of stats and reacts to them. It will be started in a go routine by the broker

func DatadogStatsdEndpoint

func DatadogStatsdEndpoint(s *ddStatsd.Client) Endpoint

DatadogStatsdEndpoint sends stats to a statsd client

func GraphiteEndpoint

func GraphiteEndpoint(govent *graphite.Graphite) Endpoint

GraphiteEndpoint sends event data to a graphite endpoint

func LogEndpoint

func LogEndpoint() Endpoint

LogEndpoint is an endpoint that logs data

func StatsdEndpoint

func StatsdEndpoint(s *statsd.Client) Endpoint

StatsdEndpoint sends stats to a statsd client

Jump to

Keyboard shortcuts

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