statsd

package
v0.0.0-...-0c7158d Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2015 License: MIT Imports: 17 Imported by: 2

Documentation

Overview

Package statsd implements functionality for creating servers compatible with the statsd protocol. See https://github.com/b/statsd_spec for a description of the protocol.

The main components of the library are MetricReceiver and MetricAggregator, which are responsible for receiving and aggregating the metrics respectively. MetricAggregator receives Metric objects via its MetricChan and then aggregates them based on their type. At every FlushInterval the metrics are flushed via the aggregator's associated MetricSender object.

Currently the library implements just one type MetricSender, compatible with Graphite (http://graphite.wikidot.org), but any object implementing the MetricSender interface can be used with the library.

Index

Constants

View Source
const DefaultConsoleAddr = ":8126"

DefaultConsoleAddr is the default address on which a ConsoleServer will listen

View Source
const DefaultMetricsAddr = ":8125"

DefaultMetricsAddr is the default address on which a MetricReceiver will listen

Variables

This section is empty.

Functions

func Connect

func Connect(addr string) (conn net.Conn, err error)

Types

type ConsoleServer

type ConsoleServer struct {
	Addr       string
	Aggregator *MetricAggregator
}

ConsoleServer is an object that listens for telnet connection on a TCP address Addr and provides a console interface to a manage a MetricAggregator

func (*ConsoleServer) ListenAndServe

func (s *ConsoleServer) ListenAndServe() error

ListenAndServe listens on the ConsoleServer's TCP network address and then calls Serve

func (*ConsoleServer) Serve

func (s *ConsoleServer) Serve(l net.Listener) error

Serve accepts incoming connections on the listener and serves them a console interface to the MetricAggregator

type GraphiteClient

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

GraphiteClient is an object that is used to send messages to a Graphite server's UDP interface

func NewGraphiteClient

func NewGraphiteClient(addr string) (client GraphiteClient, err error)

NewGraphiteClient constructs a GraphiteClient object by connecting to an address

func (*GraphiteClient) Reconnect

func (client *GraphiteClient) Reconnect()

func (*GraphiteClient) SendMetrics

func (client *GraphiteClient) SendMetrics(metrics MetricMap) (err error)

SendMetrics sends the metrics in a MetricsMap to the Graphite server

type Handler

type Handler interface {
	HandleMetric(m Metric)
}

Objects implementing the Handler interface can be used to handle metrics for a MetricReceiver

type HandlerFunc

type HandlerFunc func(Metric)

The HandlerFunc type is an adapter to allow the use of ordinary functions as metric handlers

func (HandlerFunc) HandleMetric

func (f HandlerFunc) HandleMetric(m Metric)

HandleMetric calls f(m)

type Metric

type Metric struct {
	Type       MetricType // The type of metric
	Bucket     string     // The name of the bucket where the metric belongs
	Value      float64    // The numeric value of the metric
	SampleRate float64    // The sample rate of the metric
}

Metric represents a single data collected datapoint

func (Metric) String

func (m Metric) String() string

type MetricAggregator

type MetricAggregator struct {
	sync.Mutex
	MetricChan     chan Metric   // Channel on which metrics are received
	FlushInterval  time.Duration // How often to flush metrics to the sender
	Sender         MetricSender  // The sender to which metrics are flushed
	Stats          metricAggregatorStats
	Counters       MetricMap
	Gauges         MetricMap
	Timers         MetricListMap
	TimersCounters MetricMap
}

MetricAggregator is an object that aggregates statsd metrics. The function NewMetricAggregator should be used to create the objects.

Incoming metrics should be sent to the MetricChan channel.

func NewMetricAggregator

func NewMetricAggregator(sender MetricSender, flushInterval time.Duration) MetricAggregator

NewMetricAggregator creates a new MetricAggregator object

func (*MetricAggregator) Aggregate

func (a *MetricAggregator) Aggregate()

Aggregate starts the MetricAggregator so it begins consuming metrics from MetricChan and flushing them periodically via its Sender

func (*MetricAggregator) Reset

func (a *MetricAggregator) Reset()

Reset clears the contents of a MetricAggregator

type MetricListMap

type MetricListMap map[string][]float64

MetricListMap is simlar to MetricMap but instead of storing a single aggregated Metric value it stores a list of all collected values.

func (MetricListMap) String

func (m MetricListMap) String() string

type MetricMap

type MetricMap map[string]float64

MetricMap is used for storing aggregated Metric values. The keys of the map are metric bucket names.

func (MetricMap) String

func (m MetricMap) String() string

type MetricReceiver

type MetricReceiver struct {
	Addr    string  // UDP address on which to listen for metrics
	Handler Handler // handler to invoke
}

MetricReceiver receives data on its listening port and converts lines in to Metrics. For each Metric it calls r.Handler.HandleMetric()

func (*MetricReceiver) ListenAndReceive

func (r *MetricReceiver) ListenAndReceive() error

ListenAndReceive listens on the UDP network address of srv.Addr and then calls Receive to handle the incoming datagrams. If Addr is blank then DefaultMetricsAddr is used.

func (*MetricReceiver) Receive

func (r *MetricReceiver) Receive(c net.PacketConn) error

Receive accepts incoming datagrams on c and calls r.Handler.HandleMetric() for each line in the datagram that successfully parses in to a Metric

type MetricSender

type MetricSender interface {
	SendMetrics(MetricMap) error
}

MetricSender is an interface that can be implemented by objects which can provide metrics to a MetricAggregator

type MetricSenderFunc

type MetricSenderFunc func(MetricMap) error

func (MetricSenderFunc) SendMetrics

func (f MetricSenderFunc) SendMetrics(m MetricMap) error

SendMetrics calls f(m)

type MetricType

type MetricType float64

MetricType is an enumeration of all the possible types of Metric

const (
	ERROR MetricType = 1 << (10 * iota)
	COUNTER
	TIMER
	GAUGE
)

func (MetricType) String

func (m MetricType) String() string

type StatsdClient

type StatsdClient struct {
	Host string
	Port int
	// contains filtered or unexported fields
}

func NewStatsdClient

func NewStatsdClient(host string, port int) *StatsdClient

*

  • Factory method to initialize udp connection
  • Usage: *
  • import "statsd"
  • client := statsd.NewStatsdClient('localhost', 8125) *

func (*StatsdClient) Close

func (client *StatsdClient) Close()

*

  • Method to close udp connection *

func (*StatsdClient) Decrement

func (client *StatsdClient) Decrement(stat string)

*

  • Decrements one stat counter without sampling
  • Usage: *
  • import "statsd"
  • client := statsd.NewStatsdClient('localhost', 8125)
  • client.Decrement('foo.bar') *

func (*StatsdClient) DecrementWithSampling

func (client *StatsdClient) DecrementWithSampling(stat string, sampleRate float32)

*

  • Decrements one stat counter with sampling
  • Usage: *
  • import "statsd"
  • client := statsd.NewStatsdClient('localhost', 8125)
  • client.Decrement('foo.bar', 0.2) *

func (*StatsdClient) Increment

func (client *StatsdClient) Increment(stat string)

*

  • Increments one stat counter without sampling
  • Usage: *
  • import "statsd"
  • client := statsd.NewStatsdClient('localhost', 8125)
  • client.Increment('foo.bar') *

func (*StatsdClient) IncrementWithSampling

func (client *StatsdClient) IncrementWithSampling(stat string, sampleRate float32)

*

  • Increments one stat counter with sampling
  • Usage: *
  • import "statsd"
  • client := statsd.NewStatsdClient('localhost', 8125)
  • client.Increment('foo.bar', 0.2) *

func (*StatsdClient) Open

func (client *StatsdClient) Open()

*

  • Method to open udp connection, called by default client factory *

func (*StatsdClient) Send

func (client *StatsdClient) Send(data map[string]string, sampleRate float32)

*

  • Sends data to udp statsd daemon *

func (*StatsdClient) Timing

func (client *StatsdClient) Timing(stat string, time int64)

* Log timing information (in milliseconds) without sampling * Usage: * * import "statsd" * import "time" * client := statsd.NewStatsdClient('localhost', 8125) * t1 := time.Now() * expensiveCall() * t2 := time.Now() * duration := int64(t2.Sub(t1)/time.Millisecond) * client.Timing("foo.time", duration) *

func (*StatsdClient) TimingWithSampleRate

func (client *StatsdClient) TimingWithSampleRate(stat string, time int64, sampleRate float32)

*

  • Log timing information (in milliseconds) with sampling
  • Usage: *
  • import "statsd"
  • import "time"
  • client := statsd.NewStatsdClient('localhost', 8125)
  • t1 := time.Now()
  • expensiveCall()
  • t2 := time.Now()
  • duration := int64(t2.Sub(t1)/time.Millisecond)
  • client.TimingWithSampleRate("foo.time", duration, 0.2) *

func (*StatsdClient) UpdateStats

func (client *StatsdClient) UpdateStats(stats []string, delta int, sampleRate float32)

*

  • Arbitrarily updates a list of stats by a delta *

type WebConsoleServer

type WebConsoleServer struct {
	Addr       string
	Aggregator *MetricAggregator
}

WebConsoleServer is an object that listens for HTTP connection on a TCP address Addr and provides a web interface for its MetricAggregator

func (*WebConsoleServer) ListenAndServe

func (s *WebConsoleServer) ListenAndServe() error

ListenAndServe listens on the ConsoleServer's TCP network address and then calls Serve

func (*WebConsoleServer) ServeHTTP

func (s *WebConsoleServer) ServeHTTP(w http.ResponseWriter, req *http.Request)

Jump to

Keyboard shortcuts

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