-
Notifications
You must be signed in to change notification settings - Fork 1
/
dogstatsd.go
124 lines (101 loc) · 2.81 KB
/
dogstatsd.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
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
package metrics
import (
"log"
"time"
dogstatsd "github.com/DataDog/datadog-go/statsd"
metrics "github.com/rcrowley/go-metrics"
)
type DogStatsD struct {
statsd *dogstatsd.Client
}
func NewDogStatsD(env string) (*DogStatsD, error) {
// 127.0.0.1:8125 is the URI for dogstastd process running in our servers
c, err := dogstatsd.New("127.0.0.1:8125")
if err != nil {
return nil, err
}
// Prefix every metric with the env name
if env != "" {
env = env + "."
}
c.Namespace = env + "koding.monitoring."
return &DogStatsD{
statsd: c,
}, nil
}
func (s *DogStatsD) Close() error {
if s.statsd == nil {
return nil
}
return s.statsd.Close()
}
// Gauge measure the value of a metric at a particular time
func (s *DogStatsD) Gauge(name string, value float64, tags []string, rate float64) error {
if s.statsd == nil {
return nil
}
return s.statsd.Gauge(name, value, tags, rate)
}
// Count track how many times something happened per second
func (s *DogStatsD) Count(name string, value int64, tags []string, rate float64) error {
if s.statsd == nil {
return nil
}
return s.statsd.Count(name, value, tags, rate)
}
// Timing track how long something happened.
func (s *DogStatsD) Timing(name string, value time.Duration, tags []string, rate float64) error {
if s.statsd == nil {
return nil
}
return s.statsd.Timing(name, value, tags, rate)
}
// Histogram track the statistical distribution of a set of values
func (s *DogStatsD) Histogram(name string, value float64, tags []string, rate float64) error {
if s.statsd == nil {
return nil
}
return s.statsd.Histogram(name, value, tags, rate)
}
// Sets count the number of unique elements in a group
func (s *DogStatsD) Set(name string, value string, tags []string, rate float64) error {
if s.statsd == nil {
return nil
}
return s.statsd.Set(name, value, tags, rate)
}
func Collect(r metrics.Registry, s *DogStatsD, d time.Duration) {
for _ = range time.Tick(d) {
if err := sh(r, s); nil != err {
log.Println(err)
}
}
}
func sh(r metrics.Registry, s *DogStatsD) error {
r.Each(func(name string, i interface{}) {
process := func(i int64) float64 {
if i == 0 {
i = 1
}
return float64(i)
}
switch metric := i.(type) {
case metrics.Counter:
s.Gauge(name+".gauge", process(metric.Count()), nil, 1.0)
case metrics.Gauge:
s.Gauge(name+".gauge", process(metric.Value()), nil, 1.0)
case metrics.Histogram:
h := metric.Snapshot()
s.Gauge(name+".count.gauge", process(h.Count()), nil, 1.0)
case metrics.Meter:
m := metric.Snapshot()
s.Gauge(name+".count.gauge", process(m.Count()), nil, 1.0)
case metrics.Timer:
t := metric.Snapshot()
s.Gauge(name+".mean.timer", t.Mean(), nil, 1.0)
s.Gauge(name+".max.timer", process(t.Max()), nil, 1.0)
s.Gauge(name+".min.timer", process(t.Min()), nil, 1.0)
}
})
return nil
}