forked from BonnierNews/consul-agent_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporter.go
57 lines (47 loc) · 1.71 KB
/
exporter.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 (
"flag"
"fmt"
"net/http"
"os"
"github.com/BonnierNews/consul-agent_exporter/collector"
"github.com/hashicorp/consul/api"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
)
func main() {
var (
showVersion = flag.Bool("version", false, "Print version information.")
listenAddress = flag.String("telemetry.addr", ":9217", "host:port for exporter.")
metricsPath = flag.String("telemetry.path", "/metrics", "URL path for surfacing collected metrics.")
consulAddr = flag.String("consul.addr", "http://localhost:8500", "Address to Consul agent")
)
flag.Parse()
if *showVersion {
fmt.Printf(version.Print("consul-agent_exporter"))
os.Exit(0)
}
consulConfig := &api.Config{
Address: *consulAddr,
}
consulClient, err := api.NewClient(consulConfig)
if err != nil {
// All errors from NewClient are unrecoverable
log.Fatalf("Error configuring Consul client: %v", err)
}
collector := collector.NewConsulCollector(consulClient)
prometheus.MustRegister(collector)
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "OK! (... No check against consul being done yet)")
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, *metricsPath, http.StatusMovedPermanently)
})
log.Infoln("Starting Consul agent exporter", version.Info())
log.Infoln("Build context", version.BuildContext())
log.Infoln("Starting server on", *listenAddress)
log.Fatalf("Cannot start Consul agent exporter: %s", http.ListenAndServe(*listenAddress, nil))
}