-
Notifications
You must be signed in to change notification settings - Fork 34
/
runstats.go
70 lines (57 loc) · 1.65 KB
/
runstats.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
package runstats
import (
"flag"
"fmt"
"os"
"runtime"
"strconv"
"time"
"github.com/bmhatfield/go-runtime-metrics/collector"
"github.com/peterbourgon/g2s"
)
var (
statsd *string = flag.String("statsd", "localhost:8125", "Statsd host:port pair")
prefix *string = flag.String("metric-prefix", "<detect-hostname>", "Metric prefix path; detects the local hostname by default")
pause *int = flag.Int("pause", 10, "Collection pause interval")
publish *bool = flag.Bool("publish-runtime-stats", true, "Collect go runtime statistics")
cpu *bool = flag.Bool("cpu", true, "Collect CPU Statistics")
mem *bool = flag.Bool("mem", true, "Collect Memory Statistics")
gc *bool = flag.Bool("gc", true, "Collect GC Statistics (requires Memory be enabled)")
s g2s.Statter
)
func init() {
go runCollector()
}
func runCollector() {
for !flag.Parsed() {
// Defer execution of this goroutine.
runtime.Gosched()
// Add an initial delay while the program initializes to avoid attempting to collect
// metrics prior to our flags being available / parsed.
time.Sleep(1 * time.Second)
}
s, err := g2s.Dial("udp", *statsd)
if err != nil {
panic(fmt.Sprintf("Unable to connect to Statsd on %s - %s", *statsd, err))
}
if *prefix == "<detect-hostname>" {
hn, err := os.Hostname()
if err != nil {
*prefix = "go.unknown"
} else {
*prefix = "go." + hn
}
}
*prefix += "."
gaugeFunc := func(key string, val uint64) {
s.Gauge(1.0, *prefix+key, strconv.FormatUint(val, 10))
}
c := collector.New(gaugeFunc)
c.PauseDur = time.Duration(*pause) * time.Second
c.EnableCPU = *cpu
c.EnableMem = *mem
c.EnableGC = *gc
if *publish {
c.Run()
}
}