-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
57 lines (48 loc) · 1.71 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
package main
import (
"fmt"
"net/http"
"os"
"github.com/alecthomas/kingpin/v2"
lib "github.com/eu-cdse/openstack_exporter/internal"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/version"
)
var (
config = promlog.Config{}
port = kingpin.Flag("port", "Port to serve the metrics on").Default("9595").Int()
volumeLimit = kingpin.Flag("volume.limit", "Max number of volumes when on OTC").Default("-1").Float64()
)
func main() {
kingpin.CommandLine.UsageWriter(os.Stdout)
flag.AddFlags(kingpin.CommandLine, &config)
kingpin.HelpFlag.Short('h')
kingpin.Version(version.Print("openstack_exporter"))
kingpin.Parse()
// Initialize the logger
logger := promlog.New(&config)
lib.SetLogger(logger)
openStack := lib.NewOpenStackCollector(*volumeLimit)
// Custom registry to not collect all go low-level metrics
promRegistry := prometheus.NewRegistry()
promRegistry.MustRegister(openStack)
handler := promhttp.HandlerFor(promRegistry, promhttp.HandlerOpts{})
http.Handle("/metrics", handler)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>OpenStack Exporter</title></head>
<body>
<h1>OpenStack Exporter</h1>
<p><a href='/metrics'>Metrics</a></p>
</body>
</html>`))
})
level.Info(logger).Log("message", "Starting exporter", "address", fmt.Sprintf(":%d", *port))
if err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil); err != nil {
level.Error(logger).Log("message", "Failed to start HTTP server", "err", err)
}
}