-
Notifications
You must be signed in to change notification settings - Fork 0
/
statsd.go
50 lines (41 loc) · 1.16 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
package main
import (
"fmt"
"io"
"net"
"strings"
"time"
)
var queue = make(chan string, 100)
func Increment(metric string, tags []string) {
Count(metric, 1, tags)
}
func Count(metric string, value int, tags []string) {
queue <- fmt.Sprintf("%s:%d|c|#%s", metric, value, formatTags(tags))
}
func Timer(metric string, took time.Duration, tags []string) {
queue <- fmt.Sprintf("%s:%d|ms|#%s", metric, took/1e6, formatTags(tags))
}
func Gauge(metric string, value int, tags []string) {
queue <- fmt.Sprintf("%s:%d|g|#%s", metric, value, formatTags(tags))
}
func formatTags(tags []string) string {
return strings.Join(tags[:], ",")
}
func EscapeTag(s string) string {
// Replace all special characters used in the statsd wire protocol
s = strings.Replace(s, ":", "-", -1)
s = strings.Replace(s, "|", "-", -1)
s = strings.Replace(s, ",", "-", -1)
s = strings.Replace(s, "@", "-", -1)
return s
}
func StatsdSender(config *Config) {
for s := range queue {
statsdHostPort := fmt.Sprintf("%s:%d", config.StatsdHost, config.StatsdPort)
if conn, err := net.Dial(config.StatsdProtocol, statsdHostPort); err == nil {
io.WriteString(conn, s)
conn.Close()
}
}
}