-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin-api.go
87 lines (68 loc) · 1.99 KB
/
admin-api.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
85
86
87
package main
import (
"encoding/json"
"net/http"
"github.com/IBM/sarama"
)
func (s *Server) ServeKafkaMetrics(w http.ResponseWriter, r *http.Request) {
brokers := s.kafkaConn.Brokers()
brokerIDs := make([]int32, len(brokers))
for i, broker := range brokers {
brokerIDs[i] = broker.ID()
}
topics, err := s.kafkaConn.Topics()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
topicDetails := make(map[string]interface{})
for _, topic := range topics {
partitions, err := s.kafkaConn.Partitions(topic)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
partitionDetails := make(map[int32]interface{})
for _, partition := range partitions {
offsetNewest, err := s.kafkaConn.GetOffset(topic, partition, sarama.OffsetNewest)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
offsetOldest, err := s.kafkaConn.GetOffset(topic, partition, sarama.OffsetOldest)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
leader, err := s.kafkaConn.Leader(topic, partition)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
replicas, err := s.kafkaConn.Replicas(topic, partition)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
isr, err := s.kafkaConn.InSyncReplicas(topic, partition)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
partitionDetails[partition] = map[string]interface{}{
"offsetNewest": offsetNewest,
"offsetOldest": offsetOldest,
"leader": leader.ID(),
"replicas": replicas,
"isr": isr,
}
}
topicDetails[topic] = partitionDetails
}
response := map[string]interface{}{
"brokers": brokerIDs,
"topics": topicDetails,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}