forked from QubitProducts/bamboo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statsd.go
54 lines (45 loc) · 1.14 KB
/
statsd.go
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
package configuration
import (
"github.com/QubitProducts/bamboo/Godeps/_workspace/src/github.com/peterbourgon/g2s"
"log"
"strings"
"time"
)
type StatsD struct {
Enabled bool
Host string
Prefix string
Client g2s.Statter
}
func (s *StatsD) CreateClient() {
if s.Enabled && s.Client == nil {
log.Println("StatsD is enabled")
client, err := g2s.Dial("udp", s.Host)
if err != nil {
log.Fatalf("Cannot connect to statsd server %v: %v ", s.Host, err)
}
s.Client = client
}
}
func (s *StatsD) Increment(sampleRate float32, bucket string, n int) {
if s.Client != nil {
s.Client.Counter(sampleRate, fullBucket(s.Prefix, bucket), n)
}
}
func (s *StatsD) Timing(sampleRate float32, bucket string, d time.Duration) {
if s.Client != nil {
s.Client.Timing(sampleRate, fullBucket(s.Prefix, bucket), d)
}
}
func (s *StatsD) Gauge(sampleRate float32, bucket string, value string) {
if s.Client != nil {
s.Client.Gauge(sampleRate, fullBucket(s.Prefix, bucket), value)
}
}
func fullBucket(prefix string, bucket string) string {
if strings.HasSuffix(prefix, ".") {
return prefix + bucket
} else {
return prefix + "." + bucket
}
}