Documentation ¶
Overview ¶
Package statsd implements is a client for https://github.com/etsy/statsd
Index ¶
- Constants
- Variables
- func UDP(addr string) (io.WriteCloser, error)
- type Client
- func (c *Client) Close() error
- func (c *Client) Count(metricName string, value int) error
- func (c *Client) Flush(n int) error
- func (c *Client) FlushEvery(dur time.Duration)
- func (c *Client) Gauge(metricName string, value int) error
- func (c *Client) Histogram(metricName string, value int) error
- func (c *Client) Increment(metricName string) error
- func (c *Client) IsClosed() bool
- func (c *Client) Record(metricName string, rate float32) func() error
- func (c *Client) SetFormatter(fmt func(metricName string) string)
- func (c *Client) SetMaxPackageSize(maxPacketSize int)
- func (c *Client) Time(metricName string, value time.Duration) error
- func (c *Client) Unique(metricName string, value int) error
- func (c *Client) WriteMetric(metricName, value, typ string, rate float32) error
Constants ¶
const ( // Count is the "c" Counting statsd metric type. Count string = "c" // Gauge is the "g" Gauges statsd metric type. Gauge string = "g" // Unique is the "s" Sets statsd metric type. Unique string = "s" // Set is an alias for `Unique`. Set = Unique // Time is the "ms" Timing statsd metric type. Time string = "ms" // Histogram is the "h" metric type, // difference from `Time` metric type is that `Time` writes milleseconds. // // Read more at: https://docs.netdata.cloud/collectors/statsd.plugin/ Histogram string = "h" )
Types.
Variables ¶
var ( // Duration accepts a duration and returns a string of the duration's millesecond. Duration = func(v time.Duration) string { return Int(int(v / time.Millisecond)) } // Int accepts an int and returns its string form. Int = func(v int) string { return Int64(int64(v)) } // Int8 accepts an int8 and returns its string form. Int8 = func(v int8) string { return Int64(int64(v)) } // Int16 accepts an int16 and returns its string form. Int16 = func(v int16) string { return Int64(int64(v)) } // Int32 accepts an int32 and returns its string form. Int32 = func(v int32) string { return Int64(int64(v)) } // Int64 accepts an int64 and returns its string form. Int64 = func(v int64) string { return strconv.FormatInt(v, 10) } // Uint accepts an uint and returns its string form. Uint = func(v uint) string { return Uint64(uint64(v)) } // Uint8 accepts an uint8 and returns its string form. Uint8 = func(v uint8) string { return Uint64(uint64(v)) } // Uint16 accepts an uint16 and returns its string form. Uint16 = func(v uint16) string { return Uint64(uint64(v)) } // Uint32 accepts an uint32 and returns its string form. Uint32 = func(v uint32) string { return Uint64(uint64(v)) } // Uint64 accepts an uint64 and returns its string form. Uint64 = func(v uint64) string { return strconv.FormatUint(v, 10) } // Float32 accepts a float32 and returns its string form. Float32 = func(v float32) string { return Float64(float64(v)) } // Float64 accepts a float64 and returns its string form. Float64 = func(v float64) string { return strconv.FormatFloat(v, 'f', -1, 64) } )
Values.
Functions ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client implements the StatsD Client. Look more at `WriteMetric`.
func NewClient ¶
func NewClient(writeCloser io.WriteCloser, prefix string) *Client
NewClient returns a new StatsD client. The first input argument, "writeCloser", should be a value which completes the `io.WriteCloser` interface. It can be a UDP connection or a string buffer or even the stdout for testing.
The second input argument, "prefix" can be empty but it is usually the app's name + '.'.
Example: conn, err := UDP(":8125") if err != nil { panic(err) } client := NewClient(conn, "my_prefix.") defer client.Close() client.FlushEvery(4 *time.Second) [...] err := client.WriteMetric("my_metric", Int(1), Count, 0.5) ^ increment by one, sample rate at 0.5
Read more at: https://github.com/etsy/statsd/blob/master/docs/metric_types.md
func (*Client) Close ¶
Close terminates the client, before closing it will try to write any pending metrics.
func (*Client) Count ¶
Count is a shortcut of `Client#WriteMetric(metricName, statsd.Int(value), statsd.Count, 1)`.
func (*Client) Flush ¶
Flush can be called manually, when `FlushEvery` is not configured, to flush the buffered metrics to the statsd server. Negative or zero "n" value will flush everything from the buffer. See `SetMaxPacketSize` too.
func (*Client) FlushEvery ¶
FlushEvery accepts a duration which is used to create a new ticker which will flush the buffered metrics on each tick.
func (*Client) Gauge ¶
Gauge is a shortcut of `Client#WriteMetric(metricName, statsd.Int(value), statsd.Gauge, 1)`.
func (*Client) Histogram ¶ added in v0.0.2
Histogram writes a histogram metric value, difference from `Time` metric type is that `Time` writes milleseconds.
Histogram is a shortcut of `Client#WriteMetric(metricName, value, statsd.Histogram, 1)`.
Read more at: https://docs.netdata.cloud/collectors/statsd.plugin/
func (*Client) Record ¶
Record prepares a Timing metric which records a duration from now until the returned function is executed. For example: stop := client.Record("response.time."+ path, 1) next.ServeHTTP(w, r) stop() // This will write the metric of Timing with value of start time - stop time.
Extremely useful to capture http delays.
func (*Client) SetFormatter ¶
SetFormatter accepts a function which accepts the full metric name and returns a formatted string. Optionally, defaults to nil.
func (*Client) SetMaxPackageSize ¶
SetMaxPackageSize sets the max buffer size, when exceeds it flushes the metrics to the statsd server.
Fast Ethernet (1432) - This is most likely for Intranets. Gigabit Ethernet (8932) - Jumbo frames can make use of this feature much more efficient. Commodity Internet (512) - If you are routing over the internet a value in this range will be reasonable. You might be able to go higher, but you are at the mercy of all the hops in your route.
Read more at: https://github.com/etsy/statsd/blob/master/docs/metric_types.md#multi-metric-packets Defaults to 1500. See `FlushEvery` and `Flush` too.
func (*Client) Time ¶
Time is a shortcut of `Client#WriteMetric(metricName, statsd.Duration(value), statsd.Time, 1)`.
func (*Client) Unique ¶
Unique is a shortcut of `Client#WriteMetric(metricName, statsd.Int(value), statsd.Unique, 1)`.
Sampling rate is not supported on sets.
func (*Client) WriteMetric ¶
WriteMetric writes to the buffer a single metric. When metrics are "big" enough (see `SetMaxPacketSize`) then they will be flushed to the statsd server.
The "metricName" input argument is the metric name (prefix is setted automatically if any).
The "value" input argument is any string value, use the `Int`, `Int8`,`Int16`, `Int32`, `Int64` or `Uint`, `Uint8`, `Uint16`, `Uint32`, `Uint64` or `Float32`, `Float64` or `Duration` value helpers to convert a desired number to a string value. However if you are working on a custom statds server you may want to pass any supported value here.
The "typ" input argument is the type of the statsd, i.e "c"(statsd.Count),"ms"(statsd.Time),"g"(statsd.Gauge) and "s"(`statsd.Unique`)
The "rate" input argument is optional and defaults to 1. Use the `Client#Count`, `Client#Increment`, `Client#Gauge`, `Client#Unique`, `Client#Time`, `Client#Record` and `Client#Histogram` for common metrics instead.