-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
63 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
61
62
63
package main
import (
"flag"
"net/http"
"os"
"os/signal"
"syscall"
log "github.com/sirupsen/logrus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/client_golang/prometheus"
exp "github.com/banzaicloud/preemption-exporter/exporter"
)
func init() {
flag.Parse()
parsedLevel, err := log.ParseLevel(*rawLevel)
if err != nil {
log.Fatal(err)
}
logLevel = parsedLevel
}
var logLevel = log.InfoLevel
var bindAddr = flag.String("bind-addr", ":9189", "bind address for the metrics server")
var metricsPath = flag.String("metrics-path", "/metrics", "path to metrics endpoint")
var rawLevel = flag.String("log-level", "info", "log level")
var metadataEndpoint = flag.String("metadata-endpoint", "http://metadata.google.internal/computeMetadata/v1/instance/", "metadata endpoint to query")
func main() {
log.SetLevel(logLevel)
log.Info("Starting preemption-exporter")
log.Debug("registering preemption exporter")
prometheus.MustRegister(exp.NewPreemptionExporter(*metadataEndpoint))
go serveMetrics()
exitChannel := make(chan os.Signal)
signal.Notify(exitChannel, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
exitSignal := <-exitChannel
log.WithFields(log.Fields{"signal": exitSignal}).Infof("Caught %s signal, exiting", exitSignal)
}
func serveMetrics() {
log.Infof("Starting metric http endpoint on %s", *bindAddr)
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", rootHandler)
log.Fatal(http.ListenAndServe(*bindAddr, nil))
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`<html>
<head><title>Preemption Exporter</title></head>
<body>
<h1>Preemption Exporter</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
</body>
</html>`))
}