-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (73 loc) · 2.15 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
package main
import (
"context"
"net/http"
"os"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
)
func init() {
flag.BoolP("help", "h", false, "print help and exit")
flag.String("email", "", "litter robot account email address")
flag.String("password", "", "litter robot account password")
flag.String("address", "0.0.0.0:9080", "the server address")
flag.String("log-level", "info", "the log level")
// Replace dashes with underscores in environment variable keys
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
// env vars are prefixed with LR_
viper.SetEnvPrefix("LR")
viper.AutomaticEnv()
flag.Parse()
if err := viper.BindPFlags(flag.CommandLine); err != nil {
log.Fatal(err)
}
// If the uer requests help, print help and exit
if viper.GetBool("help") {
flag.Usage()
os.Exit(0)
}
logLevel := viper.GetString("log-level")
log.SetLevel(parseLevelDefault(logLevel, log.InfoLevel))
}
// attempts to parse a provided string as a LogLevel, and returns the provided default 'def' if parsing fails.
func parseLevelDefault(level string, def log.Level) log.Level {
if l, err := log.ParseLevel(level); err != nil {
return def
} else {
return l
}
}
func main() {
email := viper.GetString("email")
password := viper.GetString("password")
address := viper.GetString("address")
ctx := context.Background()
lrc := NewCollector(ctx, email, password)
ticker := time.NewTicker((3600 * time.Second) - (120 * time.Second))
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
if err := lrc.lrClient.RefreshToken(ctx); err != nil {
log.Error("error refreshing token", "error", err.Error())
}
case <-quit:
ticker.Stop()
return
}
}
}()
prometheus.MustRegister(lrc)
http.Handle("/metrics", promhttp.Handler())
log.Infof("Starting exporter on %s", address)
if err := http.ListenAndServe(address, promhttp.Handler()); err != http.ErrServerClosed {
log.WithField("error", err).Fatal("Starting server failed")
}
close(quit)
}