forked from linkedin/Burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_notifier.go
322 lines (288 loc) · 10.4 KB
/
http_notifier.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* Copyright 2015 LinkedIn Corp. Licensed under the Apache License, Version
* 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/
package main
import (
"bytes"
log "github.com/cihub/seelog"
"github.com/pborman/uuid"
"io"
"io/ioutil"
"math/rand"
"net"
"net/http"
"os"
"strings"
"sync"
"text/template"
"time"
)
type HttpNotifier struct {
app *ApplicationContext
templatePost *template.Template
templateDelete *template.Template
extras map[string]string
refreshTicker *time.Ticker
quitChan chan struct{}
groupIds map[string]map[string]Event
groupList map[string]map[string]bool
groupLock sync.RWMutex
resultsChannel chan *ConsumerGroupStatus
httpClient *http.Client
}
type Event struct {
Id string
Start time.Time
}
func NewHttpNotifier(app *ApplicationContext) (*HttpNotifier, error) {
// Helper functions for templates
fmap := template.FuncMap{
"jsonencoder": templateJsonEncoder,
"topicsbystatus": classifyTopicsByStatus,
"partitioncounts": templateCountPartitions,
"add": templateAdd,
"minus": templateMinus,
"multiply": templateMultiply,
"divide": templateDivide,
"maxlag": maxLagHelper,
}
// Compile the templates
templatePost, err := template.New("post").Funcs(fmap).ParseFiles(app.Config.Httpnotifier.TemplatePost)
if err != nil {
log.Criticalf("Cannot parse HTTP notifier POST template: %v", err)
os.Exit(1)
}
templatePost = templatePost.Templates()[0]
templateDelete, err := template.New("delete").Funcs(fmap).ParseFiles(app.Config.Httpnotifier.TemplateDelete)
if err != nil {
log.Criticalf("Cannot parse HTTP notifier DELETE template: %v", err)
os.Exit(1)
}
templateDelete = templateDelete.Templates()[0]
// Parse the extra parameters for the templates
extras := make(map[string]string)
for _, extra := range app.Config.Httpnotifier.Extras {
parts := strings.Split(extra, "=")
extras[parts[0]] = parts[1]
}
return &HttpNotifier{
app: app,
templatePost: templatePost,
templateDelete: templateDelete,
extras: extras,
quitChan: make(chan struct{}),
groupIds: make(map[string]map[string]Event),
groupList: make(map[string]map[string]bool),
groupLock: sync.RWMutex{},
resultsChannel: make(chan *ConsumerGroupStatus),
httpClient: &http.Client{
Timeout: time.Duration(app.Config.Httpnotifier.Timeout) * time.Second,
Transport: &http.Transport{
Dial: (&net.Dialer{
KeepAlive: time.Duration(app.Config.Httpnotifier.Keepalive) * time.Second,
}).Dial,
Proxy: http.ProxyFromEnvironment,
},
},
}, nil
}
func (notifier *HttpNotifier) handleEvaluationResponse(result *ConsumerGroupStatus) {
if int(result.Status) >= notifier.app.Config.Httpnotifier.PostThreshold {
// We only use IDs if we are sending deletes
idStr := ""
startTime := time.Now()
if notifier.app.Config.Httpnotifier.SendDelete {
if _, ok := notifier.groupIds[result.Cluster]; !ok {
// Create the cluster map
notifier.groupIds[result.Cluster] = make(map[string]Event)
}
if _, ok := notifier.groupIds[result.Cluster][result.Group]; !ok {
// Create Event and Id
eventId := uuid.NewRandom()
idStr = eventId.String()
notifier.groupIds[result.Cluster][result.Group] = Event{
Id: idStr,
Start: startTime,
}
} else {
idStr = notifier.groupIds[result.Cluster][result.Group].Id
startTime = notifier.groupIds[result.Cluster][result.Group].Start
}
}
// NOTE - I'm leaving the JsonEncode item in here so as not to break compatibility. New helpers go in the FuncMap above
bytesToSend := new(bytes.Buffer)
err := notifier.templatePost.Execute(bytesToSend, struct {
Cluster string
Group string
Id string
Start time.Time
Extras map[string]string
Result *ConsumerGroupStatus
JsonEncode func(interface{}) string
}{
Cluster: result.Cluster,
Group: result.Group,
Id: idStr,
Start: startTime,
Extras: notifier.extras,
Result: result,
JsonEncode: templateJsonEncoder,
})
if err != nil {
log.Errorf("Failed to assemble POST: %v", err)
return
}
// Send POST to HTTP endpoint
req, err := http.NewRequest("POST", notifier.app.Config.Httpnotifier.Url, bytesToSend)
req.Header.Set("Content-Type", "application/json")
resp, err := notifier.httpClient.Do(req)
if err != nil {
log.Errorf("Failed to send POST for group %s in cluster %s at severity %v (Id %s): %v", result.Group, result.Cluster, result.Status, idStr, err)
return
}
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
if (resp.StatusCode >= 200) && (resp.StatusCode <= 299) {
log.Debugf("Sent POST for group %s in cluster %s at severity %v (Id %s)", result.Group, result.Cluster, result.Status, idStr)
} else {
log.Errorf("Failed to send POST for group %s in cluster %s at severity %v (Id %s): %s", result.Group,
result.Cluster, result.Status, idStr, resp.Status)
}
}
if notifier.app.Config.Httpnotifier.SendDelete && (result.Status == StatusOK) {
if _, ok := notifier.groupIds[result.Cluster][result.Group]; ok {
// Send DELETE to HTTP endpoint
bytesToSend := new(bytes.Buffer)
err := notifier.templateDelete.Execute(bytesToSend, struct {
Cluster string
Group string
Id string
Start time.Time
Extras map[string]string
}{
Cluster: result.Cluster,
Group: result.Group,
Id: notifier.groupIds[result.Cluster][result.Group].Id,
Start: notifier.groupIds[result.Cluster][result.Group].Start,
Extras: notifier.extras,
})
if err != nil {
log.Errorf("Failed to assemble DELETE for group %s in cluster %s (Id %s): %v", result.Group,
result.Cluster, notifier.groupIds[result.Cluster][result.Group].Id, err)
return
}
req, err := http.NewRequest("DELETE", notifier.app.Config.Httpnotifier.Url, bytesToSend)
req.Header.Set("Content-Type", "application/json")
resp, err := notifier.httpClient.Do(req)
if err != nil {
log.Errorf("Failed to send DELETE for group %s in cluster %s (Id %s): %v", result.Group,
result.Cluster, notifier.groupIds[result.Cluster][result.Group].Id, err)
return
}
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
if (resp.StatusCode >= 200) && (resp.StatusCode <= 299) {
log.Debugf("Sent DELETE for group %s in cluster %s (Id %s)", result.Group, result.Cluster,
notifier.groupIds[result.Cluster][result.Group].Id)
} else {
log.Errorf("Failed to send DELETE for group %s in cluster %s (Id %s): %s", result.Group,
result.Cluster, notifier.groupIds[result.Cluster][result.Group].Id, resp.Status)
}
// Remove ID for group that is now clear
delete(notifier.groupIds[result.Cluster], result.Group)
}
}
}
func (notifier *HttpNotifier) refreshConsumerGroups() {
notifier.groupLock.Lock()
defer notifier.groupLock.Unlock()
for cluster, _ := range notifier.app.Config.Kafka {
clusterGroups, ok := notifier.groupList[cluster]
if !ok {
notifier.groupList[cluster] = make(map[string]bool)
clusterGroups = notifier.groupList[cluster]
}
// Get a current list of consumer groups
storageRequest := &RequestConsumerList{Result: make(chan []string), Cluster: cluster}
notifier.app.Storage.requestChannel <- storageRequest
consumerGroups := <-storageRequest.Result
// Mark all existing groups false
for consumerGroup := range notifier.groupList {
clusterGroups[consumerGroup] = false
}
// Check for new groups, mark existing groups true
for _, consumerGroup := range consumerGroups {
// Don't bother adding groups in the blacklist
if (notifier.app.Storage.groupBlacklist != nil) && notifier.app.Storage.groupBlacklist.MatchString(consumerGroup) {
continue
}
if _, ok := clusterGroups[consumerGroup]; !ok {
// Add new consumer group and start checking it
log.Debugf("Start evaluating consumer group %s in cluster %s", consumerGroup, cluster)
go notifier.startConsumerGroupEvaluator(consumerGroup, cluster)
}
clusterGroups[consumerGroup] = true
}
// Delete groups that are still false
for consumerGroup := range clusterGroups {
if !clusterGroups[consumerGroup] {
log.Debugf("Remove evaluator for consumer group %s in cluster %s", consumerGroup, cluster)
delete(clusterGroups, consumerGroup)
}
}
}
}
func (notifier *HttpNotifier) startConsumerGroupEvaluator(group string, cluster string) {
// Sleep for a random portion of the check interval
time.Sleep(time.Duration(rand.Int63n(notifier.app.Config.Httpnotifier.Interval*1000)) * time.Millisecond)
for {
// Make sure this group still exists
notifier.groupLock.RLock()
if _, ok := notifier.groupList[cluster][group]; !ok {
notifier.groupLock.RUnlock()
log.Debugf("Stopping evaluator for consumer group %s in cluster %s", group, cluster)
break
}
notifier.groupLock.RUnlock()
// Send requests for group status - responses are handled by the main loop (for now)
storageRequest := &RequestConsumerStatus{Result: notifier.resultsChannel, Cluster: cluster, Group: group}
notifier.app.Storage.requestChannel <- storageRequest
// Sleep for the check interval
time.Sleep(time.Duration(notifier.app.Config.Httpnotifier.Interval) * time.Second)
}
}
func (notifier *HttpNotifier) Start() {
// Get a group list to start with (this will start the notifiers)
notifier.refreshConsumerGroups()
// Set a ticker to refresh the group list periodically
notifier.refreshTicker = time.NewTicker(time.Duration(notifier.app.Config.Lagcheck.ZKGroupRefresh) * time.Second)
// Main loop to handle refreshes and evaluation responses
go func() {
OUTERLOOP:
for {
select {
case <-notifier.quitChan:
break OUTERLOOP
case <-notifier.refreshTicker.C:
notifier.refreshConsumerGroups()
case result := <-notifier.resultsChannel:
go notifier.handleEvaluationResponse(result)
}
}
}()
}
func (notifier *HttpNotifier) Stop() {
if notifier.refreshTicker != nil {
notifier.refreshTicker.Stop()
notifier.groupLock.Lock()
notifier.groupList = make(map[string]map[string]bool)
notifier.groupLock.Unlock()
}
close(notifier.quitChan)
}