-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
70 lines (57 loc) · 1.62 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
package main
import (
"math/rand"
"time"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
// commands
printData = kingpin.Command("print", "print the data to stdout")
remoteWrite = kingpin.Command("remote-write", "Remote write to a Prometheus-compatible TSDB")
emitMetrics = kingpin.Command("emit", "Emit metrics over HTTP on a given port (localhost:<port>/metrics)")
// flags
frequency = kingpin.Flag("frequency", "Frequency of data points").Short('f').Default("4s").Duration()
conf = kingpin.Flag("config", "path to the config file").Short('c').File()
addr = remoteWrite.Flag("addr", "the HTTP address of the TSDB. Should include the basic auth info if required").Required().String()
port = emitMetrics.Flag("port", "HTTP port for emitting metrics").Default("9090").Int()
)
const (
// Version to be overridden at build time
Version = "0.0.1"
)
type metricDumper interface {
dumpMetrics(*timeSeries) error
}
func main() {
kingpin.Version(Version)
kingpin.Parse()
rand.Seed(time.Now().UnixNano())
config := defaultConfig
if *conf != nil {
var err error
config, err = GetConfig(*conf)
if err != nil {
kingpin.Fatalf("error parsing config: %v", err)
}
}
var dumper metricDumper
switch kingpin.Parse() {
case printData.FullCommand():
dumper = &stdout{}
case remoteWrite.FullCommand():
var err error
dumper, err = newRemoteWriter()
if err != nil {
kingpin.Fatalf("error creating remote writer: %v", err)
}
case emitMetrics.FullCommand():
dumper = newHTTPEmitter()
default:
kingpin.Usage()
return
}
for {
dumper.dumpMetrics(generateMetrics(config))
time.Sleep(*frequency)
}
}