-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
112 lines (91 loc) · 2.54 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"time"
)
const VERSION = "1.0.0"
var (
address = flag.String("address", "0.0.0.0:8125", "udp listen address")
libratoUser = flag.String("user", "", "librato api username (LIBRATO_USER)")
libratoToken = flag.String("token", "", "librato api token (LIBRATO_TOKEN)")
libratoSource = flag.String("source", "", "librato api source (LIBRATO_SOURCE)")
interval = flag.Int64("flush", 60, "interval at which data is sent to librato (in seconds)")
percentiles = flag.String("percentiles", "", "comma separated list of percentiles to calculate for timers (eg. \"95,99.5\")")
proxy = flag.String("proxy", "", "send metrics to a proxy rather than directly to librato")
debug = flag.Bool("debug", false, "enable logging of inputs and submissions")
version = flag.Bool("version", false, "print version and exit")
)
func monitor() {
var err error
t := time.NewTicker(time.Duration(*interval) * time.Second)
for {
select {
case <-t.C:
if *proxy != "" {
if err = submitProxy(); err != nil {
log.Printf("unable to submit to proxy at %s: %s\n", *proxy, err)
}
} else {
if err := submitLibrato(); err != nil {
log.Printf("unable to submit measurements: %s\n", err)
}
}
case p := <-packets:
readPacket(p)
}
}
}
func main() {
flag.Parse()
if *version {
fmt.Printf("statsd-librato v%s\n", VERSION)
return
}
if *proxy == "" {
getEnv(proxy, "PROXY")
}
if *proxy != "" {
log.Printf("sending metrics to proxy at %s\n", *proxy)
} else {
if *libratoUser == "" {
if !getEnv(libratoUser, "LIBRATO_USER") {
log.Fatal("specify a librato user with -user or the LIBRATO_USER environment variable")
}
}
if *libratoToken == "" {
if !getEnv(libratoToken, "LIBRATO_TOKEN") {
log.Fatal("specify a librato token with -token or the LIBRATO_TOKEN environment variable")
}
}
if *libratoSource == "" {
getEnv(libratoSource, "LIBRATO_SOURCE")
}
if *percentiles == "" {
getEnv(percentiles, "PERCENTILES")
}
if *percentiles != "" {
for _, s := range strings.Split(*percentiles, ",") {
if f := parseFloat(s); f > 0.0 && f < 100.0 {
tiles = append(tiles, f)
log.Printf("including percentile %f for timers\n", f)
}
}
}
log.Printf("sending metrics to librato\n")
}
log.Printf("flushing metrics every %d seconds\n", *interval)
go listenUdp()
go listenTcp()
monitor()
}
func getEnv(p *string, key string) bool {
if s := os.Getenv(key); s != "" {
*p = s
return true
}
return false
}