statsd

package module
v0.0.0-...-bcf29aa Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2015 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

gostatsd

gostatsd is a Statsd client package for Go. It supports all commands supported by the etsy/statsd project and automatically buffers stats into 512 byte UDP packets. You can customize the packet size, as well.

gostatsd is goroutine safe.

API documentation

Usage

gostatsd, by default, buffers up to 512 bytes of data before sending a UDP packet. This means that you need to manually call Flush() after you're done recording your stats to send any remaining stats.

Basic usage is easy: just give gostatsd a URL and a packet size, and start sending metrics:

statsd.Setup("statsd://127.0.0.1:8125/prefix.here", 512)
defer statsd.Flush()

// Counters
for i := 0; i < 10000; i++ {
  if math.Mod(i, 100) == 0 {
    statsd.Count("transactions", 1, 0.100)
  }
}

// Gauges
statsd.Gauge("queuesize", 28)

// Timers
start := time.Now()
statsd.Timing("methodtime", float64(time.Since(start))/float64(time.Milliseconds))
// or
statsd.TimingDuration("methodtime", time.Since(start))

If you need to send metrics to different places or want to use different metric prefixes, you can create a standalone Client:

client := statsd.New("statsd://127.0.0.1:8125/my.prefix")
defer client.Flush()

// Counters
for i := 0; i < 10000; i++ {
  if math.Mod(i, 100) == 0 {
    client.Count("transactions", 1, 0.100)
  }
}

// Gauges
client.Gauge("queuesize", 28)

// Timers
start := time.Now()
client.TimingDuration("methodtime", time.Since(start))

The buffer size (in bytes) can be customized:

client := NewWithPacketSize("statsd://127.0.0.1:8125/my.prefix.", 128)
Unbuffered sending

A buffer size of 0 or less will cause all stats to be sent individually as soon as they are received. If you're using unbuffered sending, you wont need to call Flush()

statsd.Setup("127.0.0.1:8125/my.prefix", -1)
// or
client := statsd.NewWithPacketSize("127.0.0.1:8125", "my.prefix.", -1)

Benchmarks

December 17th, 2013, Go 1.2 darwin/amd64

% go test -bench . --benchmem
BenchmarkGaugeNoPrefix             1113 ns/op   80 B/op   4 allocs/op
BenchmarkGaugeWithPrefix           1326 ns/op   80 B/op   4 allocs/op
BenchmarkGaugeNoBuffer             6984 ns/op   80 B/op   4 allocs/op
BenchmarkGaugeWithPrefixNoBuffer   6910 ns/op   80 B/op   4 allocs/op

Documentation

Overview

Package statsd implements a Statsd client. It supports all commands supported by the Etsy statsd server implementation and automatically buffers stats into 512 byte packets.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Count

func Count(bucket string, value float64, sampleRate float64)

Count sends a count metric using the global Statsd client.

func CountUnique

func CountUnique(bucket string, value string)

CountUnique sends a unique count metric using the global Statsd client.

func Flush

func Flush()

Flush flushes the buffer on the global Statsd client.

func Gauge

func Gauge(bucket string, value float64)

Gauge sends a gauge metric using the global Statsd client.

func Join

func Join(parts ...string) (name string)

Join takes a slice of strings (a metric "path") and returns a valid Statsd metric name. It cleans all parts by slug-ifying them.

func Setup

func Setup(statsdURL string, packetSize int) (err error)

Setup enables and configures the global Statsd client.

func Timing

func Timing(bucket string, value float64)

Timing sends a raw timing metric using the global Statsd client.

func TimingDuration

func TimingDuration(bucket string, duration time.Duration)

TimingDuration sends a timing metric as a time.Duration using the global Statsd client.

Types

type Client

type Client interface {
	Flush() error
	Count(bucket string, value float64, sampleRate float64)
	Gauge(bucket string, value float64)
	Timing(bucket string, value float64)
	TimingDuration(bucket string, duration time.Duration)
	CountUnique(bucket string, value string)
}

Client is a buffered Statsd client.

func New

func New(statsdURL string) (Client, error)

New is the same as calling NewWithPacketSize with a 512 byte packet size.

func NewWithPacketSize

func NewWithPacketSize(statsdURL string, packetSize int) (Client, error)

NewWithPacketSize returns a new Client that will send stats to a Statsd server. If the given URL has a path component (eg. "/my.prefix"), all metric names will be prepended with that prefix.

The packet size parameter is the maximum size (in bytes) that will be buffered before being sent. A value of 0 or less will cause each stat to be sent immediately, as it is received.

If the Statsd URL is invalid, a no-op Client will be returned along with an error so that your code can ignore the error, if desired.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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