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 ¶
- Variables
- func BigGauge(name string, i uint64)
- func Count(name string, i int)
- func Event(tag string, data string)
- func Finish(ctx context.Context) error
- func Gauge(name string, i int)
- func GetPrefix(ctx context.Context) string
- func GraphiteEvent(e *graphite.Event)
- func HasStats(ctx context.Context) (hasStatsdURL bool, hasGraphiteURL bool)
- func Incr(name string)
- func Off(name string)
- func On(name string)
- func RegisterRuntimeStatsContext(ctx context.Context) error
- func RegisterStatsContext(ctx context.Context) error
- func RegisterStatsLogger(ctx context.Context)
- func ReportRuntimeStats(ctx context.Context, sleep time.Duration) error
- func Send(datum interface{})
- func SetDatadogURL(ctx context.Context, url string) context.Context
- func SetGraphite(ctx context.Context, url, user, password string, verbose bool) context.Context
- func SetPrefix(ctx context.Context, prefix string) context.Context
- func SetRuntimeInterval(ctx context.Context, interval time.Duration) context.Context
- func SetStatsdURL(ctx context.Context, url string) context.Context
- func Timing(name string, i int)
- func TimingDuration(name string, duration time.Duration)
- func TimingPeriod(name string, start time.Time, end time.Time)
- type Broker
- func (s Broker) BigGauge(name string, value uint64)
- func (s Broker) Count(name string, value int)
- func (s Broker) Event(tag string, data string)
- func (s Broker) Finish(ctx context.Context) error
- func (s Broker) Gauge(name string, value int)
- func (s Broker) GraphiteEvent(e *graphite.Event)
- func (s Broker) Incr(name string)
- func (s Broker) Off(name string)
- func (s Broker) On(name string)
- func (s Broker) RegisterEndpoint(e Endpoint) error
- func (s Broker) Send(datum interface{})
- func (s Broker) Timing(name string, value int)
- func (s Broker) TimingDuration(name string, duration time.Duration)
- func (s Broker) TimingPeriod(name string, start time.Time, end time.Time)
- type Endpoint
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 RegisterRuntimeStatsContext ¶
RegisterRuntimeStatsContext starts runtime stats reporting based on the context
func RegisterStatsContext ¶
RegisterStatsContext starts endpoints based on what is set in the context
func RegisterStatsLogger ¶
RegisterStatsLogger starts a stats receiver that logs the stats. This is useful for tests but would probably destroy a production system.
func ReportRuntimeStats ¶
ReportRuntimeStats publishes runtime stats to statsd
func SetDatadogURL ¶
SetDatadogURL sets the datadog statsd URL
func SetGraphite ¶
SetGraphite sets the graphite client
func SetRuntimeInterval ¶
SetRuntimeInterval sets the runtime stats collector interval
func SetStatsdURL ¶
SetStatsdURL sets the stats prefix
func TimingDuration ¶
TimingDuration sends a timing value for the duration provided
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 ¶
StartBroker starts the background goroutine that listens for stats and forwards them
func (Broker) Finish ¶
Finish will attempt to shutdown the broker and all endpoints after sending buffered stats
func (Broker) GraphiteEvent ¶
GraphiteEvent will send a graphite event
func (Broker) RegisterEndpoint ¶
RegisterEndpoint will add an endpoint to the list, the provided context will be listened to for cancellation
func (Broker) TimingDuration ¶
TimingDuration sends a timing value for the duration provided
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 ¶
DatadogStatsdEndpoint sends stats to a statsd client
func GraphiteEndpoint ¶
GraphiteEndpoint sends event data to a graphite endpoint
func StatsdEndpoint ¶
func StatsdEndpoint(s *statsd.Client) Endpoint
StatsdEndpoint sends stats to a statsd client