stats

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package Meter yields summarized data describing a series of timed events.

Index

Constants

View Source
const (
	DefaultMaxPacketSize     = 1432
	DefaultMetricPrefix      = ""
	DefaultFlushInterval     = 100 * time.Millisecond
	DefaultReconnectInterval = time.Duration(0)
	DefaultReportInterval    = time.Minute
	DefaultRetryTimeout      = 5 * time.Second
	DefaultLogPrefix         = "[STATSD] "
	DefaultBufPoolCapacity   = 20
	DefaultSendQueueCapacity = 10
	DefaultSendLoopCount     = 1
)

Default settings

View Source
const (
	TagPlacementName = iota
	TagPlacementSuffix
)

Tag placement constants

Variables

View Source
var (
	// TagFormatInfluxDB is format for InfluxDB StatsD telegraf plugin
	//
	// Docs: https://github.com/influxdata/telegraf/tree/master/plugins/inputs/statsd
	TagFormatInfluxDB = &TagFormat{
		Placement:         TagPlacementName,
		FirstSeparator:    ",",
		OtherSeparator:    ',',
		KeyValueSeparator: '=',
	}

	// TagFormatDatadog is format for DogStatsD (Datadog Agent)
	//
	// Docs: https://docs.datadoghq.com/developers/dogstatsd/#datagram-format
	TagFormatDatadog = &TagFormat{
		Placement:         TagPlacementSuffix,
		FirstSeparator:    "|#",
		OtherSeparator:    ',',
		KeyValueSeparator: ':',
	}
)

Functions

This section is empty.

Types

type ClientOptions

type ClientOptions struct {
	// Addr is statsd server address in "host:port" format
	Addr string

	Size  int
	HBins int // Histogram bins.

	// MetricPrefix is metricPrefix to prepend to every metric being sent
	//
	// If not set defaults to empty string
	MetricPrefix string

	// MaxPacketSize is maximum UDP packet size
	//
	// Safe value is 1432 bytes, if your network supports jumbo frames,
	// this value could be raised up to 8960 bytes
	MaxPacketSize int

	// FlushInterval controls flushing incomplete UDP packets which makes
	// sure metric is not delayed longer than FlushInterval
	//
	// Default value is 100ms, setting FlushInterval to zero disables flushing
	FlushInterval time.Duration

	// ReconnectInterval controls UDP socket reconnects
	//
	// Reconnecting is important to follow DNS changes, e.g. in
	// dynamic container environments like K8s where statsd server
	// instance might be relocated leading to new IP address.
	//
	// By default reconnects are disabled
	ReconnectInterval time.Duration

	// RetryTimeout controls how often client should attempt reconnecting
	// to statsd server on failure
	//
	// Default value is 5 seconds
	RetryTimeout time.Duration

	// ReportInterval instructs client to report number of packets lost
	// each interval via Logger
	//
	// By default lost packets are reported every minute, setting to zero
	// disables reporting
	ReportInterval time.Duration

	// Logger is used by statsd client to report errors and lost packets
	//
	// If not set, default logger to stderr with metricPrefix `[STATSD] ` is being used
	Logger SomeLogger

	// BufPoolCapacity controls size of pre-allocated buffer cache
	//
	// Each buffer is MaxPacketSize. Cache allows to avoid allocating
	// new buffers during high load
	//
	// Default value is DefaultBufPoolCapacity
	BufPoolCapacity int

	// SendQueueCapacity controls length of the queue of packet ready to be sent
	//
	// Packets might stay in the queue during short load bursts or while
	// client is reconnecting to statsd
	//
	// Default value is DefaultSendQueueCapacity
	SendQueueCapacity int

	// SendLoopCount controls number of goroutines sending UDP packets
	//
	// Default value is 1, so packets are sent from single goroutine, this
	// value might need to be bumped under high load
	SendLoopCount int

	// TagFormat controls formatting of StatsD tags
	//
	// If tags are not used, value of this setting isn't used.
	//
	// There are two predefined formats: for InfluxDB and Datadog, default
	// format is InfluxDB tag format.
	TagFormat *TagFormat

	// DefaultTags is a list of tags to be applied to every metric
	DefaultTags []Tag
}

ClientOptions are statsd client settings

type Config

type Config struct {
	Size  int
	HBins int // Histogram bins.
	Addr  string
}

Config holds Meter initialization parameters. Size defines the sample capacity. Meter is thread safe.

type Option

type Option func(c *ClientOptions)

Option is type for option transport

func BufPoolCapacity

func BufPoolCapacity(capacity int) Option

BufPoolCapacity controls size of pre-allocated buffer cache

Each buffer is MaxPacketSize. Cache allows to avoid allocating new buffers during high load

Default value is DefaultBufPoolCapacity

func DefaultTags

func DefaultTags(tags ...Tag) Option

DefaultTags defines a list of tags to be applied to every metric

func FlushInterval

func FlushInterval(interval time.Duration) Option

FlushInterval controls flushing incomplete UDP packets which makes sure metric is not delayed longer than FlushInterval

Default value is 100ms, setting FlushInterval to zero disables flushing

func Logger

func Logger(logger SomeLogger) Option

Logger is used by statsd client to report errors and lost packets

If not set, default logger to stderr with metricPrefix `[STATSD] ` is being used

func MaxPacketSize

func MaxPacketSize(packetSize int) Option

MaxPacketSize control maximum UDP packet size

Default value is DefaultMaxPacketSize

func MetricPrefix

func MetricPrefix(prefix string) Option

MetricPrefix is metricPrefix to prepend to every metric being sent

Usually metrics are prefixed with app name, e.g. `app.`. To avoid providing this metricPrefix for every metric being collected, and to enable shared libraries to collect metric under app name, use MetricPrefix to set global metricPrefix for all the app metrics, e.g. `MetricPrefix("app".)`.

If not set defaults to empty string

func ReconnectInterval

func ReconnectInterval(interval time.Duration) Option

ReconnectInterval controls UDP socket reconnects

Reconnecting is important to follow DNS changes, e.g. in dynamic container environments like K8s where statsd server instance might be relocated leading to new IP address.

By default reconnects are disabled

func ReportInterval

func ReportInterval(interval time.Duration) Option

ReportInterval instructs client to report number of packets lost each interval via Logger

By default lost packets are reported every minute, setting to zero disables reporting

func RetryTimeout

func RetryTimeout(timeout time.Duration) Option

RetryTimeout controls how often client should attempt reconnecting to statsd server on failure

Default value is 5 seconds

func SendLoopCount

func SendLoopCount(threads int) Option

SendLoopCount controls number of goroutines sending UDP packets

Default value is 1, so packets are sent from single goroutine, this value might need to be bumped under high load

func SendQueueCapacity

func SendQueueCapacity(capacity int) Option

SendQueueCapacity controls length of the queue of packet ready to be sent

Packets might stay in the queue during short load bursts or while client is reconnecting to statsd

Default value is DefaultSendQueueCapacity

func TagStyle

func TagStyle(style *TagFormat) Option

TagStyle controls formatting of StatsD tags

There are two predefined formats: for InfluxDB and Datadog, default format is InfluxDB tag format.

type SomeLogger

type SomeLogger interface {
	Printf(fmt string, args ...interface{})
}

SomeLogger defines logging interface that allows using 3rd party loggers (e.g. github.com/sirupsen/logrus) with this Statsd client.

type Stats

type Stats struct {
	sync.Mutex

	Count uint64
	// contains filtered or unexported fields
}

Meter holds event durations and counts.

func New

func New(c *Config, options ...Option) *Stats

New initializes a new Meter.

func (*Stats) Decr

func (s *Stats) Decr(stat string, count int64, tags ...Tag)

Decr decrements a counter metri

Often used to note a particular event

func (*Stats) Incr

func (s *Stats) Incr(stat string, count int64, tags ...Tag)

Incr increments a counter metric

Often used to note a particular event, for example incoming web request.

func (*Stats) PrecisionTiming

func (s *Stats) PrecisionTiming(stat string, delta time.Duration, tags ...Tag)

PrecisionTiming track a duration event, the time delta has to be a duration

Usually request processing time, time to run database query, etc. are used with this metric type.

func (*Stats) SetAdd

func (s *Stats) SetAdd(stat string, value string, tags ...Tag)

SetAdd adds unique element to a set

Statsd server will provide cardinality of the set over aggregation period.

func (*Stats) Timing

func (s *Stats) Timing(stat string, delta int64, tags ...Tag)

Timing tracks a duration event, the time delta must be given in milliseconds

func (*Stats) Unregister

func (s *Stats) Unregister()

type Tag

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

Tag is metric-specific tag

func Int64Tag

func Int64Tag(name string, value int64) Tag

Int64Tag creates Tag with integer value

func IntTag

func IntTag(name string, value int) Tag

IntTag creates Tag with integer value

func StringTag

func StringTag(name, value string) Tag

StringTag creates Tag with string value

func (Tag) Append

func (tag Tag) Append(buf []byte, style *TagFormat) []byte

Append formats tag and appends it to the buffer

type TagFormat

type TagFormat struct {
	// FirstSeparator is put after metric name and before first tag
	FirstSeparator string
	// Placement specifies part of the message to append tags to
	Placement byte
	// OtherSeparator separates 2nd and subsequent tags from each other
	OtherSeparator byte
	// KeyValueSeparator separates tag name and tag value
	KeyValueSeparator byte
}

TagFormat controls tag formatting style

Jump to

Keyboard shortcuts

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