-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
86 lines (74 loc) · 2.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"flag"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
const (
defaultFahAddress = "127.0.0.1:36330"
)
var (
fahAddress = defaultFahAddress
getAPI = false
apiThrottle time.Duration
myClient = &http.Client{Timeout: 10 * time.Second}
)
func main() {
var (
level string
listenAddress string
metricsPath string
socketActivate bool
noTimestamps bool
defaultThrottle, _ = time.ParseDuration("1h")
)
flag.StringVar(&level, "log.level", "info", "Set the output log level")
flag.BoolVar(&noTimestamps, "log.no-timestamps", false, "Disable logging timestamps, true when using systemd activation")
flag.StringVar(&listenAddress, "web.listen-address", "0.0.0.0:9659", "Address to listen on for web interface and telemetry.")
flag.StringVar(&metricsPath, "web.telemetry-path", "/metrics", "Path under which to expose metrics.")
flag.BoolVar(&socketActivate, "systemd", false, "Run using systemd socket activation")
flag.StringVar(&fahAddress, "fah.address", defaultFahAddress, "Listen address of FAH client")
flag.BoolVar(&getAPI, "fah.api", false, "Get donor stats from FAH API")
flag.DurationVar(&apiThrottle, "fah.api-throttle", defaultThrottle, "How often to refresh API data")
flag.Parse()
setLogLevel(level)
if noTimestamps || socketActivate {
log.SetFormatter(&log.TextFormatter{DisableTimestamp: true})
} else {
log.SetFormatter(&log.TextFormatter{DisableTimestamp: false, FullTimestamp: true})
}
prometheus.MustRegister(NewExporter())
http.Handle(metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>FAH Exporter</title></head>
<body>
<h1>FAH Exporter</h1>
<p><a href='` + metricsPath + `'>Metrics</a></p>
<h2>More information:</h2>
<p><a href="https://github.com/cosandr/fah-exporter">github.com/cosandr/fah-exporter</a></p>
</body>
</html>`))
})
listener := getListener(socketActivate, listenAddress)
log.Infof("FAH client address: %s", fahAddress)
log.Infof("Starting HTTP server on %s", listener.Addr().String())
log.Fatal(http.Serve(listener, nil))
}
func setLogLevel(level string) {
switch level {
case "error":
log.SetLevel(log.ErrorLevel)
case "warn":
log.SetLevel(log.WarnLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "debug":
log.SetLevel(log.DebugLevel)
default:
log.Warnln("Unrecognized minimum log level; using 'info' as default")
}
}