-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
67 lines (62 loc) · 2.52 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
package main
import (
"flag"
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
listen_address = flag.String("listen_address", ":10001", "http port where metrics are published")
metrics_url = flag.String("metrics_url", "/metrics", "URL where mettrics is accessible")
kafka_brokers = flag.String("kafka_brokers", "127.0.0.1:9092", "Comma-separated list of Kafka brokers")
namespace = flag.String("namespace", "kafka", "Namespace for metrics")
topics = flag.String("topics", "", "Comma separated list of kafka topics")
gometrics = flag.Bool("with-go-metrics", false, "Should we import go runtime and http handler metrics")
groups arrayFlags
)
func main() {
var group_map map[string][]string
flag.Var(&groups, "group", "consumer-group and topics in the form of group1:topic1,topic2,topic3 etc")
flag.Parse()
*kafka_brokers = check_input_str(*kafka_brokers, "Empty broker list")
*topics = check_input_str(*topics, "Empty topics list")
broker_list := get_list_from_string(*kafka_brokers, ",", "Empty broker list")
topic_list := get_list_from_string(*topics, ",", "Empty string list")
topic_list = unique_list(topic_list)
if groups.GetElCount() > 0 {
group_map = get_group_map(&groups, "Incorrrect values")
}
*metrics_url = check_input_str(*metrics_url, "Empty metrics url")
*namespace = check_input_str(*namespace, "Empty namspace string")
client := newKafkaClient(broker_list, topic_list, group_map)
if client.getRequiredTopicCount() < 1 {
log.Fatal("Topic count for stats is 0")
}
log.Printf("Starting kafka exporter")
if !*gometrics {
registry := prometheus.NewRegistry()
if err := registry.Register(newExporter(client, *namespace)); err != nil {
log.Fatalf("Registration error: %v", err)
}
handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
http.Handle(*metrics_url, handler)
} else {
prometheus.DefaultRegisterer.MustRegister(newExporter(client, *namespace))
http.Handle(*metrics_url, promhttp.Handler())
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<html>
<head><title>Kafka Consumer Exporter </title></head>
<body>
<h1>Kafka exporter </h1>
<p><a href='` + *metrics_url + `'>Metrics</a></p>
</body>
</html>`))
})
log.Printf("Listening on %s, url: %s", *listen_address, *metrics_url)
if err := http.ListenAndServe(*listen_address, nil); err != nil {
log.Fatalf("Error in starting http listner: %v", err)
}
}