forked from cloudflare/kafka_zookeeper_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
90 lines (73 loc) · 2.18 KB
/
main.go
File metadata and controls
90 lines (73 loc) · 2.18 KB
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
85
86
87
88
89
90
package main
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
zkLog "log"
)
const (
metricsRoute = "/metrics"
probeRoute = "/kafka"
)
var (
version = "unknown"
listenAddress = flag.String("web.listen-address", ":9381", "Address to listen on for web interface and telemetry.")
serverTimeout = flag.Duration("web.timeout", 60*time.Second, "Timeout for responding to HTTP requests.")
zkTimeout = flag.Duration("zk.timeout", 5*time.Second, "Timeout for ZooKeeper requests")
showVersion = flag.Bool("version", false, "Show version and exit")
)
func handler(w http.ResponseWriter, r *http.Request) {
zookeeper := r.URL.Query().Get("zookeeper")
if zookeeper == "" {
http.Error(w, "'zookeeper' parameter must be specified", 400)
return
}
chroot := r.URL.Query().Get("chroot")
topic := r.URL.Query().Get("topic")
topics := []string{}
if topic != "" {
topics = strings.Split(topic, ",")
}
registry := prometheus.NewRegistry()
registry.MustRegister(newCollector(zookeeper, chroot, topics))
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
func main() {
flag.Parse()
if *showVersion {
fmt.Println(version)
os.Exit(0)
}
// kazoo uses ZooKeeper client that logs everything by default, so we end up
// with duplicated logs we don't control, disable vanilla logger messages
// and rely on logs generated by our code
zkLog.SetOutput(ioutil.Discard)
http.Handle(metricsRoute, promhttp.Handler())
http.HandleFunc(probeRoute, handler)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<html>
<head><title>Kafka ZooKeeper Exporter</title></head>
<body>
<p><a href='` + metricsRoute + `'>Metrics</a></p>
<p><a href='` + probeRoute + `?zookeeper=zookeeper1.local:2181&chroot=/path'>Example Kafka ZooKeeper probe</a></p>
</body>
</html>
`))
})
log.Infoln("Listening on", *listenAddress)
s := &http.Server{
Addr: *listenAddress,
ReadTimeout: *serverTimeout,
WriteTimeout: *serverTimeout,
}
log.Fatal(s.ListenAndServe())
}