2016-02-13 00:50:37 +00:00
|
|
|
// Copyright 2013 Ooyala, Inc.
|
|
|
|
|
|
|
|
/*
|
|
|
|
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.
|
|
|
|
|
|
|
|
Example Usage:
|
|
|
|
|
|
|
|
// Create the client
|
|
|
|
c, err := statsd.New("127.0.0.1:8125")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
// Prefix every metric with the app name
|
|
|
|
c.Namespace = "flubber."
|
|
|
|
// Send the EC2 availability zone as a tag with every metric
|
|
|
|
c.Tags = append(c.Tags, "us-east-1a")
|
|
|
|
err = c.Gauge("request.duration", 1.2, nil, 1)
|
|
|
|
|
|
|
|
statsd is based on go-statsd-client.
|
|
|
|
*/
|
|
|
|
package statsd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2019-06-19 12:50:48 +00:00
|
|
|
"os"
|
2016-02-13 00:50:37 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-08-09 23:25:51 +00:00
|
|
|
/*
|
2020-07-23 18:37:33 +00:00
|
|
|
OptimalUDPPayloadSize defines the optimal payload size for a UDP datagram, 1432 bytes
|
2016-08-09 23:25:51 +00:00
|
|
|
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.
|
|
|
|
*/
|
2020-07-23 18:37:33 +00:00
|
|
|
const OptimalUDPPayloadSize = 1432
|
2016-08-09 23:25:51 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
const MaxUDPPayloadSize = 65467
|
|
|
|
|
2020-07-23 18:37:33 +00:00
|
|
|
// DefaultUDPBufferPoolSize is the default size of the buffer pool for UDP clients.
|
|
|
|
const DefaultUDPBufferPoolSize = 2048
|
|
|
|
|
|
|
|
// DefaultUDSBufferPoolSize is the default size of the buffer pool for UDS clients.
|
|
|
|
const DefaultUDSBufferPoolSize = 512
|
|
|
|
|
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
const DefaultMaxAgentPayloadSize = 8192
|
|
|
|
|
|
|
|
/*
|
|
|
|
TelemetryInterval is the interval at which telemetry will be sent by the client.
|
|
|
|
*/
|
|
|
|
const TelemetryInterval = 10 * time.Second
|
|
|
|
|
|
|
|
/*
|
|
|
|
clientTelemetryTag is a tag identifying this specific client.
|
|
|
|
*/
|
|
|
|
var clientTelemetryTag = "client:go"
|
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
/*
|
|
|
|
UnixAddressPrefix holds the prefix to use to enable Unix Domain Socket
|
|
|
|
traffic instead of UDP.
|
|
|
|
*/
|
|
|
|
const UnixAddressPrefix = "unix://"
|
|
|
|
|
|
|
|
// Client-side entity ID injection for container tagging
|
|
|
|
const (
|
|
|
|
entityIDEnvName = "DD_ENTITY_ID"
|
|
|
|
entityIDTagName = "dd.internal.entity_id"
|
|
|
|
)
|
|
|
|
|
2020-07-23 18:37:33 +00:00
|
|
|
type metricType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
gauge metricType = iota
|
|
|
|
count
|
|
|
|
histogram
|
|
|
|
distribution
|
|
|
|
set
|
|
|
|
timing
|
|
|
|
event
|
|
|
|
serviceCheck
|
2019-06-19 12:50:48 +00:00
|
|
|
)
|
|
|
|
|
2020-07-23 18:37:33 +00:00
|
|
|
type metric struct {
|
|
|
|
metricType metricType
|
|
|
|
namespace string
|
|
|
|
globalTags []string
|
|
|
|
name string
|
|
|
|
fvalue float64
|
|
|
|
ivalue int64
|
|
|
|
svalue string
|
|
|
|
evalue *Event
|
|
|
|
scvalue *ServiceCheck
|
|
|
|
tags []string
|
|
|
|
rate float64
|
|
|
|
}
|
|
|
|
|
|
|
|
type noClientErr string
|
|
|
|
|
|
|
|
// ErrNoClient is returned if statsd reporting methods are invoked on
|
|
|
|
// a nil client.
|
|
|
|
const ErrNoClient = noClientErr("statsd client is nil")
|
|
|
|
|
|
|
|
func (e noClientErr) Error() string {
|
|
|
|
return string(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 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.
|
2019-06-19 12:50:48 +00:00
|
|
|
Close() error
|
2020-07-23 18:37:33 +00:00
|
|
|
|
|
|
|
// 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
|
2019-06-19 12:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A Client is a handle for sending messages to dogstatsd. It is safe to
|
2016-02-13 00:50:37 +00:00
|
|
|
// use one Client from multiple goroutines simultaneously.
|
|
|
|
type Client struct {
|
2020-07-23 18:37:33 +00:00
|
|
|
// Sender handles the underlying networking protocol
|
|
|
|
sender *sender
|
2016-02-13 00:50:37 +00:00
|
|
|
// Namespace to prepend to all statsd calls
|
|
|
|
Namespace string
|
|
|
|
// Tags are global tags to be added to every statsd call
|
|
|
|
Tags []string
|
2019-06-19 12:50:48 +00:00
|
|
|
// skipErrors turns off error passing and allows UDS to emulate UDP behaviour
|
2020-07-23 18:37:33 +00:00
|
|
|
SkipErrors bool
|
|
|
|
flushTime time.Duration
|
|
|
|
bufferPool *bufferPool
|
|
|
|
buffer *statsdBuffer
|
|
|
|
telemetryTags []string
|
|
|
|
stop chan struct{}
|
2016-02-13 00:50:37 +00:00
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
2020-07-23 18:37:33 +00:00
|
|
|
// Verify that Client implements the ClientInterface.
|
|
|
|
// https://golang.org/doc/faq#guarantee_satisfies_interface
|
|
|
|
var _ ClientInterface = &Client{}
|
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
// New returns a pointer to a new Client given an addr in the format "hostname:port" or
|
|
|
|
// "unix:///path/to/socket".
|
|
|
|
func New(addr string, options ...Option) (*Client, error) {
|
2020-07-23 18:37:33 +00:00
|
|
|
var w statsdWriter
|
2019-06-19 12:50:48 +00:00
|
|
|
o, err := resolveOptions(options)
|
2016-02-13 00:50:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-19 12:50:48 +00:00
|
|
|
|
2020-07-23 18:37:33 +00:00
|
|
|
var writerType string
|
|
|
|
optimalPayloadSize := OptimalUDPPayloadSize
|
|
|
|
defaultBufferPoolSize := DefaultUDPBufferPoolSize
|
2019-06-19 12:50:48 +00:00
|
|
|
if !strings.HasPrefix(addr, UnixAddressPrefix) {
|
|
|
|
w, err = newUDPWriter(addr)
|
2020-07-23 18:37:33 +00:00
|
|
|
writerType = "udp"
|
2019-06-19 12:50:48 +00:00
|
|
|
} else {
|
2020-07-23 18:37:33 +00:00
|
|
|
// FIXME: The agent has a performance pitfall preventing us from using better defaults here.
|
|
|
|
// Once it's fixed, use `DefaultMaxAgentPayloadSize` and `DefaultUDSBufferPoolSize` instead.
|
|
|
|
optimalPayloadSize = OptimalUDPPayloadSize
|
|
|
|
defaultBufferPoolSize = DefaultUDPBufferPoolSize
|
|
|
|
w, err = newUDSWriter(addr[len(UnixAddressPrefix)-1:])
|
|
|
|
writerType = "uds"
|
2019-06-19 12:50:48 +00:00
|
|
|
}
|
2016-02-13 00:50:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-19 12:50:48 +00:00
|
|
|
|
2020-07-23 18:37:33 +00:00
|
|
|
if o.MaxBytesPerPayload == 0 {
|
|
|
|
o.MaxBytesPerPayload = optimalPayloadSize
|
2019-06-19 12:50:48 +00:00
|
|
|
}
|
2020-07-23 18:37:33 +00:00
|
|
|
if o.BufferPoolSize == 0 {
|
|
|
|
o.BufferPoolSize = defaultBufferPoolSize
|
2019-06-19 12:50:48 +00:00
|
|
|
}
|
2020-07-23 18:37:33 +00:00
|
|
|
if o.SenderQueueSize == 0 {
|
|
|
|
o.SenderQueueSize = defaultBufferPoolSize
|
2019-06-19 12:50:48 +00:00
|
|
|
}
|
2020-07-23 18:37:33 +00:00
|
|
|
return newWithWriter(w, o, writerType)
|
2019-06-19 12:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewWithWriter creates a new Client with given writer. Writer is a
|
|
|
|
// io.WriteCloser + SetWriteTimeout(time.Duration) error
|
2020-07-23 18:37:33 +00:00
|
|
|
func NewWithWriter(w statsdWriter, options ...Option) (*Client, error) {
|
|
|
|
o, err := resolveOptions(options)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newWithWriter(w, o, "custom")
|
|
|
|
}
|
|
|
|
|
|
|
|
func newWithWriter(w statsdWriter, o *Options, writerName string) (*Client, error) {
|
|
|
|
|
|
|
|
w.SetWriteTimeout(o.WriteTimeoutUDS)
|
|
|
|
|
|
|
|
c := Client{
|
|
|
|
Namespace: o.Namespace,
|
|
|
|
Tags: o.Tags,
|
|
|
|
telemetryTags: []string{clientTelemetryTag, "transport:" + writerName},
|
|
|
|
}
|
2019-06-19 12:50:48 +00:00
|
|
|
|
|
|
|
// Inject DD_ENTITY_ID as a constant tag if found
|
|
|
|
entityID := os.Getenv(entityIDEnvName)
|
|
|
|
if entityID != "" {
|
|
|
|
entityTag := fmt.Sprintf("%s:%s", entityIDTagName, entityID)
|
2020-07-23 18:37:33 +00:00
|
|
|
c.Tags = append(c.Tags, entityTag)
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.MaxBytesPerPayload == 0 {
|
|
|
|
o.MaxBytesPerPayload = OptimalUDPPayloadSize
|
2019-06-19 12:50:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 18:37:33 +00:00
|
|
|
c.bufferPool = newBufferPool(o.BufferPoolSize, o.MaxBytesPerPayload, o.MaxMessagesPerPayload)
|
|
|
|
c.buffer = c.bufferPool.borrowBuffer()
|
|
|
|
c.sender = newSender(w, o.SenderQueueSize, c.bufferPool)
|
|
|
|
c.flushTime = o.BufferFlushInterval
|
|
|
|
c.stop = make(chan struct{}, 1)
|
|
|
|
go c.watch()
|
|
|
|
go c.telemetry()
|
|
|
|
|
|
|
|
return &c, nil
|
2016-02-13 00:50:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBuffered returns a Client that buffers its output and sends it in chunks.
|
|
|
|
// Buflen is the length of the buffer in number of commands.
|
2019-06-19 12:50:48 +00:00
|
|
|
//
|
|
|
|
// 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.
|
2016-02-13 00:50:37 +00:00
|
|
|
func NewBuffered(addr string, buflen int) (*Client, error) {
|
2020-07-23 18:37:33 +00:00
|
|
|
return New(addr, WithMaxMessagesPerPayload(buflen))
|
2019-06-19 12:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetWriteTimeout allows the user to set a custom UDS write timeout. Not supported for UDP.
|
|
|
|
func (c *Client) SetWriteTimeout(d time.Duration) error {
|
|
|
|
if c == nil {
|
2020-07-23 18:37:33 +00:00
|
|
|
return ErrNoClient
|
2016-02-13 00:50:37 +00:00
|
|
|
}
|
2020-07-23 18:37:33 +00:00
|
|
|
return c.sender.transport.SetWriteTimeout(d)
|
2016-02-13 00:50:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) watch() {
|
2019-06-19 12:50:48 +00:00
|
|
|
ticker := time.NewTicker(c.flushTime)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
c.Lock()
|
2020-07-23 18:37:33 +00:00
|
|
|
c.flushUnsafe()
|
2019-06-19 12:50:48 +00:00
|
|
|
c.Unlock()
|
|
|
|
case <-c.stop:
|
|
|
|
ticker.Stop()
|
2016-02-13 00:50:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 18:37:33 +00:00
|
|
|
func (c *Client) telemetry() {
|
|
|
|
ticker := time.NewTicker(TelemetryInterval)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
metrics := c.sender.flushMetrics()
|
|
|
|
c.telemetryCount("datadog.dogstatsd.client.packets_sent", int64(metrics.TotalSentPayloads), c.telemetryTags, 1)
|
|
|
|
c.telemetryCount("datadog.dogstatsd.client.bytes_sent", int64(metrics.TotalSentBytes), c.telemetryTags, 1)
|
|
|
|
c.telemetryCount("datadog.dogstatsd.client.packets_dropped", int64(metrics.TotalDroppedPayloads), c.telemetryTags, 1)
|
|
|
|
c.telemetryCount("datadog.dogstatsd.client.bytes_dropped", int64(metrics.TotalDroppedBytes), c.telemetryTags, 1)
|
|
|
|
c.telemetryCount("datadog.dogstatsd.client.packets_dropped_queue", int64(metrics.TotalDroppedPayloadsQueueFull), c.telemetryTags, 1)
|
|
|
|
c.telemetryCount("datadog.dogstatsd.client.bytes_dropped_queue", int64(metrics.TotalDroppedBytesQueueFull), c.telemetryTags, 1)
|
|
|
|
c.telemetryCount("datadog.dogstatsd.client.packets_dropped_writer", int64(metrics.TotalDroppedPayloadsWriter), c.telemetryTags, 1)
|
|
|
|
c.telemetryCount("datadog.dogstatsd.client.bytes_dropped_writer", int64(metrics.TotalDroppedBytesWriter), c.telemetryTags, 1)
|
|
|
|
case <-c.stop:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
2016-08-09 23:25:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 18:37:33 +00:00
|
|
|
// same as Count but without global namespace / tags
|
|
|
|
func (c *Client) telemetryCount(name string, value int64, tags []string, rate float64) {
|
|
|
|
c.addMetric(metric{metricType: count, name: name, ivalue: value, tags: tags, rate: rate})
|
2016-08-09 23:25:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 18:37:33 +00:00
|
|
|
// 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
|
2019-06-19 12:50:48 +00:00
|
|
|
func (c *Client) Flush() error {
|
|
|
|
if c == nil {
|
2020-07-23 18:37:33 +00:00
|
|
|
return ErrNoClient
|
2019-06-19 12:50:48 +00:00
|
|
|
}
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2020-07-23 18:37:33 +00:00
|
|
|
c.flushUnsafe()
|
|
|
|
c.sender.flush()
|
|
|
|
return nil
|
2019-06-19 12:50:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 18:37:33 +00:00
|
|
|
// flush the current buffer. Lock must be held by caller.
|
|
|
|
// flushed buffer written to the network asynchronously.
|
|
|
|
func (c *Client) flushUnsafe() {
|
|
|
|
if len(c.buffer.bytes()) > 0 {
|
|
|
|
c.sender.send(c.buffer)
|
|
|
|
c.buffer = c.bufferPool.borrowBuffer()
|
2016-08-09 23:25:51 +00:00
|
|
|
}
|
2016-02-13 00:50:37 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 18:37:33 +00:00
|
|
|
func (c *Client) shouldSample(rate float64) bool {
|
|
|
|
if rate < 1 && rand.Float64() > rate {
|
|
|
|
return true
|
2019-06-19 12:50:48 +00:00
|
|
|
}
|
2020-07-23 18:37:33 +00:00
|
|
|
return false
|
|
|
|
}
|
2019-06-19 12:50:48 +00:00
|
|
|
|
2020-07-23 18:37:33 +00:00
|
|
|
func (c *Client) globalTags() []string {
|
|
|
|
if c != nil {
|
|
|
|
return c.Tags
|
2016-02-13 00:50:37 +00:00
|
|
|
}
|
2020-07-23 18:37:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-06-19 12:50:48 +00:00
|
|
|
|
2020-07-23 18:37:33 +00:00
|
|
|
func (c *Client) namespace() string {
|
|
|
|
if c != nil {
|
|
|
|
return c.Namespace
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) writeMetricUnsafe(m metric) error {
|
|
|
|
switch m.metricType {
|
|
|
|
case gauge:
|
|
|
|
return c.buffer.writeGauge(m.namespace, m.globalTags, m.name, m.fvalue, m.tags, m.rate)
|
|
|
|
case count:
|
|
|
|
return c.buffer.writeCount(m.namespace, m.globalTags, m.name, m.ivalue, m.tags, m.rate)
|
|
|
|
case histogram:
|
|
|
|
return c.buffer.writeHistogram(m.namespace, m.globalTags, m.name, m.fvalue, m.tags, m.rate)
|
|
|
|
case distribution:
|
|
|
|
return c.buffer.writeDistribution(m.namespace, m.globalTags, m.name, m.fvalue, m.tags, m.rate)
|
|
|
|
case set:
|
|
|
|
return c.buffer.writeSet(m.namespace, m.globalTags, m.name, m.svalue, m.tags, m.rate)
|
|
|
|
case timing:
|
|
|
|
return c.buffer.writeTiming(m.namespace, m.globalTags, m.name, m.fvalue, m.tags, m.rate)
|
|
|
|
case event:
|
|
|
|
return c.buffer.writeEvent(*m.evalue, m.globalTags)
|
|
|
|
case serviceCheck:
|
|
|
|
return c.buffer.writeServiceCheck(*m.scvalue, m.globalTags)
|
|
|
|
default:
|
2019-06-19 12:50:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-02-13 00:50:37 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 18:37:33 +00:00
|
|
|
func (c *Client) addMetric(m metric) error {
|
2016-02-13 00:50:37 +00:00
|
|
|
if c == nil {
|
2020-07-23 18:37:33 +00:00
|
|
|
return ErrNoClient
|
2016-02-13 00:50:37 +00:00
|
|
|
}
|
2020-07-23 18:37:33 +00:00
|
|
|
if c.shouldSample(m.rate) {
|
2016-02-13 00:50:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-07-23 18:37:33 +00:00
|
|
|
c.Lock()
|
|
|
|
var err error
|
|
|
|
if err = c.writeMetricUnsafe(m); err == errBufferFull {
|
|
|
|
c.flushUnsafe()
|
|
|
|
err = c.writeMetricUnsafe(m)
|
|
|
|
}
|
|
|
|
c.Unlock()
|
|
|
|
return err
|
2016-02-13 00:50:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Gauge measures the value of a metric at a particular time.
|
|
|
|
func (c *Client) Gauge(name string, value float64, tags []string, rate float64) error {
|
2020-07-23 18:37:33 +00:00
|
|
|
return c.addMetric(metric{namespace: c.namespace(), globalTags: c.globalTags(), metricType: gauge, name: name, fvalue: value, tags: tags, rate: rate})
|
2016-02-13 00:50:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Count tracks how many times something happened per second.
|
|
|
|
func (c *Client) Count(name string, value int64, tags []string, rate float64) error {
|
2020-07-23 18:37:33 +00:00
|
|
|
return c.addMetric(metric{namespace: c.namespace(), globalTags: c.globalTags(), metricType: count, name: name, ivalue: value, tags: tags, rate: rate})
|
2016-02-13 00:50:37 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
// Histogram tracks the statistical distribution of a set of values on each host.
|
2016-02-13 00:50:37 +00:00
|
|
|
func (c *Client) Histogram(name string, value float64, tags []string, rate float64) error {
|
2020-07-23 18:37:33 +00:00
|
|
|
return c.addMetric(metric{namespace: c.namespace(), globalTags: c.globalTags(), metricType: histogram, name: name, fvalue: value, tags: tags, rate: rate})
|
2019-06-19 12:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Distribution tracks the statistical distribution of a set of values across your infrastructure.
|
|
|
|
func (c *Client) Distribution(name string, value float64, tags []string, rate float64) error {
|
2020-07-23 18:37:33 +00:00
|
|
|
return c.addMetric(metric{namespace: c.namespace(), globalTags: c.globalTags(), metricType: distribution, name: name, fvalue: value, tags: tags, rate: rate})
|
2019-06-19 12:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decr is just Count of -1
|
|
|
|
func (c *Client) Decr(name string, tags []string, rate float64) error {
|
2020-07-23 18:37:33 +00:00
|
|
|
return c.Count(name, -1, tags, rate)
|
2019-06-19 12:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Incr is just Count of 1
|
|
|
|
func (c *Client) Incr(name string, tags []string, rate float64) error {
|
2020-07-23 18:37:33 +00:00
|
|
|
return c.Count(name, 1, tags, rate)
|
2016-02-13 00:50:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set counts the number of unique elements in a group.
|
|
|
|
func (c *Client) Set(name string, value string, tags []string, rate float64) error {
|
2020-07-23 18:37:33 +00:00
|
|
|
return c.addMetric(metric{namespace: c.namespace(), globalTags: c.globalTags(), metricType: set, name: name, svalue: value, tags: tags, rate: rate})
|
2019-06-19 12:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Timing sends timing information, it is an alias for TimeInMilliseconds
|
|
|
|
func (c *Client) Timing(name string, value time.Duration, tags []string, rate float64) error {
|
|
|
|
return c.TimeInMilliseconds(name, value.Seconds()*1000, tags, rate)
|
2016-02-13 00:50:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 (c *Client) TimeInMilliseconds(name string, value float64, tags []string, rate float64) error {
|
2020-07-23 18:37:33 +00:00
|
|
|
return c.addMetric(metric{namespace: c.namespace(), globalTags: c.globalTags(), metricType: timing, name: name, fvalue: value, tags: tags, rate: rate})
|
2016-02-13 00:50:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Event sends the provided Event.
|
|
|
|
func (c *Client) Event(e *Event) error {
|
2020-07-23 18:37:33 +00:00
|
|
|
return c.addMetric(metric{globalTags: c.globalTags(), metricType: event, evalue: e, rate: 1})
|
2016-02-13 00:50:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SimpleEvent sends an event with the provided title and text.
|
|
|
|
func (c *Client) SimpleEvent(title, text string) error {
|
|
|
|
e := NewEvent(title, text)
|
|
|
|
return c.Event(e)
|
|
|
|
}
|
|
|
|
|
2019-06-19 12:50:48 +00:00
|
|
|
// ServiceCheck sends the provided ServiceCheck.
|
|
|
|
func (c *Client) ServiceCheck(sc *ServiceCheck) error {
|
2020-07-23 18:37:33 +00:00
|
|
|
return c.addMetric(metric{globalTags: c.globalTags(), metricType: serviceCheck, scvalue: sc, rate: 1})
|
2019-06-19 12:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SimpleServiceCheck sends an serviceCheck with the provided name and status.
|
|
|
|
func (c *Client) SimpleServiceCheck(name string, status ServiceCheckStatus) error {
|
|
|
|
sc := NewServiceCheck(name, status)
|
|
|
|
return c.ServiceCheck(sc)
|
|
|
|
}
|
|
|
|
|
2016-02-13 00:50:37 +00:00
|
|
|
// Close the client connection.
|
|
|
|
func (c *Client) Close() error {
|
|
|
|
if c == nil {
|
2020-07-23 18:37:33 +00:00
|
|
|
return ErrNoClient
|
2016-02-13 00:50:37 +00:00
|
|
|
}
|
2019-06-19 12:50:48 +00:00
|
|
|
select {
|
|
|
|
case c.stop <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
2020-07-23 18:37:33 +00:00
|
|
|
c.Flush()
|
|
|
|
return c.sender.close()
|
2019-06-19 12:50:48 +00:00
|
|
|
}
|