-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
84 lines (71 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"fmt"
"net/http"
"os"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
func init() {
var err error
err = nil
// Flags
err = initFlags()
if err != nil {
log.Errorln(err)
os.Exit(1)
}
// StatusCake API
err = initStkAPI()
if err != nil {
log.Errorln(err)
os.Exit(1)
}
// Prometheus
initPrometheusApp()
err = initPrometheus()
if err != nil {
log.Errorln(err)
os.Exit(1)
}
}
// Main Prometheus handler
func handler(w http.ResponseWriter, r *http.Request) {
filters := r.URL.Query()["collect[]"]
log.Debugln("collect query:", filters)
if len(filters) > 0 {
err := initPrometheus(filters...)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("Couldn't create %s", err)))
return
}
}
// Delegate http serving to Prometheus client library, which will call collector.Collect.
h := promhttp.InstrumentMetricHandler(
prom.Registry,
promhttp.HandlerFor(prom.Gatherers,
promhttp.HandlerOpts{
// ErrorLog: log.NewErrorLogger(),
ErrorHandling: promhttp.ContinueOnError,
}),
)
h.ServeHTTP(w, r)
}
func main() {
log.Infoln("Starting exporter ", config.version)
//This section will start the HTTP server and expose
//any metrics on the /metrics endpoint.
http.HandleFunc(config.metricsPath, handler)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>"` + exporterDescription + `"</title></head>
<body>
<h1>"` + exporterDescription + `"</h1>
<p><a href="` + config.metricsPath + `">Metrics</a></p>
</body>
</html>`))
})
log.Info("Beginning to serve on port " + config.listenAddress)
log.Fatal(http.ListenAndServe(config.listenAddress, nil))
}