forked from rcrowley/go-metrics
-
Notifications
You must be signed in to change notification settings - Fork 1
/
graphite.go
69 lines (65 loc) · 2.96 KB
/
graphite.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
package metrics
import (
"bufio"
"fmt"
"net"
"time"
)
func sendToGraphite(r Registry, prefix string, addr *net.TCPAddr) {
now := time.Now().Unix()
conn, err := net.DialTCP("tcp", nil, addr)
if nil != err {
return
}
defer conn.Close()
w := bufio.NewWriter(conn)
r.Each(func(name string, i interface{}) {
switch m := i.(type) {
case Counter:
fmt.Fprintf(w, "%s.%s.count %d %d\n", prefix, name, m.Count(), now)
case Gauge:
fmt.Fprintf(w, "%s.%s.value %d %d\n", prefix, name, m.Value(), now)
case Histogram:
ps := m.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
fmt.Fprintf(w, "%s.%s.count %d %d\n", prefix, name, m.Count(), now)
fmt.Fprintf(w, "%s.%s.min %d %d\n", prefix, name, m.Min(), now)
fmt.Fprintf(w, "%s.%s.max %d %d\n", prefix, name, m.Max(), now)
fmt.Fprintf(w, "%s.%s.mean %.2f %d\n", prefix, name, m.Mean(), now)
fmt.Fprintf(w, "%s.%s.std-dev %.2f %d\n", prefix, name, m.StdDev(), now)
fmt.Fprintf(w, "%s.%s.50-percentile %.2f %d\n", prefix, name, ps[0], now)
fmt.Fprintf(w, "%s.%s.75-percentile %.2f %d\n", prefix, name, ps[1], now)
fmt.Fprintf(w, "%s.%s.95-percentile %.2f %d\n", prefix, name, ps[2], now)
fmt.Fprintf(w, "%s.%s.99-percentile %.2f %d\n", prefix, name, ps[3], now)
fmt.Fprintf(w, "%s.%s.999-percentile %.2f %d\n", prefix, name, ps[4], now)
case Meter:
fmt.Fprintf(w, "%s.%s.count %d %d\n", prefix, name, m.Count(), now)
fmt.Fprintf(w, "%s.%s.one-minute %.2f %d\n", prefix, name, m.Rate1(), now)
fmt.Fprintf(w, "%s.%s.five-minute %.2f %d\n", prefix, name, m.Rate5(), now)
fmt.Fprintf(w, "%s.%s.fifteen-minute %.2f %d\n", prefix, name, m.Rate15(), now)
fmt.Fprintf(w, "%s.%s.mean %.2f %d\n", prefix, name, m.RateMean(), now)
case Timer:
ps := m.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
fmt.Fprintf(w, "%s.%s.count %d %d\n", prefix, name, m.Count(), now)
fmt.Fprintf(w, "%s.%s.min %d %d\n", prefix, name, m.Min(), now)
fmt.Fprintf(w, "%s.%s.max %d %d\n", prefix, name, m.Max(), now)
fmt.Fprintf(w, "%s.%s.mean %.2f %d\n", prefix, name, m.Mean(), now)
fmt.Fprintf(w, "%s.%s.std-dev %.2f %d\n", prefix, name, m.StdDev(), now)
fmt.Fprintf(w, "%s.%s.50-percentile %.2f %d\n", prefix, name, ps[0], now)
fmt.Fprintf(w, "%s.%s.75-percentile %.2f %d\n", prefix, name, ps[1], now)
fmt.Fprintf(w, "%s.%s.95-percentile %.2f %d\n", prefix, name, ps[2], now)
fmt.Fprintf(w, "%s.%s.99-percentile %.2f %d\n", prefix, name, ps[3], now)
fmt.Fprintf(w, "%s.%s.999-percentile %.2f %d\n", prefix, name, ps[4], now)
fmt.Fprintf(w, "%s.%s.one-minute %.2f %d\n", prefix, name, m.Rate1(), now)
fmt.Fprintf(w, "%s.%s.five-minute %.2f %d\n", prefix, name, m.Rate5(), now)
fmt.Fprintf(w, "%s.%s.fifteen-minute %.2f %d\n", prefix, name, m.Rate15(), now)
fmt.Fprintf(w, "%s.%s.mean %.2f %d\n", prefix, name, m.RateMean(), now)
}
w.Flush()
})
}
func Graphite(r Registry, d time.Duration, prefix string, addr *net.TCPAddr) {
for {
sendToGraphite(r, prefix, addr)
time.Sleep(d)
}
}