Golang Example 04.28.16

github.com/alexcesaro/statsd — An efficient Statsd Go client.

Godoc Reference: https://godoc.org/gopkg.in/alexcesaro/statsd.v2

Features:

  • Supports all StatsD metrics: counter, gauge, timing and set
  • Supports InfluxDB and Datadog tags
  • Fast and GC-friendly: all functions for sending metrics do not allocate
  • Efficient: metrics are buffered by default
  • Simple and clean API
  • 100% test coverage
  • Versioned API using gopkg.in
c, err := statsd.New() // Connect to the UDP port 8125 by default.
if err != nil {
// If nothing is listening on the target port, an error is returned and
// the returned client does nothing but is still usable. So we can
// just log the error and go on.
log.Print(err)
}
defer c.Close()

// Increment a counter.
c.Increment("foo.counter")

// Gauge something.
c.Gauge("num_goroutine", runtime.NumGoroutine())

// Time something.
t := c.NewTiming()
ping("http://example.com/")
t.Send("homepage.response_time")

// It can also be used as a one-liner to easily time a function.
pingHomepage := func() {
defer c.NewTiming().Send("homepage.response_time")

ping("http://example.com/")
}
pingHomepage()

// Cloning a Client allows using different parameters while still using the
// same connection.
// This is way cheaper and more efficient than using New().
stat := c.Clone(statsd.Prefix("http"), statsd.SampleRate(0.2))
stat.Increment("view") // Increments http.view