-
Notifications
You must be signed in to change notification settings - Fork 46
/
main.go
60 lines (52 loc) · 1.75 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
package main
import (
"fmt"
"github.com/google-cloud-tools/kube-eagle/collector"
"github.com/prometheus/client_golang/prometheus"
"net/http"
"os"
"strconv"
"github.com/google-cloud-tools/kube-eagle/options"
"github.com/kelseyhightower/envconfig"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
func healthcheck(collector *collector.KubeEagleCollector) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Debug("Healthcheck has been called")
if collector.IsHealthy() == true {
w.Write([]byte("Ok"))
} else {
http.Error(w, "Healthcheck failed", http.StatusServiceUnavailable)
}
})
}
func main() {
// Initialize logrus settings
log.SetOutput(os.Stdout)
log.SetFormatter(&log.JSONFormatter{})
// Parse & validate environment variable
opts := options.NewOptions()
err := envconfig.Process("", opts)
if err != nil {
log.Fatal("Error parsing env vars into opts", err)
}
// Set log level from environment variable
level, err := log.ParseLevel(opts.LogLevel)
if err != nil {
log.Panicf("Loglevel could not be parsed as one of the known loglevels. See logrus documentation for valid log level inputs. Given input was: '%s'", opts.LogLevel)
}
log.SetLevel(level)
// Start kube eagle exporter
log.Infof("Starting kube eagle v%v", opts.Version)
collector, err := collector.NewKubeEagleCollector(opts)
if err != nil {
log.Fatalf("could not start kube eagle collector: '%v'", err)
}
prometheus.MustRegister(collector)
http.Handle("/metrics", promhttp.Handler())
http.Handle("/health", healthcheck(collector))
address := fmt.Sprintf("%v:%s", opts.Host, strconv.Itoa(opts.Port))
log.Info("Listening on ", address)
log.Fatal(http.ListenAndServe(address, nil))
}