-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmetrics-collector.go
88 lines (78 loc) · 2.14 KB
/
metrics-collector.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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"path"
"time"
"github.com/klingerf/metrics-collector/publisher"
"github.com/klingerf/metrics-collector/sampler"
)
func main() {
defaultSource, err := os.Hostname()
if err != nil {
panic(err)
}
metricsURL := flag.String("metrics-url",
"http://127.0.0.1:9990/admin/metrics.json",
"Address of TwitterServer metrics to collect")
publisherStr := flag.String("publisher", "",
"Stats publisher; supported publishers: datadog, debug")
trimmed := flag.Bool("trimmed", false,
"Reduce metrics before sending them to the publisher")
namespace := flag.String("namespace", "",
"Namespace where metrics are being collected")
source := flag.String("source", defaultSource,
"Common name for entity that is being collected")
service := flag.String("service", "",
"Logical name for service that is being collected")
period := flag.Duration("period", 60*time.Second,
"Polling period")
datadogStatsd := flag.String("datadog-statsd", "127.0.0.1:8125",
"Address of StatsD process used by the datadog publisher")
matchRegexp := flag.String("match", "",
"Regular expression for recording additional metrics.")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [flags]\n", path.Base(os.Args[0]))
flag.PrintDefaults()
}
flag.Parse()
var p publisher.Publisher
switch *publisherStr {
case "":
fmt.Fprintf(os.Stderr, "Publisher is required\n")
flag.Usage()
os.Exit(1)
case "datadog":
p = publisher.NewDatadog(*datadogStatsd, *namespace, *source, *service)
case "debug":
p = publisher.DebugPublisher{}
default:
fmt.Fprintf(os.Stderr, "Unrecognized publisher: %s\n", *publisherStr)
os.Exit(1)
}
exitChan := make(chan os.Signal, 1)
signal.Notify(exitChan, os.Interrupt, os.Kill)
t := time.NewTicker(*period)
s := sampler.NewTwitterServerSampler(*metricsURL, *matchRegexp)
for {
select {
case <-t.C:
sample, err := s.Sample()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
continue
}
if *trimmed {
s.Trim(sample)
}
err = p.Publish(sample)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
case <-exitChan:
os.Exit(0)
}
}
}