-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.go
416 lines (351 loc) · 11.7 KB
/
server.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Copyright 2017 Hewlett Packard Enterprise Development LP
//
// 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. See the
// License for the specific language governing permissions and limitations
// under the License.
package main
import (
"encoding/json"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
log "github.com/Sirupsen/logrus"
"github.com/confluentinc/confluent-kafka-go/kafka"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/viper"
"github.com/monasca/monasca-aggregator/aggregation"
"github.com/monasca/monasca-aggregator/models"
)
var windowSize time.Duration
var windowLag time.Duration
var offsetCache = make(map[int64]map[int32]int64)
//init global references to counters
var inCounter = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "in_messages",
Help: "Number of messages read"})
var outCounter = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "out_messages",
Help: "Number of messages written"})
// This is a workaround to handle https://issues.apache.org/jira/browse/KAFKA-3593
var lastMessage = time.Now()
var config = initConfig()
var aggregations = initAggregationSpecs()
var aggregationRules = initAggregationRules(aggregations)
func init() {
// Log as JSON instead of the default ASCII formatter.
log.SetFormatter(&log.JSONFormatter{})
//log.SetOutput(os.Stdout)
//log.SetLevel(log.InfoLevel)
logfile := config.GetString("logging.file")
if logfile != "" {
f, err := os.Create(logfile)
if err != nil {
log.SetOutput(os.Stdout)
log.Fatalf("Failed to create log file: %v", err)
}
log.SetOutput(f)
} else {
log.SetOutput(os.Stdout)
}
loglevel := config.GetString("logging.level")
switch strings.ToUpper(loglevel) {
case "DEBUG":
log.SetLevel(log.DebugLevel)
case "INFO":
log.SetLevel(log.InfoLevel)
case "WARN":
log.SetLevel(log.WarnLevel)
case "ERROR":
log.SetLevel(log.ErrorLevel)
default:
log.SetLevel(log.WarnLevel)
}
prometheus.MustRegister(inCounter)
prometheus.MustRegister(outCounter)
}
func initConfig() *viper.Viper {
config := viper.New()
config.SetDefault("windowSize", 10)
config.SetDefault("windowLag", 2)
config.SetDefault("consumerTopic", "metrics")
config.SetDefault("producerTopic", "metrics")
config.SetDefault("kafka.bootstrap.servers", "localhost:9092")
config.SetDefault("kafka.group.id", "monasca-aggregation")
config.SetDefault("prometheus.endpoint", "localhost:8080")
config.SetConfigName("config")
config.AddConfigPath(".")
err := config.ReadInConfig()
if err != nil {
log.Fatalf("Fatal error reading config file: %s", err)
}
return config
}
func initAggregationSpecs() []models.AggregationSpecification {
config := viper.New()
config.SetConfigName("aggregation-specifications")
config.AddConfigPath(".")
err := config.ReadInConfig()
if err != nil {
log.Fatalf("Fatal error reading aggregations: %s", err)
}
var aggregations []models.AggregationSpecification
err = config.UnmarshalKey("aggregationSpecifications", &aggregations)
if err != nil {
log.Fatalf("Failed to parse aggregations: %s", err)
}
return aggregations
}
func initAggregationRules(specifications []models.AggregationSpecification) []aggregation.Rule {
var rules = make([]aggregation.Rule, len(specifications))
i := 0
for _, spec := range specifications {
rule, err := aggregation.NewAggregationRule(spec)
if err != nil {
log.Fatalf(err.Error())
}
rules[i] = rule
i++
log.Infof("New Spec: %v", spec)
}
return rules
}
func initConsumer(consumerTopic, groupID, bootstrapServers string) *kafka.Consumer {
c, err := kafka.NewConsumer(&kafka.ConfigMap{
"bootstrap.servers": bootstrapServers,
"group.id": groupID,
"session.timeout.ms": 6000,
"go.events.channel.enable": true,
"go.application.rebalance.enable": true,
"enable.auto.commit": false,
"default.topic.config": kafka.ConfigMap{"auto.offset.reset": "earliest"},
})
if err != nil {
log.Fatalf("Failed to create consumer: %s", err)
}
log.Infof("Created kafka consumer %v", c)
err = c.Subscribe(consumerTopic, nil)
if err != nil {
log.Fatalf("Failed to subscribe to topics %c", err)
}
log.Infof("Subscribed to topic %s as group %s", consumerTopic, groupID)
return c
}
func initProducer(bootstrapServers string) *kafka.Producer {
p, err := kafka.NewProducer(&kafka.ConfigMap{"bootstrap.servers": bootstrapServers})
if err != nil {
log.Fatalf("Failed to create producer: %s", err)
}
log.Infof("Created kafka producer %v", p)
return p
}
func handleProducerEvents(p *kafka.Producer) {
for e := range p.Events() {
switch ev := e.(type) {
case *kafka.Message:
m := ev
if m.TopicPartition.Error != nil {
log.Errorf("Delivery failed: %v\n", m.TopicPartition.Error)
} else {
log.Debugf("Delivered message to topic %s [%d] at offset %v\n",
*m.TopicPartition.Topic, m.TopicPartition.Partition, m.TopicPartition.Offset)
}
default:
log.Debugf("Ignored event: %s\n", ev)
}
}
}
// Return a timer for when the first window should be processed
// TODO: Check this math to account for all boundary conditions and large lag times
func firstTick() *time.Timer {
now := time.Now().Unix()
completed := now%int64(windowSize.Seconds()) - int64(windowLag.Seconds())
remaining := int64(windowSize.Seconds()) - completed
firstTick := time.NewTimer(time.Duration(remaining * 1e9))
return firstTick
}
func publishAggregations(outbound chan *kafka.Message, topic *string, c *kafka.Consumer) {
var currentTimeWindow = int64(time.Now().Unix()) / int64(windowSize.Seconds())
// roof(windowLag / windowSize) i.e. number of windows in the lag time
var windowLagCount = int64(windowLag.Seconds()/windowSize.Seconds()) + 1
var activeTimeWindow = currentTimeWindow - windowLagCount
log.Debugf("currentTimeWindow: %d", currentTimeWindow)
log.Debugf("Publishing metrics in window %d", activeTimeWindow)
for _, rule := range aggregationRules {
windowLoop:
for windowTime := range rule.Windows {
if windowTime > activeTimeWindow {
continue windowLoop
}
for _, metric := range rule.GetMetrics(windowTime) {
metric.CreationTime = time.Now().Unix() * 1000
value, _ := json.Marshal(metric)
outbound <- &kafka.Message{TopicPartition: kafka.TopicPartition{Topic: topic, Partition: kafka.PartitionAny}, Value: []byte(value)}
outCounter.Inc()
}
}
}
// TODO: Confirm messages published before committing offsets
offsetList := maxOffsets(activeTimeWindow)
commitOffsets(offsetList, topic, c)
deleteInactiveTimeWindows(activeTimeWindow)
}
func maxOffsets(eventTime int64) map[int32]int64 {
output := make(map[int32]int64)
for windowTime, window := range offsetCache {
if windowTime > eventTime {
continue
}
for partition, offset := range window {
oldOffset, exists := output[partition]
if !exists || offset > oldOffset {
output[partition] = offset
}
}
}
return output
}
// Commit the Kafka offsets.
func commitOffsets(offsetList map[int32]int64, topic *string, c *kafka.Consumer) {
if len(offsetList) <= 0 {
log.Debug("No new offsets, skipping commit")
return
}
//convert ints to kafka structures
finalOffsets := make([]kafka.TopicPartition, len(offsetList))
idx := 0
for partition, value := range offsetList {
newOffset, err := kafka.NewOffset(value + 1)
if err != nil {
log.Fatalf("Failed to update kafka offset %s[%d]@%d", *topic, partition, value)
}
finalOffsets[idx] = kafka.TopicPartition{
Topic: topic,
Partition: partition,
Offset: newOffset}
idx++
}
log.Infof("Committing offsets: %v", finalOffsets)
_, err := c.CommitOffsets(finalOffsets)
if err != nil {
log.Errorf("Consumer errors submitting offsets %v", err)
}
}
// Delete time window aggregations for inactive time windows.
func deleteInactiveTimeWindows(activeTimeWindow int64) {
log.Debugf("Deleteing windows older than %d", activeTimeWindow)
for _, rule := range aggregationRules {
for windowTime := range rule.Windows {
if windowTime <= activeTimeWindow {
delete(rule.Windows, windowTime)
}
}
}
for windowTime := range offsetCache {
if windowTime <= activeTimeWindow {
delete(offsetCache, windowTime)
}
}
}
func processMessage(e *kafka.Message) {
metricEnvelope := models.MetricEnvelope{}
err := json.Unmarshal([]byte(e.Value), &metricEnvelope)
if err != nil {
log.Warnf("%% Invalid metric envelope on %s:%s", e.TopicPartition, string(e.Value))
return
}
log.Debugf("Processing metric: %v", metricEnvelope)
for _, rule := range aggregationRules {
if rule.MatchesMetric(metricEnvelope) {
rule.AddMetric(metricEnvelope, windowSize)
}
}
eventTime := int64(metricEnvelope.Metric.Timestamp) / (1000 * int64(windowSize.Seconds()))
_, exists := offsetCache[eventTime]
if !exists {
offsetCache[eventTime] = make(map[int32]int64)
}
//we can assume each new message read is the latest offset for its partition
offsetCache[eventTime][e.TopicPartition.Partition] = int64(e.TopicPartition.Offset)
inCounter.Inc()
lastMessage = time.Now()
}
// TODO: Add validation for aggregation rules (and metrics?)
// TODO Allow easy grouping on 'all' dimensions
// TODO: Allow start/end consumer offsets to be specified as parameters.
// TODO: Allow start/end aggregation period to be specified.
func main() {
windowSize = time.Duration(config.GetInt("WindowSize") * 1e9)
windowLag = time.Duration(config.GetInt("WindowLag") * 1e9)
consumerTopic := config.GetString("consumerTopic")
producerTopic := config.GetString("producerTopic")
bootstrapServers := config.GetString("kafka.bootstrap.servers")
groupID := config.GetString("kafka.group.id")
prometheusEndpoint := config.GetString("prometheus.endpoint")
sigchan := make(chan os.Signal)
signal.Notify(sigchan, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigchan
log.Fatalf("Caught signal %v: terminating", sig)
}()
c := initConsumer(consumerTopic, groupID, bootstrapServers)
defer c.Close()
p := initProducer(bootstrapServers)
defer p.Close()
go handleProducerEvents(p)
log.Info("Started monasca-aggregation")
// align to time boundaries?
firstTick := firstTick()
var ticker = new(time.Ticker)
go func() {
// Start prometheus endpoint
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(prometheusEndpoint, nil))
}()
log.Infof("Serving metrics on %s/metrics", prometheusEndpoint)
for true {
select {
case <-firstTick.C:
ticker = time.NewTicker(windowSize)
publishAggregations(p.ProduceChannel(), &producerTopic, c)
case <-ticker.C:
publishAggregations(p.ProduceChannel(), &producerTopic, c)
// if the window size has passed and no messages have been received, die in case
// https://issues.apache.org/jira/browse/KAFKA-3593 is occurring.
if time.Now().Sub(lastMessage) > windowSize {
log.Fatalf("No messages received since %v", lastMessage)
}
case ev := <-c.Events():
switch e := ev.(type) {
case kafka.AssignedPartitions:
log.Infof("%% %v", e)
c.Assign(e.Partitions)
case kafka.RevokedPartitions:
log.Infof("%% %v", e)
c.Unassign()
case *kafka.Message:
processMessage(e)
case kafka.OffsetsCommitted:
log.Infof("Commited offsets ", e)
case kafka.PartitionEOF:
//log.Infof("%% Reached %v", e)
case kafka.Error:
log.Fatalf("%% Error: %v", e)
}
}
}
log.Info("Stopped monasca-aggregation")
}