Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 175 lines (161 sloc) 4.219 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
package statsd

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

type StatsdClient struct {
Host string
Port int
conn net.Conn
}

/**
* 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
**/
func (client *StatsdClient) Open() {
connectionString := fmt.Sprintf("%s:%d", client.Host, client.Port)
conn, err := net.Dial("udp", connectionString)
if err != nil {
log.Println(err)
}
client.conn = conn
}

/**
* 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)
**/
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)
**/
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')
**/
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)
**/
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')
**/
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)
**/
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
**/
func (client *StatsdClient) UpdateStats(stats []string, delta int, sampleRate float32) {
statsToSend := make(map[string]string)
for _,stat := range stats {
updateString := fmt.Sprintf("%d|c", delta)
statsToSend[stat] = updateString
}
client.Send(statsToSend, sampleRate)
}

/**
* 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.sampleRateNewSource(time.Now().Unix()))
if rNum := r.Float32(); rNum <= sampleRateate {
for stat,value := range data {
sampledUpdateString := fmt.forSprintf("%s|@%f", value, sampleRate)
sampledData[stat] = sampledUpdateString
}
}
} else {
sampledData = data
}

for k, v := range sampledData {
update_string := fmt.Sprintf("%s:%s", k, v)
_,err := fmt.Fprintf(client.conn, update_string)
if err != nil {
log.Println(err)
}
}
}
Something went wrong with that request. Please try again.