statsd

package
v4.8.3+incompatible Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2021 License: MIT Imports: 11 Imported by: 742

README

Overview

Package statsd provides a Go dogstatsd client. Dogstatsd extends Statsd, adding tags and histograms.

Documentation

Overview

Package statsd provides a Go dogstatsd client. Dogstatsd extends the popular statsd, adding tags and histograms and pushing upstream to Datadog.

Refer to http://docs.datadoghq.com/guides/dogstatsd/ for information about DogStatsD.

statsd is based on go-statsd-client.

Index

Constants

View Source
const (
	WriterNameUDP     string = "udp"
	WriterNameUDS     string = "uds"
	WriterWindowsPipe string = "pipe"
)
View Source
const DefaultMaxAgentPayloadSize = 8192

DefaultMaxAgentPayloadSize is the default maximum payload size the agent can receive. This can be adjusted by changing dogstatsd_buffer_size in the agent configuration file datadog.yaml. This is also used as the optimal payload size for UDS datagrams.

View Source
const DefaultUDPBufferPoolSize = 2048

DefaultUDPBufferPoolSize is the default size of the buffer pool for UDP clients.

View Source
const DefaultUDSBufferPoolSize = 512

DefaultUDSBufferPoolSize is the default size of the buffer pool for UDS clients.

View Source
const ErrNoClient = noClientErr("statsd client is nil")

ErrNoClient is returned if statsd reporting methods are invoked on a nil client.

View Source
const MaxUDPPayloadSize = 65467

MaxUDPPayloadSize defines the maximum payload size for a UDP datagram. Its value comes from the calculation: 65535 bytes Max UDP datagram size - 8byte UDP header - 60byte max IP headers any number greater than that will see frames being cut out.

View Source
const OptimalUDPPayloadSize = 1432

OptimalUDPPayloadSize defines the optimal payload size for a UDP datagram, 1432 bytes is optimal for regular networks with an MTU of 1500 so datagrams don't get fragmented. It's generally recommended not to fragment UDP datagrams as losing a single fragment will cause the entire datagram to be lost.

View Source
const TelemetryInterval = 10 * time.Second

TelemetryInterval is the interval at which telemetry will be sent by the client.

View Source
const UnixAddressPrefix = "unix://"

UnixAddressPrefix holds the prefix to use to enable Unix Domain Socket traffic instead of UDP.

View Source
const WindowsPipeAddressPrefix = `\\.\pipe\`

WindowsPipeAddressPrefix holds the prefix to use to enable Windows Named Pipes traffic instead of UDP.

Variables

View Source
var (
	// DefaultNamespace is the default value for the Namespace option
	DefaultNamespace = ""
	// DefaultTags is the default value for the Tags option
	DefaultTags = []string{}
	// DefaultMaxBytesPerPayload is the default value for the MaxBytesPerPayload option
	DefaultMaxBytesPerPayload = 0
	// DefaultMaxMessagesPerPayload is the default value for the MaxMessagesPerPayload option
	DefaultMaxMessagesPerPayload = math.MaxInt32
	// DefaultBufferPoolSize is the default value for the DefaultBufferPoolSize option
	DefaultBufferPoolSize = 0
	// DefaultBufferFlushInterval is the default value for the BufferFlushInterval option
	DefaultBufferFlushInterval = 100 * time.Millisecond
	// DefaultBufferShardCount is the default value for the BufferShardCount option
	DefaultBufferShardCount = 32
	// DefaultSenderQueueSize is the default value for the DefaultSenderQueueSize option
	DefaultSenderQueueSize = 0
	// DefaultWriteTimeoutUDS is the default value for the WriteTimeoutUDS option
	DefaultWriteTimeoutUDS = 100 * time.Millisecond
	// DefaultTelemetry is the default value for the Telemetry option
	DefaultTelemetry = true
	// DefaultReceivingMode is the default behavior when sending metrics
	DefaultReceivingMode = MutexMode
	// DefaultChannelModeBufferSize is the default size of the channel holding incoming metrics
	DefaultChannelModeBufferSize = 4096
	// DefaultAggregationFlushInterval is the default interval for the aggregator to flush metrics.
	// This should divide the Agent reporting period (default=10s) evenly to reduce "aliasing" that
	// can cause values to appear irregular.
	DefaultAggregationFlushInterval = 2 * time.Second
	// DefaultAggregation
	DefaultAggregation = false
	// DefaultExtendedAggregation
	DefaultExtendedAggregation = false
	// DefaultDevMode
	DefaultDevMode = false
)

Functions

This section is empty.

Types

type Client

type Client struct {

	// Namespace to prepend to all statsd calls
	Namespace string
	// Tags are global tags to be added to every statsd call
	Tags []string
	// skipErrors turns off error passing and allows UDS to emulate UDP behaviour
	SkipErrors bool
	// contains filtered or unexported fields
}

A Client is a handle for sending messages to dogstatsd. It is safe to use one Client from multiple goroutines simultaneously.

func CloneWithExtraOptions

func CloneWithExtraOptions(c *Client, options ...Option) (*Client, error)

CloneWithExtraOptions create a new Client with extra options

func New

func New(addr string, options ...Option) (*Client, error)

New returns a pointer to a new Client given an addr in the format "hostname:port" for UDP, "unix:///path/to/socket" for UDS or "\\.\pipe\path\to\pipe" for Windows Named Pipes.

func NewBuffered

func NewBuffered(addr string, buflen int) (*Client, error)

NewBuffered returns a Client that buffers its output and sends it in chunks. Buflen is the length of the buffer in number of commands.

When addr is empty, the client will default to a UDP client and use the DD_AGENT_HOST and (optionally) the DD_DOGSTATSD_PORT environment variables to build the target address.

func NewWithWriter

func NewWithWriter(w statsdWriter, options ...Option) (*Client, error)

NewWithWriter creates a new Client with given writer. Writer is a io.WriteCloser + SetWriteTimeout(time.Duration) error

func (*Client) Close

func (c *Client) Close() error

Close the client connection.

func (*Client) Count

func (c *Client) Count(name string, value int64, tags []string, rate float64) error

Count tracks how many times something happened per second.

func (*Client) Decr

func (c *Client) Decr(name string, tags []string, rate float64) error

Decr is just Count of -1

func (*Client) Distribution

func (c *Client) Distribution(name string, value float64, tags []string, rate float64) error

Distribution tracks the statistical distribution of a set of values across your infrastructure.

func (*Client) Event

func (c *Client) Event(e *Event) error

Event sends the provided Event.

func (*Client) Flush

func (c *Client) Flush() error

Flush forces a flush of all the queued dogstatsd payloads This method is blocking and will not return until everything is sent through the network. In MutexMode, this will also block sampling new data to the client while the workers and sender are flushed.

func (*Client) FlushTelemetryMetrics

func (c *Client) FlushTelemetryMetrics() ClientMetrics

func (*Client) Gauge

func (c *Client) Gauge(name string, value float64, tags []string, rate float64) error

Gauge measures the value of a metric at a particular time.

func (*Client) Histogram

func (c *Client) Histogram(name string, value float64, tags []string, rate float64) error

Histogram tracks the statistical distribution of a set of values on each host.

func (*Client) Incr

func (c *Client) Incr(name string, tags []string, rate float64) error

Incr is just Count of 1

func (*Client) ServiceCheck

func (c *Client) ServiceCheck(sc *ServiceCheck) error

ServiceCheck sends the provided ServiceCheck.

func (*Client) Set

func (c *Client) Set(name string, value string, tags []string, rate float64) error

Set counts the number of unique elements in a group.

func (*Client) SetWriteTimeout

func (c *Client) SetWriteTimeout(d time.Duration) error

SetWriteTimeout allows the user to set a custom UDS write timeout. Not supported for UDP or Windows Pipes.

func (*Client) SimpleEvent

func (c *Client) SimpleEvent(title, text string) error

SimpleEvent sends an event with the provided title and text.

func (*Client) SimpleServiceCheck

func (c *Client) SimpleServiceCheck(name string, status ServiceCheckStatus) error

SimpleServiceCheck sends an serviceCheck with the provided name and status.

func (*Client) TimeInMilliseconds

func (c *Client) TimeInMilliseconds(name string, value float64, tags []string, rate float64) error

TimeInMilliseconds sends timing information in milliseconds. It is flushed by statsd with percentiles, mean and other info (https://github.com/etsy/statsd/blob/master/docs/metric_types.md#timing)

func (*Client) Timing

func (c *Client) Timing(name string, value time.Duration, tags []string, rate float64) error

Timing sends timing information, it is an alias for TimeInMilliseconds

type ClientInterface

type ClientInterface interface {
	// Gauge measures the value of a metric at a particular time.
	Gauge(name string, value float64, tags []string, rate float64) error

	// Count tracks how many times something happened per second.
	Count(name string, value int64, tags []string, rate float64) error

	// Histogram tracks the statistical distribution of a set of values on each host.
	Histogram(name string, value float64, tags []string, rate float64) error

	// Distribution tracks the statistical distribution of a set of values across your infrastructure.
	Distribution(name string, value float64, tags []string, rate float64) error

	// Decr is just Count of -1
	Decr(name string, tags []string, rate float64) error

	// Incr is just Count of 1
	Incr(name string, tags []string, rate float64) error

	// Set counts the number of unique elements in a group.
	Set(name string, value string, tags []string, rate float64) error

	// Timing sends timing information, it is an alias for TimeInMilliseconds
	Timing(name string, value time.Duration, tags []string, rate float64) error

	// TimeInMilliseconds sends timing information in milliseconds.
	// It is flushed by statsd with percentiles, mean and other info (https://github.com/etsy/statsd/blob/master/docs/metric_types.md#timing)
	TimeInMilliseconds(name string, value float64, tags []string, rate float64) error

	// Event sends the provided Event.
	Event(e *Event) error

	// SimpleEvent sends an event with the provided title and text.
	SimpleEvent(title, text string) error

	// ServiceCheck sends the provided ServiceCheck.
	ServiceCheck(sc *ServiceCheck) error

	// SimpleServiceCheck sends an serviceCheck with the provided name and status.
	SimpleServiceCheck(name string, status ServiceCheckStatus) error

	// Close the client connection.
	Close() error

	// Flush forces a flush of all the queued dogstatsd payloads.
	Flush() error

	// SetWriteTimeout allows the user to set a custom write timeout.
	SetWriteTimeout(d time.Duration) error
}

ClientInterface is an interface that exposes the common client functions for the purpose of being able to provide a no-op client or even mocking. This can aid downstream users' with their testing.

type ClientMetrics

type ClientMetrics struct {
	TotalMetrics             uint64
	TotalMetricsGauge        uint64
	TotalMetricsCount        uint64
	TotalMetricsHistogram    uint64
	TotalMetricsDistribution uint64
	TotalMetricsSet          uint64
	TotalMetricsTiming       uint64
	TotalEvents              uint64
	TotalServiceChecks       uint64
	TotalDroppedOnReceive    uint64
}

ClientMetrics contains metrics about the client

type Event

type Event struct {
	// Title of the event.  Required.
	Title string
	// Text is the description of the event.  Required.
	Text string
	// Timestamp is a timestamp for the event.  If not provided, the dogstatsd
	// server will set this to the current time.
	Timestamp time.Time
	// Hostname for the event.
	Hostname string
	// AggregationKey groups this event with others of the same key.
	AggregationKey string
	// Priority of the event.  Can be statsd.Low or statsd.Normal.
	Priority EventPriority
	// SourceTypeName is a source type for the event.
	SourceTypeName string
	// AlertType can be statsd.Info, statsd.Error, statsd.Warning, or statsd.Success.
	// If absent, the default value applied by the dogstatsd server is Info.
	AlertType EventAlertType
	// Tags for the event.
	Tags []string
}

An Event is an object that can be posted to your DataDog event stream.

func NewEvent

func NewEvent(title, text string) *Event

NewEvent creates a new event with the given title and text. Error checking against these values is done at send-time, or upon running e.Check.

func (Event) Check

func (e Event) Check() error

Check verifies that an event is valid.

func (Event) Encode

func (e Event) Encode(tags ...string) (string, error)

Encode returns the dogstatsd wire protocol representation for an event. Tags may be passed which will be added to the encoded output but not to the Event's list of tags, eg. for default tags.

type EventAlertType

type EventAlertType string

EventAlertType is the alert type for events

const (
	// Info is the "info" AlertType for events
	Info EventAlertType = "info"
	// Error is the "error" AlertType for events
	Error EventAlertType = "error"
	// Warning is the "warning" AlertType for events
	Warning EventAlertType = "warning"
	// Success is the "success" AlertType for events
	Success EventAlertType = "success"
)

type EventPriority

type EventPriority string

EventPriority is the event priority for events

const (
	// Normal is the "normal" Priority for events
	Normal EventPriority = "normal"
	// Low is the "low" Priority for events
	Low EventPriority = "low"
)

type NoOpClient

type NoOpClient struct{}

NoOpClient is a statsd client that does nothing. Can be useful in testing situations for library users.

func (*NoOpClient) Close

func (n *NoOpClient) Close() error

Close does nothing and returns nil

func (*NoOpClient) Count

func (n *NoOpClient) Count(name string, value int64, tags []string, rate float64) error

Count does nothing and returns nil

func (*NoOpClient) Decr

func (n *NoOpClient) Decr(name string, tags []string, rate float64) error

Decr does nothing and returns nil

func (*NoOpClient) Distribution

func (n *NoOpClient) Distribution(name string, value float64, tags []string, rate float64) error

Distribution does nothing and returns nil

func (*NoOpClient) Event

func (n *NoOpClient) Event(e *Event) error

Event does nothing and returns nil

func (*NoOpClient) Flush

func (n *NoOpClient) Flush() error

Flush does nothing and returns nil

func (*NoOpClient) Gauge

func (n *NoOpClient) Gauge(name string, value float64, tags []string, rate float64) error

Gauge does nothing and returns nil

func (*NoOpClient) Histogram

func (n *NoOpClient) Histogram(name string, value float64, tags []string, rate float64) error

Histogram does nothing and returns nil

func (*NoOpClient) Incr

func (n *NoOpClient) Incr(name string, tags []string, rate float64) error

Incr does nothing and returns nil

func (*NoOpClient) ServiceCheck

func (n *NoOpClient) ServiceCheck(sc *ServiceCheck) error

ServiceCheck does nothing and returns nil

func (*NoOpClient) Set

func (n *NoOpClient) Set(name string, value string, tags []string, rate float64) error

Set does nothing and returns nil

func (*NoOpClient) SetWriteTimeout

func (n *NoOpClient) SetWriteTimeout(d time.Duration) error

SetWriteTimeout does nothing and returns nil

func (*NoOpClient) SimpleEvent

func (n *NoOpClient) SimpleEvent(title, text string) error

SimpleEvent does nothing and returns nil

func (*NoOpClient) SimpleServiceCheck

func (n *NoOpClient) SimpleServiceCheck(name string, status ServiceCheckStatus) error

SimpleServiceCheck does nothing and returns nil

func (*NoOpClient) TimeInMilliseconds

func (n *NoOpClient) TimeInMilliseconds(name string, value float64, tags []string, rate float64) error

TimeInMilliseconds does nothing and returns nil

func (*NoOpClient) Timing

func (n *NoOpClient) Timing(name string, value time.Duration, tags []string, rate float64) error

Timing does nothing and returns nil

type Option

type Option func(*Options) error

Option is a client option. Can return an error if validation fails.

func WithAggregationInterval

func WithAggregationInterval(interval time.Duration) Option

WithAggregationInterval set the aggregation interval

func WithBufferFlushInterval

func WithBufferFlushInterval(bufferFlushInterval time.Duration) Option

WithBufferFlushInterval sets the BufferFlushInterval option.

func WithBufferPoolSize

func WithBufferPoolSize(bufferPoolSize int) Option

WithBufferPoolSize sets the BufferPoolSize option.

func WithBufferShardCount

func WithBufferShardCount(bufferShardCount int) Option

WithBufferShardCount sets the BufferShardCount option.

func WithChannelMode

func WithChannelMode() Option

WithChannelMode will use channel to receive metrics

func WithChannelModeBufferSize

func WithChannelModeBufferSize(bufferSize int) Option

WithChannelModeBufferSize the channel buffer size when using "drop mode"

func WithClientSideAggregation

func WithClientSideAggregation() Option

WithClientSideAggregation enables client side aggregation for Gauges, Counts and Sets. Client side aggregation is a beta feature.

func WithDevMode

func WithDevMode() Option

WithDevMode enables client "dev" mode, sending more Telemetry metrics to help troubleshoot client behavior.

func WithExtendedClientSideAggregation

func WithExtendedClientSideAggregation() Option

WithExtendedClientSideAggregation enables client side aggregation for all types. This feature is only compatible with Agent's version >=6.25.0 && <7.0.0 or Agent's versions >=7.25.0. Client side aggregation is a beta feature.

func WithMaxBytesPerPayload

func WithMaxBytesPerPayload(MaxBytesPerPayload int) Option

WithMaxBytesPerPayload sets the MaxBytesPerPayload option.

func WithMaxMessagesPerPayload

func WithMaxMessagesPerPayload(maxMessagesPerPayload int) Option

WithMaxMessagesPerPayload sets the MaxMessagesPerPayload option.

func WithMutexMode

func WithMutexMode() Option

WithMutexMode will use mutex to receive metrics

func WithNamespace

func WithNamespace(namespace string) Option

WithNamespace sets the Namespace option.

func WithSenderQueueSize

func WithSenderQueueSize(senderQueueSize int) Option

WithSenderQueueSize sets the SenderQueueSize option.

func WithTags

func WithTags(tags []string) Option

WithTags sets the Tags option.

func WithTelemetryAddr

func WithTelemetryAddr(addr string) Option

WithTelemetryAddr specify a different address for telemetry metrics.

func WithWriteTimeoutUDS

func WithWriteTimeoutUDS(writeTimeoutUDS time.Duration) Option

WithWriteTimeoutUDS sets the WriteTimeoutUDS option.

func WithoutClientSideAggregation

func WithoutClientSideAggregation() Option

WithoutClientSideAggregation disables client side aggregation.

func WithoutDevMode

func WithoutDevMode() Option

WithoutDevMode disables client "dev" mode, sending more Telemetry metrics to help troubleshoot client behavior.

func WithoutTelemetry

func WithoutTelemetry() Option

WithoutTelemetry disables the telemetry

type Options

type Options struct {
	// Namespace to prepend to all metrics, events and service checks name.
	Namespace string
	// Tags are global tags to be applied to every metrics, events and service checks.
	Tags []string
	// MaxBytesPerPayload is the maximum number of bytes a single payload will contain.
	// The magic value 0 will set the option to the optimal size for the transport
	// protocol used when creating the client: 1432 for UDP and 8192 for UDS.
	MaxBytesPerPayload int
	// MaxMessagesPerPayload is the maximum number of metrics, events and/or service checks a single payload will contain.
	// This option can be set to `1` to create an unbuffered client.
	MaxMessagesPerPayload int
	// BufferPoolSize is the size of the pool of buffers in number of buffers.
	// The magic value 0 will set the option to the optimal size for the transport
	// protocol used when creating the client: 2048 for UDP and 512 for UDS.
	BufferPoolSize int
	// BufferFlushInterval is the interval after which the current buffer will get flushed.
	BufferFlushInterval time.Duration
	// BufferShardCount is the number of buffer "shards" that will be used.
	// Those shards allows the use of multiple buffers at the same time to reduce
	// lock contention.
	BufferShardCount int
	// SenderQueueSize is the size of the sender queue in number of buffers.
	// The magic value 0 will set the option to the optimal size for the transport
	// protocol used when creating the client: 2048 for UDP and 512 for UDS.
	SenderQueueSize int
	// WriteTimeoutUDS is the timeout after which a UDS packet is dropped.
	WriteTimeoutUDS time.Duration
	// Telemetry is a set of metrics automatically injected by the client in the
	// dogstatsd stream to be able to monitor the client itself.
	Telemetry bool
	// ReceiveMode determins the behavior of the client when receiving to many
	// metrics. The client will either drop the metrics if its buffers are
	// full (ChannelMode mode) or block the caller until the metric can be
	// handled (MutexMode mode). By default the client will MutexMode. This
	// option should be set to ChannelMode only when use under very high
	// load.
	//
	// MutexMode uses a mutex internally which is much faster than
	// channel but causes some lock contention when used with a high number
	// of threads. Mutex are sharded based on the metrics name which
	// limit mutex contention when goroutines send different metrics.
	//
	// ChannelMode: uses channel (of ChannelModeBufferSize size) to send
	// metrics and drop metrics if the channel is full. Sending metrics in
	// this mode is slower that MutexMode (because of the channel), but
	// will not block the application. This mode is made for application
	// using many goroutines, sending the same metrics at a very high
	// volume. The goal is to not slow down the application at the cost of
	// dropping metrics and having a lower max throughput.
	ReceiveMode ReceivingMode
	// ChannelModeBufferSize is the size of the channel holding incoming metrics
	ChannelModeBufferSize int
	// AggregationFlushInterval is the interval for the aggregator to flush metrics
	AggregationFlushInterval time.Duration
	// [beta] Aggregation enables/disables client side aggregation for
	// Gauges, Counts and Sets (compatible with every Agent's version).
	Aggregation bool
	// [beta] Extended aggregation enables/disables client side aggregation
	// for all types. This feature is only compatible with Agent's versions
	// >=7.25.0 or Agent's version >=6.25.0 && < 7.0.0.
	ExtendedAggregation bool
	// TelemetryAddr specify a different endpoint for telemetry metrics.
	TelemetryAddr string
	// DevMode enables the "dev" mode where the client sends much more
	// telemetry metrics to help troubleshooting the client behavior.
	DevMode bool
}

Options contains the configuration options for a client.

type ReceivingMode

type ReceivingMode int
const (
	MutexMode ReceivingMode = iota
	ChannelMode
)

type SenderMetrics

type SenderMetrics struct {
	TotalSentBytes                uint64
	TotalSentPayloads             uint64
	TotalDroppedPayloads          uint64
	TotalDroppedBytes             uint64
	TotalDroppedPayloadsQueueFull uint64
	TotalDroppedBytesQueueFull    uint64
	TotalDroppedPayloadsWriter    uint64
	TotalDroppedBytesWriter       uint64
}

SenderMetrics contains metrics about the health of the sender

type ServiceCheck

type ServiceCheck struct {
	// Name of the service check.  Required.
	Name string
	// Status of service check.  Required.
	Status ServiceCheckStatus
	// Timestamp is a timestamp for the serviceCheck.  If not provided, the dogstatsd
	// server will set this to the current time.
	Timestamp time.Time
	// Hostname for the serviceCheck.
	Hostname string
	// A message describing the current state of the serviceCheck.
	Message string
	// Tags for the serviceCheck.
	Tags []string
}

A ServiceCheck is an object that contains status of DataDog service check.

func NewServiceCheck

func NewServiceCheck(name string, status ServiceCheckStatus) *ServiceCheck

NewServiceCheck creates a new serviceCheck with the given name and status. Error checking against these values is done at send-time, or upon running sc.Check.

func (ServiceCheck) Check

func (sc ServiceCheck) Check() error

Check verifies that a service check is valid.

func (ServiceCheck) Encode

func (sc ServiceCheck) Encode(tags ...string) (string, error)

Encode returns the dogstatsd wire protocol representation for a service check. Tags may be passed which will be added to the encoded output but not to the Service Check's list of tags, eg. for default tags.

type ServiceCheckStatus

type ServiceCheckStatus byte

ServiceCheckStatus support

const (
	// Ok is the "ok" ServiceCheck status
	Ok ServiceCheckStatus = 0
	// Warn is the "warning" ServiceCheck status
	Warn ServiceCheckStatus = 1
	// Critical is the "critical" ServiceCheck status
	Critical ServiceCheckStatus = 2
	// Unknown is the "unknown" ServiceCheck status
	Unknown ServiceCheckStatus = 3
)

Jump to

Keyboard shortcuts

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