-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (54 loc) · 1.85 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
package main
import (
. "aiven-metadata-prometheus-exporter/internal/pkg"
"flag"
"github.com/aiven/aiven-go-client"
"github.com/go-co-op/gocron"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"net/http"
"os"
"time"
)
func main() {
flag.Parse()
setupLogging()
aivenToken, set := os.LookupEnv("AIVEN_API_TOKEN")
if set == false {
log.Fatal("No Aiven token found in environment. Please export the token as 'AIVEN_API_TOKEN' environment variable")
}
aivenClient, err := aiven.NewTokenClient(aivenToken, "")
if err != nil {
log.Fatalln(err)
}
log.Infoln("Starting Aiven Metadata Prometheus Exporter")
collector := AivenCollector.Init(AivenCollector{}, aivenClient)
r := prometheus.NewRegistry()
r.MustRegister(collector)
scheduler := gocron.NewScheduler(time.UTC)
_, err = scheduler.Every(*interval).Do(func() { collector.CollectScheduled() })
if err != nil {
log.Fatalln(err)
}
log.Infoln("Scheduler set up. Metrics will be refreshed every", *interval)
scheduler.StartAsync()
log.Infoln("Listening on port", *listenAddress, "and", *metricsPath)
http.Handle(*metricsPath, promhttp.HandlerFor(r, promhttp.HandlerOpts{}))
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}
func setupLogging() {
log.SetFormatter(&log.JSONFormatter{})
if *debugEnabled {
log.SetLevel(log.DebugLevel)
log.Debug("Debugging enabled")
} else {
log.SetLevel(log.InfoLevel)
}
}
var (
debugEnabled = flag.Bool("debug", false, "Enable debug logging")
interval = flag.String("collect-interval", "5m", "In which interval shall the metrics collected from the Aiven API")
listenAddress = flag.String("listen-address", ":2112", "Address to listen on for telemetry")
metricsPath = flag.String("telemetry-path", "/metrics", "Path under which to expose metrics")
)