-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkafka_handler.go
More file actions
76 lines (64 loc) · 1.87 KB
/
Copy pathkafka_handler.go
File metadata and controls
76 lines (64 loc) · 1.87 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
package main
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"log"
"github.com/Shopify/sarama"
)
// SendingToKafka -
func SendingToKafka(jsonFlowChanel chan string) {
defer RecoverAnyPanic("SendingToKafka")
saramaConfig := sarama.NewConfig()
saramaConfig.Producer.RequiredAcks = sarama.WaitForAll
saramaConfig.Producer.Retry.Max = 5
saramaConfig.Producer.Return.Successes = true
if Config.Output.Kafka.TLS.Enabled {
saramaConfig.Net.TLS.Config = createTLSConfiguration()
saramaConfig.Net.TLS.Enable = true
}
producer, err := sarama.NewSyncProducer(Config.Output.Kafka.BrokerList, saramaConfig)
if err != nil {
ExitOnError("SendingToKafka", err)
}
defer func() {
if err := producer.Close(); err != nil {
ExitOnError("SendingToKafka", err)
}
}()
for {
msg := &sarama.ProducerMessage{
Topic: Config.Output.Kafka.Topic,
Value: sarama.StringEncoder(<-jsonFlowChanel),
}
partition, offset, err := producer.SendMessage(msg)
if err != nil {
LogOnError("SendingToKafka", err)
}
if offset == 0 {
log.Printf("First flow stored in topic(%s)/partition(%d)/offset(%d)\n", Config.Output.Kafka.Topic, partition, offset)
} else if offset%100 == 0 {
log.Printf("Another 100 flows stored in topic(%s)/partition(%d)/offset(%d)\n", Config.Output.Kafka.Topic, partition, offset)
}
}
}
func createTLSConfiguration() (t *tls.Config) {
cert, err := tls.LoadX509KeyPair(
Config.Output.Kafka.TLS.CertFilePath,
Config.Output.Kafka.TLS.KeyFilePath)
if err != nil {
ExitOnError("createTLSConfiguration", err)
}
caCert, err := ioutil.ReadFile(Config.Output.Kafka.TLS.CAFilePath)
if err != nil {
ExitOnError("createTLSConfiguration", err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
t = &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
InsecureSkipVerify: false,
}
return t
}