Skip to content

Commit

Permalink
Merge pull request statsd#340 from marpaia/master
Browse files Browse the repository at this point in the history
Cleaning up the golang statsd example
  • Loading branch information
mrtazz committed Sep 8, 2013
2 parents 31ff23e + eae7f6a commit 7c9ba1a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 82 deletions.
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Here's a bunch of example code contributed by the community for interfacing with
statsd.erl - Erlang
statsd-client.sh - Bash
StatsD.scala - Scala
statsd.go - Go

Third Party StatsD Libraries
============================
Expand Down
148 changes: 66 additions & 82 deletions examples/statsd.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package statsd

import (
"net"
"fmt"
"log"
"math/rand"
"net"
"time"
)

Expand All @@ -14,22 +14,18 @@ type StatsdClient struct {
conn net.Conn
}

/**
* Factory method to initialize udp connection
* Usage:
*
* import "statsd"
* client := statsd.New('localhost', 8125)
**/
// Factory method to initialize udp connection
// Usage:
//
// import "statsd"
// client := statsd.New('localhost', 8125)
func New(host string, port int) *StatsdClient {
client := StatsdClient{Host: host, Port: port}
client.Open()
return &client
}

/**
* Method to open udp connection, called by default client factory
**/
// Method to open udp connection, called by default client factory
func (client *StatsdClient) Open() {
connectionString := fmt.Sprintf("%s:%d", client.Host, client.Port)
conn, err := net.Dial("udp", connectionString)
Expand All @@ -39,125 +35,113 @@ func (client *StatsdClient) Open() {
client.conn = conn
}

/**
* Method to close udp connection
**/
// Method to close udp connection
func (client *StatsdClient) Close() {
client.conn.Close()
}

/*
* Log timing information (in milliseconds) without sampling
* Usage:
*
* import "statsd"
* import "time"
* client := statsd.New('localhost', 8125)
* t1 := time.Now()
* expensiveCall()
* t2 := time.Now()
* duration := int64(t2.Sub(t1)/time.Millisecond)
* client.Timing("foo.time", duration)
**/
// Log timing information (in milliseconds) without sampling
// Usage:
//
// import (
// "statsd"
// "time"
// )
//
// client := statsd.New('localhost', 8125)
// t1 := time.Now()
// expensiveCall()
// t2 := time.Now()
// duration := int64(t2.Sub(t1)/time.Millisecond)
// client.Timing("foo.time", duration)
func (client *StatsdClient) Timing(stat string, time int64) {
updateString := fmt.Sprintf("%d|ms", time)
stats := map[string]string{stat: updateString}
client.Send(stats, 1)
}

/**
* Log timing information (in milliseconds) with sampling
* Usage:
*
* import "statsd"
* import "time"
* client := statsd.New('localhost', 8125)
* t1 := time.Now()
* expensiveCall()
* t2 := time.Now()
* duration := int64(t2.Sub(t1)/time.Millisecond)
* client.TimingWithSampleRate("foo.time", duration, 0.2)
**/
// Log timing information (in milliseconds) with sampling
// Usage:
//
// import (
// "statsd"
// "time"
// )
//
// client := statsd.New('localhost', 8125)
// t1 := time.Now()
// expensiveCall()
// t2 := time.Now()
// duration := int64(t2.Sub(t1)/time.Millisecond)
// client.TimingWithSampleRate("foo.time", duration, 0.2)
func (client *StatsdClient) TimingWithSampleRate(stat string, time int64, sampleRate float32) {
updateString := fmt.Sprintf("%d|ms", time)
stats := map[string]string{stat: updateString}
client.Send(stats, sampleRate)
}

/**
* Increments one stat counter without sampling
* Usage:
*
* import "statsd"
* client := statsd.New('localhost', 8125)
* client.Increment('foo.bar')
**/
// Increments one stat counter without sampling
// Usage:
//
// import "statsd"
// client := statsd.New('localhost', 8125)
// client.Increment('foo.bar')
func (client *StatsdClient) Increment(stat string) {
stats := []string{stat}
client.UpdateStats(stats, 1, 1)
}

/**
* Increments one stat counter with sampling
* Usage:
*
* import "statsd"
* client := statsd.New('localhost', 8125)
* client.Increment('foo.bar', 0.2)
**/
// Increments one stat counter with sampling
// Usage:
//
// import "statsd"
// client := statsd.New('localhost', 8125)
// client.Increment('foo.bar', 0.2)
func (client *StatsdClient) IncrementWithSampling(stat string, sampleRate float32) {
stats := []string{stat}
client.UpdateStats(stats[:], 1, sampleRate)
}

/**
* Decrements one stat counter without sampling
* Usage:
*
* import "statsd"
* client := statsd.New('localhost', 8125)
* client.Decrement('foo.bar')
**/
// Decrements one stat counter without sampling
// Usage:
//
// import "statsd"
// client := statsd.New('localhost', 8125)
// client.Decrement('foo.bar')
func (client *StatsdClient) Decrement(stat string) {
stats := []string{stat}
client.UpdateStats(stats[:], -1, 1)
}

/**
* Decrements one stat counter with sampling
* Usage:
*
* import "statsd"
* client := statsd.New('localhost', 8125)
* client.Decrement('foo.bar', 0.2)
**/
// Decrements one stat counter with sampling
// Usage:
//
// import "statsd"
// client := statsd.New('localhost', 8125)
// client.Decrement('foo.bar', 0.2)
func (client *StatsdClient) DecrementWithSampling(stat string, sampleRate float32) {
stats := []string{stat}
client.UpdateStats(stats[:], -1, sampleRate)
}

/**
* Arbitrarily updates a list of stats by a delta
**/
// Arbitrarily updates a list of stats by a delta
func (client *StatsdClient) UpdateStats(stats []string, delta int, sampleRate float32) {
statsToSend := make(map[string]string)
for _,stat := range stats {
for _, stat := range stats {
updateString := fmt.Sprintf("%d|c", delta)
statsToSend[stat] = updateString
}
client.Send(statsToSend, sampleRate)
}

/**
* Sends data to udp statsd daemon
**/
// Sends data to udp statsd daemon
func (client *StatsdClient) Send(data map[string]string, sampleRate float32) {
sampledData := make(map[string]string)
if sampleRate < 1 {
r := rand.New(rand.NewSource(time.Now().Unix()))
rNum := r.Float32();
rNum := r.Float32()
if rNum <= sampleRate {
for stat,value := range data {
for stat, value := range data {
sampledUpdateString := fmt.Sprintf("%s|@%f", value, sampleRate)
sampledData[stat] = sampledUpdateString
}
Expand All @@ -168,7 +152,7 @@ func (client *StatsdClient) Send(data map[string]string, sampleRate float32) {

for k, v := range sampledData {
update_string := fmt.Sprintf("%s:%s", k, v)
_,err := fmt.Fprintf(client.conn, update_string)
_, err := fmt.Fprintf(client.conn, update_string)
if err != nil {
log.Println(err)
}
Expand Down

0 comments on commit 7c9ba1a

Please sign in to comment.