-
Notifications
You must be signed in to change notification settings - Fork 814
Expand file tree
/
Copy pathprometheus.go
More file actions
235 lines (199 loc) · 7.04 KB
/
Copy pathprometheus.go
File metadata and controls
235 lines (199 loc) · 7.04 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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, or ERR",
},
[]string{"cluster", "consumer_group"},
)
partitionStatusGauge = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "burrow_kafka_topic_partition_status",
Help: "The status of topic partition. It is calculated from the highest status for the individual partitions. Statuses are an index list from OK, WARN, STOP, STALL, REWIND",
},
[]string{"cluster", "consumer_group", "topic", "partition"},
)
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"},
)
)
// DeleteConsumerMetrics deletes all metrics that are labeled with a consumer group
func DeleteConsumerMetrics(cluster, consumer string) {
labels := map[string]string{
"cluster": cluster,
"consumer_group": consumer,
}
consumerTotalLagGauge.Delete(labels)
consumerStatusGauge.Delete(labels)
consumerPartitionLagGauge.DeletePartialMatch(labels)
consumerPartitionCurrentOffset.DeletePartialMatch(labels)
partitionStatusGauge.DeletePartialMatch(labels)
}
// DeleteTopicMetrics deletes all metrics that are labeled with a topic
func DeleteTopicMetrics(cluster, topic string) {
labels := map[string]string{
"cluster": cluster,
"topic": topic,
}
topicPartitionOffsetGauge.DeletePartialMatch(labels)
// If a topic is deleted there cannot be any consumers, so delete all consumer metrics too
// Not strictly necessary as Kafka will delete the consumer groups, which will eventually trigger DeleteConsumerMetrics
consumerPartitionLagGauge.DeletePartialMatch(labels)
consumerPartitionCurrentOffset.DeletePartialMatch(labels)
consumerTotalLagGauge.DeletePartialMatch(labels)
consumerStatusGauge.DeletePartialMatch(labels)
}
// DeleteConsumerTopicMetrics deletes all metrics that are labeled with the provided consumer group AND topic
func DeleteConsumerTopicMetrics(cluster, consumer, topic string) {
labels := map[string]string{
"cluster": cluster,
"consumer_group": consumer,
"topic": topic,
}
partitionStatusGauge.DeletePartialMatch(labels)
consumerPartitionCurrentOffset.DeletePartialMatch(labels)
consumerPartitionLagGauge.DeletePartialMatch(labels)
}
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 {
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 {
labels := map[string]string{
"cluster": cluster,
"consumer_group": consumer,
"topic": partition.Topic,
"partition": strconv.FormatInt(int64(partition.Partition), 10),
}
consumerPartitionLagGauge.With(labels).Set(float64(partition.CurrentLag))
if partition.Complete > 0.0 {
consumerPartitionCurrentOffset.With(labels).Set(float64(partition.End.Offset))
}
if partition.Complete == 1.0 {
partitionStatusGauge.With(labels).Set(float64(partition.Status))
}
}
}
// 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)
}