-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
84 lines (69 loc) · 1.79 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
package main
import "github.com/segmentio/go-log"
import "github.com/stathat/go"
import "github.com/tj/docopt"
import "strconv"
import "strings"
import "bufio"
import "time"
import "os"
var Version = "0.0.1"
const Usage = `
Usage:
stathat --key s
stathat -h | --help
stathat --version
Options:
-k, --key s EZ key
-h, --help output help information
-v, --version output version
`
func main() {
args, err := docopt.Parse(Usage, nil, true, Version, false)
log.Check(err)
scan(args["--key"].(string))
stathat.WaitUntilFinished(10 * time.Second)
}
// scan stdin.
func scan(key string) {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
if name, value, err := parse(scanner.Text()); err == nil {
send(name, value, key)
}
}
if err := scanner.Err(); err != nil {
log.Error("failed to read stdin: %s", err)
os.Exit(1)
}
}
// parse metric line.
func parse(line string) (name string, value float64, err error) {
parts := strings.Split(line, "|")
name = parts[0]
value, err = strconv.ParseFloat(parts[1], 64)
return
}
// send to stathat.
func send(name string, value float64, key string) {
if strings.HasPrefix(name, "timers.") {
if strings.HasSuffix(name, ".count") || strings.HasSuffix(name, ".sum") {
name = strings.Replace(name, "timers.", "", 1)
log.Info("sending timer %q %f", name, value)
stathat.PostEZValue(name, key, value)
}
return
}
if strings.HasPrefix(name, "gauges.") {
name = strings.Replace(name, "gauges.", "", 1)
log.Info("sending gauge %q %f", name, value)
stathat.PostEZValue(name, key, value)
return
}
if strings.HasPrefix(name, "counts.") {
name = strings.Replace(name, "counts.", "", 1)
log.Info("sending count %q %d", name, int(value))
stathat.PostEZCount(name, key, int(value))
return
}
}