forked from CiscoDevNet/bigmuddy-network-telemetry-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xport_kafka.go
945 lines (821 loc) · 22.4 KB
/
xport_kafka.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
//
// January 2016, cisco
//
// Copyright (c) 2016 by cisco Systems, Inc.
// All rights reserved.
//
//
package main
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/Shopify/sarama"
log "github.com/Sirupsen/logrus"
"github.com/prometheus/client_golang/prometheus"
cluster "gopkg.in/bsm/sarama-cluster.v2"
"io/ioutil"
"regexp"
"strings"
"text/template"
"time"
)
const (
KAFKATOPICDEFAULT = "telemetry"
KAFKATOPICMAXLEN = 250
KAFKARECONNECTTIMEOUT = 1
KAFKACHECKPOINTFLUSH = 1
)
type kafkaKeySpec int
//
// kafkaTopicExtractor is the signature of a method used to extract a
// topic. Closure used with statically configured topics of template
// extraction.
type kafkaTopicExtractor func(*dataMsgMetaData) string
const (
KAFKA_KEY_SPEC_NONE = iota
//
// This spec forces ID and PATH into key. This is here to
// support esoteric requirement by consumer.
KAFKA_KEY_SPEC_ID_AND_PATH
//
// This spec used ID as key.
KAFKA_KEY_SPEC_ID
)
var keyExtractionMethod = map[string]kafkaKeySpec{
"": KAFKA_KEY_SPEC_NONE,
"path_and_id": KAFKA_KEY_SPEC_ID_AND_PATH,
"id": KAFKA_KEY_SPEC_ID,
}
type kafkaProducerConfig struct {
name string
fetchTopic kafkaTopicExtractor
brokerList []string
keySpec kafkaKeySpec
streamSpec *dataMsgStreamSpec
requiredAcks sarama.RequiredAcks
logData bool
logCtx *log.Entry
stats msgStats
}
type kafkaConsumerConfig struct {
name string
topic string
consumerGroup string
brokerList []string
keySpec kafkaKeySpec
msgEncoding encoding
logData bool
}
//
// kafkaProducerTopicExtractor provides the closure required to
// extract the topic. This could be static, or template based
// operating on message meta data. Eventually, this may be extended to
// operate on body.
func kafkaProducerTopicExtractor(
topic string, template *template.Template) kafkaTopicExtractor {
if template != nil {
validate, _ := regexp.Compile("[^a-zA-Z0-9\\._\\-]+")
return func(m *dataMsgMetaData) string {
if m != nil {
var b bytes.Buffer
err := template.Execute(&b, m)
if err == nil {
topic := b.String()
//
// Post process string for topic rules:
// i.e. < 250 chars, and matching [a-zA-Z0-9\\._\\-]
if len(topic) > KAFKATOPICMAXLEN {
topic = topic[:KAFKATOPICMAXLEN]
}
return validate.ReplaceAllLiteralString(topic, "___")
}
// default to returning topic when template execution
// fails.
return topic
} else {
// Provide a description of topic when no meta data is
// passed.
return fmt.Sprintf("metadata template [%v]", template.Name())
}
}
} else {
return func(*dataMsgMetaData) string {
return topic
}
}
}
func (cfg *kafkaProducerConfig) dataMsgToKafkaMessage(imsg dataMsg) (
error, *sarama.ProducerMessage, *string, *string, *[]byte) {
var key string
topic := cfg.fetchTopic(imsg.getMetaData())
//
// Ask for the stream in the desired format
err, rawstream := imsg.produceByteStream(cfg.streamSpec)
if err != nil {
cfg.logCtx.WithError(err).WithFields(log.Fields{
"msg": imsg.getDataMsgDescription(),
"topic": topic,
}).Error("kafka feeder loop, failed message")
return err, nil, nil, &topic, nil
} else if rawstream == nil {
return nil, nil, nil, &topic, nil
}
switch cfg.keySpec {
case KAFKA_KEY_SPEC_ID_AND_PATH:
//
// Used in customer demo and expected to be
// deprecated.
err, identifier := imsg.getMetaDataIdentifier()
if err != nil {
identifier = ""
}
err, path := imsg.getMetaDataPath()
if err != nil {
path = ""
}
key = path + ":" + identifier
case KAFKA_KEY_SPEC_ID:
err, key = imsg.getMetaDataIdentifier()
if err != nil {
cfg.logCtx.WithError(err).WithFields(log.Fields{
"msg": imsg.getDataMsgDescription(),
}).Error("kafka feeder loop, " +
"failed to extract ID key")
}
case KAFKA_KEY_SPEC_NONE:
// This is fine. We don't have to have a key.
}
return nil, &sarama.ProducerMessage{
Topic: topic,
Key: sarama.StringEncoder(key),
Value: sarama.ByteEncoder(rawstream),
}, &key, &topic, &rawstream
}
//
// Loop taking content on channel and pushing out to kafka
func (cfg *kafkaProducerConfig) kafkaFeederLoop(
dataChan <-chan dataMsg,
ctrlChan <-chan *ctrlMsg,
msgproducer sarama.SyncProducer) {
stats := &cfg.stats
logCtx := cfg.logCtx
logCtx.Info("kafka producer configured")
for {
select {
case msg, ok := <-dataChan:
if !ok {
// Channel has been closed. Our demise
// is near. SHUTDOWN is likely to be
// received soon on control
// channel. While the only message we
// receive is shutdown, this could
// have done without a control channel
// and instead should have used range
// over the channel.
//
dataChan = nil
continue
}
err, kafkamsg, key, topic, rawstream :=
cfg.dataMsgToKafkaMessage(msg)
if err != nil {
// event logged in called function, need to count
stats.MsgsNOK++
continue
} else if kafkamsg == nil {
// count it?
continue
}
partition, offset, err := msgproducer.SendMessage(kafkamsg)
if err != nil {
stats.MsgsNOK++
kafkaMetaMonitor.CountersErrors.WithLabelValues(
cfg.name, *topic, *key, "out").Inc()
logMsgCtx := logCtx.WithError(err).WithFields(log.Fields{
"msg": msg.getDataMsgDescription(),
"topic": *topic,
"key": *key,
"content": hex.Dump(*rawstream),
})
if cfg.logData {
logMsgCtx = logMsgCtx.WithFields(log.Fields{
"content": hex.Dump(*rawstream),
})
}
logMsgCtx.WithError(err).Error(
"kafka feeder loop, failed message")
} else {
stats.MsgsOK++
kafkaMetaMonitor.CountersMsgs.WithLabelValues(
cfg.name, *topic, *key, "out").Inc()
kafkaMetaMonitor.CountersBytes.WithLabelValues(
cfg.name, *topic, *key, "out").Add(
float64(len(*rawstream)))
if cfg.logData {
logCtx.WithFields(
log.Fields{
"msg": msg.getDataMsgDescription(),
"key": *key,
"partition": partition,
"offset": offset,
"topic": *topic,
"content": hex.Dump(*rawstream)}).Debug(
"kafka feeder loop, posted message")
}
}
case msg := <-ctrlChan:
if cfg.kafkaHandleCtrlMsg(msg) {
//
// Close connection to kafka. Should we drain the
// dataChan first? Probably.
if err := msgproducer.Close(); err !=
nil {
logCtx.WithError(err).Error(
"Failed to shut down producer cleanly")
}
return
}
}
}
}
func (cfg *kafkaProducerConfig) kafkaHandleCtrlMsg(msg *ctrlMsg) bool {
switch msg.id {
case REPORT:
content, _ := json.Marshal(cfg.stats)
resp := &ctrlMsg{
id: ACK,
content: content,
respChan: nil,
}
msg.respChan <- resp
case SHUTDOWN:
cfg.logCtx.Info("kafka producer rxed SHUTDOWN")
resp := &ctrlMsg{
id: ACK,
respChan: nil,
}
msg.respChan <- resp
return true
default:
cfg.logCtx.Error("kafka producer, unknown ctrl message")
}
return false
}
//
// kafkaFeederLoopSticky handles setting up the kafka sync produces as
// often as possible.
func (cfg *kafkaProducerConfig) kafkaFeederLoopSticky(
dataChan <-chan dataMsg,
ctrlChan <-chan *ctrlMsg) {
var msgproducer sarama.SyncProducer
var err error
for {
config := sarama.NewConfig()
config.Producer.RequiredAcks = cfg.requiredAcks
config.Producer.Return.Successes = true
msgproducer, err = sarama.NewSyncProducer(cfg.brokerList, config)
if err != nil {
cfg.logCtx.WithError(err).Error(
"kafka producer setup (will retry)")
select {
case <-time.After(KAFKARECONNECTTIMEOUT * time.Second):
// Time to try again
break
case msg := <-ctrlChan:
if cfg.kafkaHandleCtrlMsg(msg) {
//
// We're going down. Given up and leave.
return
}
}
continue
} else {
break
}
}
//
// We're all setup and ready to go... never back until we're
// done.
cfg.kafkaFeederLoop(dataChan, ctrlChan, msgproducer)
}
//
// Module implementing outputNodeModule interface.
type kafkaOutputModule struct{}
func kafkaOutputModuleNew() outputNodeModule {
return &kafkaOutputModule{}
}
//
// Setup a kafka producer and get back a data and control channel
func (k *kafkaOutputModule) configure(name string, nc nodeConfig) (
error, chan<- dataMsg, chan<- *ctrlMsg) {
var topicTemplate *template.Template
var requiredAcks sarama.RequiredAcks
logctx := logger.WithFields(log.Fields{"name": name})
brokers, err := nc.config.GetString(name, "brokers")
if err != nil {
logctx.WithError(err).Error(
"kafka producer setup, broker config required")
return err, nil, nil
}
brokerList := strings.Split(brokers, ",")
dataChannelDepth, err := nc.config.GetInt(name, "datachanneldepth")
if err != nil {
dataChannelDepth = DATACHANNELDEPTH
}
//
// Setup topic handling.
topicTemplateFileName, err := nc.config.GetString(
name, "topic_metadata_template")
if err == nil {
//
// A template has been provided. Lets make sure we can load
// and parse it.
templateSpec, err := ioutil.ReadFile(topicTemplateFileName)
if err != nil {
logctx.WithError(err).Error(
"kafka producer setup, topic template load fail")
return err, nil, nil
}
topicTemplate, err = template.New(name).Parse(string(templateSpec))
if err != nil {
logctx.WithError(err).Error(
"kafka producer setup, topic template parse fail")
return err, nil, nil
}
}
topic, err := nc.config.GetString(name, "topic")
if err != nil {
topic = KAFKATOPICDEFAULT
}
topicExtractor := kafkaProducerTopicExtractor(topic, topicTemplate)
keyopt, err := nc.config.GetString(name, "key")
if err != nil {
keyopt = ""
}
// Pick default output stream
err, streamSpec := dataMsgStreamSpecFromConfig(nc, name)
if err != nil {
logctx.WithError(err).Error(
"'encoding' option for kafka output")
return err, nil, nil
}
//
// If not set, will default to false, but let's be clear.
logData, _ := nc.config.GetBool(name, "logdata")
if err != nil {
logData = false
}
keySpec, ok := keyExtractionMethod[keyopt]
if !ok {
err = fmt.Errorf("kafka: key not a valid value")
logctx.WithError(err).WithFields(
log.Fields{
"brokers": brokerList,
"key": keyopt,
"expected one of": keyExtractionMethod}).Error(
"kafka producer setup")
return err, nil, nil
}
//
// Reliability setup
requiredAcksString, err := nc.config.GetString(name, "required_acks")
if err != nil {
// Explicit default
requiredAcks = sarama.NoResponse
} else {
switch {
case requiredAcksString == "none":
requiredAcks = sarama.NoResponse
case requiredAcksString == "local":
requiredAcks = sarama.WaitForLocal
case requiredAcksString == "commit":
requiredAcks = sarama.WaitForAll
default:
logctx.WithError(err).Error(
"'required_acks' option expects 'local' or 'commit'")
}
}
logctx = logctx.WithFields(
log.Fields{
"name": name,
"topic": topicExtractor(nil),
"brokers": brokerList,
"streamSpec": streamSpec,
"requiredAcks": requiredAcks,
})
//
// track config in struct
cfg := &kafkaProducerConfig{
name: name,
brokerList: brokerList,
fetchTopic: topicExtractor,
keySpec: keySpec,
streamSpec: streamSpec,
requiredAcks: requiredAcks,
logData: logData,
logCtx: logctx,
}
// Create the required channels; a sync ctrl channel and a data channel.
ctrlChan := make(chan *ctrlMsg)
dataChan := make(chan dataMsg, dataChannelDepth)
go cfg.kafkaFeederLoopSticky(dataChan, ctrlChan)
return nil, dataChan, ctrlChan
}
//
// Module implementing inputNodeModule interface.
type kafkaInputModule struct {
name string
}
func kafkaInputModuleNew() inputNodeModule {
return &kafkaInputModule{}
}
func (k *kafkaInputModule) handleCtrlMsg(
msg *ctrlMsg,
consumer *cluster.Consumer,
logCtx *log.Entry,
stats msgStats) bool {
done := false
resp := &ctrlMsg{
id: ACK,
respChan: nil,
}
switch msg.id {
case REPORT:
resp.content, _ = json.Marshal(stats)
case SHUTDOWN:
logCtx.Info("kafka consumer rxed SHUTDOWN")
if consumer != nil {
if err := consumer.Close(); err != nil {
logCtx.WithError(err).Error(
"kafka consumer shutdown")
}
}
done = true
default:
logCtx.Error("kafka consumer, unknown ctrl message")
}
msg.respChan <- resp
return done
}
type kafkaRemoteProducer struct {
remoteProducer string
}
func (s *kafkaRemoteProducer) String() string {
return s.remoteProducer
}
func (k *kafkaInputModule) extractKafkaProducer(
cfg *kafkaConsumerConfig,
msg *sarama.ConsumerMessage,
) msgproducer {
if msg.Key != nil && len(msg.Key) != 0 {
switch cfg.keySpec {
case KAFKA_KEY_SPEC_ID:
return &kafkaRemoteProducer{remoteProducer: string(msg.Key)}
case KAFKA_KEY_SPEC_ID_AND_PATH:
//
// key is in form path:id
keypart := strings.Split(string(msg.Key), ":")
if len(keypart) == 2 {
return &kafkaRemoteProducer{
remoteProducer: keypart[1],
}
}
}
}
//
// Last resort producer
return &kafkaRemoteProducer{remoteProducer: k.name}
}
func (k *kafkaInputModule) maintainKafkaConsumerConnection(
cfg *kafkaConsumerConfig,
ctrlChan <-chan *ctrlMsg,
dataChans []chan<- dataMsg,
) {
var consumer *cluster.Consumer
var client *cluster.Client
var notifications <-chan *cluster.Notification
var errorChan <-chan error
var msgChan <-chan *sarama.ConsumerMessage
var checkpointMsg *sarama.ConsumerMessage
var stats msgStats
//
// Timeout channel
timeout := make(chan bool, 1)
//
// Checkpoint offset timer
checkpoint_flush := make(chan bool, 1)
checkpoint_flush_scheduled := false
//
// Setup logging context once
logCtx := logger.WithFields(
log.Fields{
"name": k.name,
"topic": cfg.topic,
"group": cfg.consumerGroup,
"brokers": cfg.brokerList,
"encoding": encodingToName(cfg.msgEncoding),
})
//
// Setup codec according to configuration
err, codec := getCodec(k.name, cfg.msgEncoding)
if err != nil {
logCtx.WithError(err).Error(
"kafka consumer unsupported encoding (fatal)")
return
}
logCtx.Info("kafka consumer configured")
for {
//
// Get a default configuration
clusterConfig := cluster.NewConfig()
//
// Register to receive notifications on rebalancing of
// partition to consumer mapping. This allows us to
// have multiple consumers in consumer group, and
// allows us to adapt as we add and remove such
// consumers.
clusterConfig.Group.Return.Notifications = true
clusterConfig.Consumer.Return.Errors = true
client, err = cluster.NewClient(cfg.brokerList, clusterConfig)
if err != nil {
logCtx.WithError(err).Error(
"kafka consumer client connect (will retry)")
//
// We will setup restart further down if this fails.
}
if client != nil {
consumer, err = cluster.NewConsumerFromClient(
client, cfg.consumerGroup, []string{cfg.topic})
if err != nil {
logCtx.WithError(err).Error(
"kafka consumer create (will retry)")
}
}
if consumer != nil {
//
// Listen to notifications, for visibility only
notifications = consumer.Notifications()
//
// Listen to and log errors (eventually we may handle
// differently)
errorChan = consumer.Errors()
//
// Listen for messages... this is where we
// will do the heavy lifting bit.
msgChan = consumer.Messages()
logCtx.WithFields(log.Fields{
"subscriptions": consumer.Subscriptions(),
}).Info("kafka consumer connected")
} else {
//
// Go into select loop, with scheduled retry
// in a second. Note that this will make sure we
// remain responsive and handle cleanup signal
// correctly if necessary.
go func() {
time.Sleep(
KAFKARECONNECTTIMEOUT * time.Second)
timeout <- true
}()
}
restart := false
for {
select {
//
// Handle rebalancing, shutdown, and retry in
// this select clause
case remoteErr := <-errorChan:
logCtx.WithError(remoteErr).Error("kafka consumer rxed Error")
case restart_request := <-timeout:
// Handle timeout, simply restart the whole
// sequence trying to connect to kafka as
// consumer.
if restart_request {
logCtx.Debug("kafka consumer, attempt reconnect")
restart = true
}
case <-checkpoint_flush:
checkpoint_flush_scheduled = false
if checkpointMsg != nil {
consumer.MarkOffset(checkpointMsg, "ok")
}
checkpointMsg = nil
case data := <-msgChan:
checkpointMsg = data
if !checkpoint_flush_scheduled {
checkpoint_flush_scheduled = true
go func() {
time.Sleep(KAFKACHECKPOINTFLUSH * time.Second)
checkpoint_flush <- true
}()
}
err, dMs := codec.blockToDataMsgs(
k.extractKafkaProducer(cfg, data),
data.Value,
)
kafkaMetaMonitor.CountersMsgs.WithLabelValues(
cfg.name, cfg.topic, string(data.Key), "in").Inc()
kafkaMetaMonitor.CountersBytes.WithLabelValues(
cfg.name, cfg.topic, string(data.Key), "in").Add(
float64(len(data.Value)))
if err != nil || cfg.logData {
logMsgCtx := logCtx.WithFields(log.Fields{
"partition": data.Partition,
"offset": data.Offset,
"key": string(data.Key),
"len": len(data.Value),
})
if cfg.logData {
//
// Only add hex dump if logging data.
logMsgCtx = logMsgCtx.WithFields(log.Fields{
"content": hex.Dump(data.Value),
})
}
if err != nil {
stats.MsgsNOK++
logMsgCtx.WithError(err).Error(
"kafka consumer msg")
kafkaMetaMonitor.CountersErrors.WithLabelValues(
cfg.name, cfg.topic, string(data.Key), "in").Inc()
} else if cfg.logData {
stats.MsgsOK++
logMsgCtx.Debug("kafka consumer msg")
}
}
for _, dM := range dMs {
for i, dataChan := range dataChans {
if cap(dataChan) == len(dataChan) {
//
// Data channel full; blocking.
// If we choose to add tail drop option to avoid
// head-of-line blocking, this is where we would
// drop.
logCtx.WithFields(
log.Fields{"channel": i}).Debug(
"kafka consumer overrun (increase 'datachanneldepth'?)")
}
//
// Pass it on. Make sure we handle shutdown
// gracefully too.
select {
case dataChan <- dM:
continue
case msg := <-ctrlChan:
if k.handleCtrlMsg(msg, consumer, logCtx, stats) {
return
}
}
}
}
case notification := <-notifications:
//
// Rebalancing activity. Setup and
// teardown partition readers
// according to rebalance.
logCtx.WithFields(log.Fields{
"claimed": notification.Claimed,
"released": notification.Released,
"current": notification.Current,
}).Debug("kafka consumer notification")
case msg := <-ctrlChan:
if k.handleCtrlMsg(msg, consumer, logCtx, stats) {
return
}
}
if restart {
break
}
}
if consumer != nil {
if err := consumer.Close(); err != nil {
logCtx.WithError(err).Error(
"kafka consumer cleanup before retry")
}
}
//
// Ensure a flush does not wrap around consumer instances.
checkpointMsg = nil
consumer = nil
}
}
//
// Setup a kafka consumer. A fair bit of this setup is common between
// consumer and producer and should be shared.
func (k *kafkaInputModule) configure(
name string,
nc nodeConfig,
dataChans []chan<- dataMsg) (error, chan<- *ctrlMsg) {
k.name = name
brokers, err := nc.config.GetString(name, "brokers")
if err != nil {
logger.WithError(err).WithFields(
log.Fields{"name": name}).Error(
"kafka consumer requires 'brokers'")
return err, nil
}
brokerList := strings.Split(brokers, ",")
consumerGroup, err := nc.config.GetString(name, "consumergroup")
if err != nil {
logger.WithError(err).WithFields(
log.Fields{"name": name}).Error(
"kafka consumer requires 'consumergroup' identifying consumer")
return err, nil
}
topic, err := nc.config.GetString(name, "topic")
if err != nil {
topic = KAFKATOPICDEFAULT
}
// Pick default input stream type
enc := ENCODING_JSON
// Check for configuration override
encodingopt, err := nc.config.GetString(name, "encoding")
if err == nil {
err, enc = nameToEncoding(encodingopt)
if err != nil {
logger.WithError(err).WithFields(
log.Fields{"name": name}).Error(
"kafka consumer encoding option")
return err, nil
}
}
//
// If not set, will default to false, but let's be clear.
logData, err := nc.config.GetBool(name, "logdata")
if err != nil {
logData = false
}
keyopt, err := nc.config.GetString(name, "key")
if err != nil {
keyopt = ""
}
keySpec, ok := keyExtractionMethod[keyopt]
if !ok {
err = fmt.Errorf("kafka: key not a valid value")
logger.WithError(err).WithFields(
log.Fields{"name": name,
"brokers": brokerList,
"key": keyopt,
"expected one of": keyExtractionMethod}).Error(
"kafka consumer setup, unknown message key type")
return err, nil
}
//
// track config in struct
cfg := &kafkaConsumerConfig{
name: name,
topic: topic,
consumerGroup: consumerGroup,
brokerList: brokerList,
keySpec: keySpec,
msgEncoding: enc,
logData: logData,
}
//
// Setup logger in kafka library to feed ours for detailed
// logging. Would need to figure out how to do this for output
// module too. We handle sarama error messages already over error
// channel.
//
// sarama.Logger = logger
//
// Set up control channel and kick off sticky consumer
// connection
ctrlChan := make(chan *ctrlMsg)
go k.maintainKafkaConsumerConnection(cfg, ctrlChan, dataChans)
return nil, ctrlChan
}
type kafkaMetaMonitorType struct {
CountersMsgs *prometheus.CounterVec
CountersBytes *prometheus.CounterVec
CountersErrors *prometheus.CounterVec
}
var kafkaMetaMonitor *kafkaMetaMonitorType
func init() {
kafkaMetaMonitor = &kafkaMetaMonitorType{
CountersMsgs: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "kafka_messages",
Help: "Messages",
},
[]string{"section", "topic", "key", "dir"}),
CountersBytes: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "kafka_bytes",
Help: "Bytes",
},
[]string{"section", "topic", "key", "dir"}),
CountersErrors: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "kafka_errors",
Help: "Errors",
},
[]string{"section", "topic", "key", "dir"}),
}
// Dump content
prometheus.MustRegister(kafkaMetaMonitor.CountersMsgs)
prometheus.MustRegister(kafkaMetaMonitor.CountersBytes)
prometheus.MustRegister(kafkaMetaMonitor.CountersErrors)
}