forked from linkedin/Burrow
/
prometheus.go
182 lines (154 loc) · 5.06 KB
/
prometheus.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package httpserver
import (
"net/http"
"strconv"
"github.com/prometheus/client_golang/prometheus"
"github.com/linkedin/Burrow/core/protocol"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
consumerTotalLagGauge = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "burrow_kafka_consumer_lag_total",
Help: "The sum of all partition current lag values for the group",
},
[]string{"cluster", "consumer_group"},
)
consumerStatusGauge = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "burrow_kafka_consumer_status",
Help: "The status of the consumer group. It is calculated from the highest status for the individual partitions. Statuses are an index list from NOTFOUND, OK, WARN, ERR, STOP, STALL, REWIND",
},
[]string{"cluster", "consumer_group"},
)
consumerPartitionCurrentOffset = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "burrow_kafka_consumer_current_offset",
Help: "Latest offset that Burrow is storing for this partition",
},
[]string{"cluster", "consumer_group", "topic", "partition"},
)
consumerPartitionLagGauge = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "burrow_kafka_consumer_partition_lag",
Help: "Number of messages the consumer group is behind by for a partition as reported by Burrow",
},
[]string{"cluster", "consumer_group", "topic", "partition"},
)
topicPartitionOffsetGauge = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "burrow_kafka_topic_partition_offset",
Help: "Latest offset the topic that Burrow is storing for this partition",
},
[]string{"cluster", "topic", "partition"},
)
)
func (hc *Coordinator) handlePrometheusMetrics() http.HandlerFunc {
promHandler := promhttp.Handler()
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
for _, cluster := range listClusters(hc.App) {
for _, consumer := range listConsumers(hc.App, cluster) {
consumerStatus := getFullConsumerStatus(hc.App, cluster, consumer)
if consumerStatus == nil ||
consumerStatus.Status == protocol.StatusNotFound ||
consumerStatus.Complete < 1.0 {
continue
}
labels := map[string]string{
"cluster": cluster,
"consumer_group": consumer,
}
consumerTotalLagGauge.With(labels).Set(float64(consumerStatus.TotalLag))
consumerStatusGauge.With(labels).Set(float64(consumerStatus.Status))
for _, partition := range consumerStatus.Partitions {
if partition.Complete < 1.0 {
continue
}
labels := map[string]string{
"cluster": cluster,
"consumer_group": consumer,
"topic": partition.Topic,
"partition": strconv.FormatInt(int64(partition.Partition), 10),
}
consumerPartitionCurrentOffset.With(labels).Set(float64(partition.End.Offset))
consumerPartitionLagGauge.With(labels).Set(float64(partition.CurrentLag))
}
}
// Topics
for _, topic := range listTopics(hc.App, cluster) {
for partitionNumber, offset := range getTopicDetail(hc.App, cluster, topic) {
topicPartitionOffsetGauge.With(map[string]string{
"cluster": cluster,
"topic": topic,
"partition": strconv.FormatInt(int64(partitionNumber), 10),
}).Set(float64(offset))
}
}
}
promHandler.ServeHTTP(resp, req)
})
}
func listClusters(app *protocol.ApplicationContext) []string {
request := &protocol.StorageRequest{
RequestType: protocol.StorageFetchClusters,
Reply: make(chan interface{}),
}
app.StorageChannel <- request
response := <-request.Reply
if response == nil {
return []string{}
}
return response.([]string)
}
func listConsumers(app *protocol.ApplicationContext, cluster string) []string {
request := &protocol.StorageRequest{
RequestType: protocol.StorageFetchConsumers,
Cluster: cluster,
Reply: make(chan interface{}),
}
app.StorageChannel <- request
response := <-request.Reply
if response == nil {
return []string{}
}
return response.([]string)
}
func getFullConsumerStatus(app *protocol.ApplicationContext, cluster, consumer string) *protocol.ConsumerGroupStatus {
request := &protocol.EvaluatorRequest{
Cluster: cluster,
Group: consumer,
ShowAll: true,
Reply: make(chan *protocol.ConsumerGroupStatus),
}
app.EvaluatorChannel <- request
response := <-request.Reply
return response
}
func listTopics(app *protocol.ApplicationContext, cluster string) []string {
request := &protocol.StorageRequest{
RequestType: protocol.StorageFetchTopics,
Cluster: cluster,
Reply: make(chan interface{}),
}
app.StorageChannel <- request
response := <-request.Reply
if response == nil {
return []string{}
}
return response.([]string)
}
func getTopicDetail(app *protocol.ApplicationContext, cluster, topic string) []int64 {
request := &protocol.StorageRequest{
RequestType: protocol.StorageFetchTopic,
Cluster: cluster,
Topic: topic,
Reply: make(chan interface{}),
}
app.StorageChannel <- request
response := <-request.Reply
if response == nil {
return []int64{}
}
return response.([]int64)
}