This repository has been archived by the owner on May 19, 2022. It is now read-only.
forked from carbonblack/cb-event-forwarder
-
Notifications
You must be signed in to change notification settings - Fork 3
/
kafka_output.go
134 lines (104 loc) · 2.52 KB
/
kafka_output.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
package main
import (
"encoding/json"
"sync"
"time"
"fmt"
"github.com/Shopify/sarama"
log "github.com/sirupsen/logrus"
"os"
"os/signal"
"strings"
"sync/atomic"
"syscall"
)
type KafkaOutput struct {
brokers []string
topicSuffix string
producer sarama.AsyncProducer
droppedEventCount int64
eventSentCount int64
sync.RWMutex
}
type KafkaStatistics struct {
DroppedEventCount int64 `json:"dropped_event_count"`
EventSentCount int64 `json:"event_sent_count"`
}
func (o *KafkaOutput) Initialize(unused string) error {
o.Lock()
defer o.Unlock()
o.brokers = strings.Split(*config.KafkaBrokers, ",")
o.topicSuffix = *config.KafkaTopicSuffix
kafkaConfig := sarama.NewConfig()
kafkaConfig.Producer.Return.Successes = true
producer, err := sarama.NewAsyncProducer(o.brokers, kafkaConfig)
if err != nil {
panic(err)
}
o.producer = producer
return nil
}
func (o *KafkaOutput) Go(messages <-chan string, errorChan chan<- error) error {
go func() {
refreshTicker := time.NewTicker(1 * time.Second)
defer refreshTicker.Stop()
defer func() {
if err := o.producer.Close(); err != nil {
log.Fatalln(err)
}
}()
hup := make(chan os.Signal, 1)
signal.Notify(hup, syscall.SIGHUP)
defer signal.Stop(hup)
for {
select {
case message := <-messages:
var parsedMsg map[string]interface{}
json.Unmarshal([]byte(message), &parsedMsg)
topic := parsedMsg["type"]
if topicString, ok := topic.(string); ok {
topicString = strings.Replace(topicString, "ingress.event.", "", -1)
topicString += o.topicSuffix
o.output(topicString, message)
} else {
log.Info("ERROR: Topic was not a string")
}
}
}
}()
go func() {
for range o.producer.Successes() {
atomic.AddInt64(&o.eventSentCount, 1)
}
}()
go func() {
for err := range o.producer.Errors() {
log.Info(err)
atomic.AddInt64(&o.droppedEventCount, 1)
errorChan <- err
}
}()
return nil
}
func (o *KafkaOutput) Statistics() interface{} {
o.RLock()
defer o.RUnlock()
return KafkaStatistics{DroppedEventCount: o.droppedEventCount, EventSentCount: o.eventSentCount}
}
func (o *KafkaOutput) String() string {
o.RLock()
defer o.RUnlock()
return fmt.Sprintf("Brokers %s", o.brokers)
}
func (o *KafkaOutput) Key() string {
o.RLock()
defer o.RUnlock()
return fmt.Sprintf("brokers:%s", o.brokers)
}
func (o *KafkaOutput) output(topic string, m string) {
o.producer.Input() <- &sarama.ProducerMessage{
Topic: topic,
Key: nil,
Value: sarama.StringEncoder(m),
}
}