Version: v0.0.2 Latest Latest

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

Go to latest
Published: May 13, 2014 License: BSD-2-Clause, BSD-2-Clause Imports: 6 Imported by: 0



Get to Statsd: forward simple statistics to a statsd server.

Build Status


g2s provides a Statsd object, which provides some convenience functions for each of the supported statsd statistic-types. Just call the relevant function on the Statsd object wherever it makes sense in your code.

s, err := g2s.Dial("udp", "statsd-server:8125")
if err != nil {
	// do something

s.Counter(1.0, "my.silly.counter", 1)
s.Timing(1.0, "my.silly.slow-process", time.Since(somethingBegan))
s.Timing(0.2, "my.silly.fast-process", 7*time.Millisecond)
s.Gauge(1.0, "my.silly.status", "green")

If you use a standard UDP connection to a statsd server, all 'update'-class functions are goroutine safe. They should return quickly, but they're safe to fire in a seperate goroutine.

Upgrading API

Upgrade to the latest API by running ./fix.bash *.go where *.go expands to the paths of the source files you'd like to rewrite to the new API.




View Source
const (
	MAX_PACKET_SIZE = 65536 - 8 - 20 // 8-byte UDP header, 20-byte IP header


This section is empty.


This section is empty.


type Statter

type Statter interface {
	Counter(sampleRate float32, bucket string, n ...int)
	Timing(sampleRate float32, bucket string, d ...time.Duration)
	Gauge(sampleRate float32, bucket string, value ...string)

func Dial

func Dial(proto, endpoint string) (Statter, error)

Dial takes the same parameters as net.Dial, ie. a transport protocol (typically "udp") and an endpoint. It returns a new Statsd structure, ready to use.

Note that g2s currently performs no management on the connection it creates.

func New

func New(w io.Writer) (Statter, error)

New constructs a Statsd structure which will write statsd-protocol messages into the given io.Writer. New is intended to be used by consumers who want nonstandard behavior: for example, they may pass an io.Writer which performs buffering and aggregation of statsd-protocol messages.

Note that g2s provides no synchronization. If you pass an io.Writer which is not goroutine-safe, for example a bytes.Buffer, you must make sure you synchronize your calls to the Statter methods.

func Noop

func Noop() Statter

Noop returns a struct that satisfies the Statter interface but silently ignores all Statter method invocations. It's designed to be used when normal g2s construction fails, eg.

s, err := g2s.Dial("udp", someEndpoint)
if err != nil {
    log.Printf("not sending statistics to statsd (%s)", err)
    s = g2s.Noop()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL