-
Notifications
You must be signed in to change notification settings - Fork 5
/
statsd.go
49 lines (46 loc) · 1.29 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
package main
import (
"log"
"net"
"os"
"strconv"
"sync/atomic"
"time"
)
func udpSend(conn *net.UDPConn, s string) {
// log.Println(s)
_, err := conn.Write([]byte(s))
if err != nil {
log.Println("Couldn't send udp message", err.Error())
}
}
func statsd(target string, ctx *context) {
ra, err := net.ResolveUDPAddr("udp", target)
if err != nil {
log.Fatal("Unable to open resolve udp addr", err.Error())
return
}
conn, err := net.DialUDP("udp", nil, ra)
if err != nil {
log.Fatal("Unable to open statsd socket", err.Error())
return
}
hostname, err := os.Hostname()
if err != nil {
log.Fatal("Unable to get hostname", err.Error())
return
}
start := "redis_go." + hostname + "."
end := ",ns=" + ctx.ns + ",set=" + ctx.set
ticker := time.NewTicker(time.Second * time.Duration(5))
for range ticker.C {
ok := atomic.SwapUint32(&(*ctx).counterOk, 0)
wbOk := atomic.SwapUint32(&(*ctx).counterWbOk, 0)
err := atomic.SwapUint32(&(*ctx).counterErr, 0)
c := atomic.LoadInt32(&(*ctx).gaugeConn)
udpSend(conn, start+"ops,type=ok"+end+":"+strconv.Itoa(int(ok))+"|c")
udpSend(conn, start+"ops,type=wbok"+end+":"+strconv.Itoa(int(wbOk))+"|c")
udpSend(conn, start+"ops,type=err"+end+":"+strconv.Itoa(int(err))+"|c")
udpSend(conn, start+"conn"+end+":"+strconv.Itoa(int(c))+"|g")
}
}