-
Notifications
You must be signed in to change notification settings - Fork 12
/
proxy.go
63 lines (47 loc) · 931 Bytes
/
proxy.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
package main
import (
"fmt"
"log"
"net"
)
func submitProxy() (err error) {
msg, num := buildPayload()
if num == 0 {
return
}
conn, err := net.Dial("tcp", *proxy)
if err != nil {
return
}
defer conn.Close()
n, err := conn.Write(msg)
if err != nil {
return
}
if n != len(msg) {
return fmt.Errorf("wrote %d of %d bytes", n, len(msg))
}
log.Printf("%d measurements sent to proxy\n", num)
resetAll()
return
}
func buildPayload() ([]byte, int) {
result := ""
for k, v := range counters {
result += buildMetric(k, "c", v)
}
for k, v := range gauges {
result += buildMetric(k, "g", v)
}
n := len(counters) + len(gauges)
for k, vs := range timers {
n += len(vs)
for _, v := range vs {
result += buildMetric(k, "ms", v)
}
}
return []byte(result), n
}
func buildMetric(name string, bucket string, value float64) string {
return fmt.Sprintf("%s:%f|%s\n", name, value, bucket)
}