forked from buildkite/buildkite-agent-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
127 lines (108 loc) · 3.34 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"time"
"github.com/buildkite/buildkite-agent-metrics/backend"
"github.com/buildkite/buildkite-agent-metrics/collector"
"github.com/buildkite/buildkite-agent-metrics/version"
)
var bk backend.Backend
func main() {
var (
token = flag.String("token", "", "A Buildkite Agent Registration Token")
interval = flag.Duration("interval", 0, "Update metrics every interval, rather than once")
showVersion = flag.Bool("version", false, "Show the version")
quiet = flag.Bool("quiet", false, "Only print errors")
debug = flag.Bool("debug", false, "Show debug output")
debugHttp = flag.Bool("debug-http", false, "Show full http traces")
dryRun = flag.Bool("dry-run", false, "Whether to only print metrics")
endpoint = flag.String("endpoint", "https://agent.buildkite.com/v3", "A custom Buildkite Agent API endpoint")
// backend config
backendOpt = flag.String("backend", "cloudwatch", "Specify the backend to use: cloudwatch, statsd, prometheus")
statsdHost = flag.String("statsd-host", "127.0.0.1:8125", "Specify the StatsD server")
statsdTags = flag.Bool("statsd-tags", false, "Whether your StatsD server supports tagging like Datadog")
prometheusAddr = flag.String("prometheus-addr", ":8080", "Prometheus metrics transport bind address")
prometheusPath = flag.String("prometheus-path", "/metrics", "Prometheus metrics transport path")
clwDimensions = flag.String("cloudwatch-dimensions", "", "Cloudwatch dimensions to index metrics under, in the form of Key=Value, Other=Value")
// filters
queue = flag.String("queue", "", "Only include a specific queue")
)
flag.Parse()
if *showVersion {
fmt.Printf("buildkite-agent-metrics %s\n", version.Version)
os.Exit(0)
}
if *token == "" {
fmt.Println("Must provide a value for -token")
os.Exit(1)
}
switch strings.ToLower(*backendOpt) {
case "cloudwatch":
dimensions, err := backend.ParseCloudWatchDimensions(*clwDimensions)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
bk = backend.NewCloudWatchBackend(dimensions)
case "statsd":
var err error
bk, err = backend.NewStatsDBackend(*statsdHost, *statsdTags)
if err != nil {
fmt.Printf("Error starting StatsD, err: %v\n", err)
os.Exit(1)
}
case "prometheus":
bk = backend.NewPrometheusBackend(*prometheusPath, *prometheusAddr)
default:
fmt.Println("Must provide a supported backend: cloudwatch, statsd, prometheus")
os.Exit(1)
}
if *quiet {
log.SetOutput(ioutil.Discard)
}
userAgent := fmt.Sprintf("buildkite-agent-metrics/%s buildkite-agent-metrics-cli", version.Version)
if *interval > 0 {
userAgent += fmt.Sprintf(" interval=%s", *interval)
}
c := collector.Collector{
UserAgent: userAgent,
Endpoint: *endpoint,
Token: *token,
Queue: *queue,
Quiet: *quiet,
Debug: *debug,
DebugHttp: *debugHttp,
}
f := func() error {
t := time.Now()
result, err := c.Collect()
if err != nil {
return err
}
if !*dryRun {
err = bk.Collect(result)
if err != nil {
return err
}
}
log.Printf("Finished in %s", time.Now().Sub(t))
return nil
}
if err := f(); err != nil {
fmt.Println(err)
os.Exit(1)
}
if *interval > 0 {
for _ = range time.NewTicker(*interval).C {
if err := f(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}
}