Skip to content

Import path: gitlab.soludian.com/soludian/fountain/libs/metrics/stats_d

stats_d

go
import "gitlab.soludian.com/soludian/fountain/libs/metrics/stats_d"

Package stats_d provides a StatsD backend for package metrics. StatsD has no concept of arbitrary key-value tagging, so label values are not supported, and With is a no-op on all metrics.

This package batches observations and emits them on some schedule to the remote server. This is useful even if you connect to your StatsD server over UDP. Emitting one network packet per observation can quickly overwhelm even the fastest internal network.

Index

Variables

go
var KPackageName = "stats_d"

type Counter

Counter is a StatsD counter. Observations are forwarded to a StatsD object, and aggregated (summed) per time series.

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

func (*Counter) Add

go
func (c *Counter) Add(delta float64)

Add implements metrics.Counter.

func (*Counter) With

go
func (c *Counter) With(...string) metrics.Counter

With is a no-op.

type Gauge

Gauge is a StatsD gauge. Observations are forwarded to a StatsD object, and aggregated (the last observation selected) per time series.

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

func (*Gauge) Add

go
func (g *Gauge) Add(delta float64)

Add implements metrics.Gauge.

func (*Gauge) Set

go
func (g *Gauge) Set(value float64)

Set implements metrics.Gauge.

func (*Gauge) With

go
func (g *Gauge) With(...string) metrics.Gauge

With is a no-op.

type StatsD

StatsD receives metrics observations and forwards them to a StatsD server. Create a StatsD object, use it to create metrics, and pass those metrics as dependencies to the components that will use them.

All metrics are buffered until WriteTo is called. Counters and gauges are aggregated into a single observation per time series per write. Timings are buffered but not aggregated.

To regularly report metrics to an io.Writer, use the WriteLoop helper method. To send to a StatsD server, use the SendLoop helper method.

go
type StatsD struct {
    // contains filtered or unexported fields
}

func New

go
func New(prefix string, logger flog.FlogInf) *StatsD

New returns a StatsD object that may be used to create metrics. Prefix is applied to all created metrics. Callers must ensure that regular calls to WriteTo are performed, either manually or with one of the helper methods.

func (*StatsD) NewCounter

go
func (s *StatsD) NewCounter(name string, sampleRate float64) *Counter

NewCounter returns a counter, sending observations to this StatsD object.

func (*StatsD) NewGauge

go
func (s *StatsD) NewGauge(name string) *Gauge

NewGauge returns a gauge, sending observations to this StatsD object.

func (*StatsD) NewTiming

go
func (s *StatsD) NewTiming(name string, sampleRate float64) *Timing

NewTiming returns a histogram whose observations are interpreted as millisecond durations, and are forwarded to this StatsD object.

func (*StatsD) SendLoop

go
func (s *StatsD) SendLoop(ctx context.Context, c <-chan time.Time, network, address string)

SendLoop is a helper method that wraps WriteLoop, passing a managed connection to the network and address. Like WriteLoop, this method blocks until ctx is canceled, so clients probably want to start it in its own goroutine. For typical usage, create a time.Ticker and pass its C channel to this method.

func (*StatsD) WriteLoop

go
func (s *StatsD) WriteLoop(ctx context.Context, c <-chan time.Time, w io.Writer)

WriteLoop is a helper method that invokes WriteTo to the passed writer every time the passed channel fires. This method blocks until ctx is canceled, so clients probably want to run it in its own goroutine. For typical usage, create a time.Ticker and pass its C channel to this method.

func (*StatsD) WriteTo

go
func (s *StatsD) WriteTo(w io.Writer) (count int64, err error)

WriteTo flushes the buffered content of the metrics to the writer, in StatsD format. WriteTo abides best-effort semantics, so observations are lost if there is a problem with the write. Clients should be sure to call WriteTo regularly, ideally through the WriteLoop or SendLoop helper methods.

type Timing

Timing is a StatsD timing, or metrics.Histogram. Observations are forwarded to a StatsD object, and collected (but not aggregated) per time series.

go
type Timing struct {
    // contains filtered or unexported fields
}

func (*Timing) Observe

go
func (t *Timing) Observe(value float64)

Observe implements metrics.Histogram. Value is interpreted as milliseconds.

func (*Timing) With

go
func (t *Timing) With(...string) metrics.Histogram

With is a no-op.

Generated by gomarkdoc