-
Notifications
You must be signed in to change notification settings - Fork 41
/
kafka.go
161 lines (139 loc) · 3.56 KB
/
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
package kafka
import (
"bytes"
"fmt"
"log"
"os"
"strconv"
"strings"
"text/template"
"time"
"github.com/gliderlabs/logspout/router"
"gopkg.in/Shopify/sarama.v1"
)
func init() {
router.AdapterFactories.Register(NewKafkaAdapter, "kafka")
}
type KafkaAdapter struct {
route *router.Route
brokers []string
topic string
producer sarama.AsyncProducer
tmpl *template.Template
}
func NewKafkaAdapter(route *router.Route) (router.LogAdapter, error) {
brokers := readBrokers(route.Address)
if len(brokers) == 0 {
return nil, errorf("The Kafka broker host:port is missing. Did you specify it as a route address?")
}
topic := readTopic(route.Address, route.Options)
if topic == "" {
return nil, errorf("The Kafka topic is missing. Did you specify it as a route option?")
}
var err error
var tmpl *template.Template
if text := os.Getenv("KAFKA_TEMPLATE"); text != "" {
tmpl, err = template.New("kafka").Parse(text)
if err != nil {
return nil, errorf("Couldn't parse Kafka message template. %v", err)
}
}
if os.Getenv("DEBUG") != "" {
log.Printf("Starting Kafka producer for address: %s, topic: %s.\n", brokers, topic)
}
var retries int
retries, err = strconv.Atoi(os.Getenv("KAFKA_CONNECT_RETRIES"))
if err != nil {
retries = 3
}
var producer sarama.AsyncProducer
for i := 0; i < retries; i++ {
producer, err = sarama.NewAsyncProducer(brokers, newConfig())
if err != nil {
if os.Getenv("DEBUG") != "" {
log.Println("Couldn't create Kafka producer. Retrying...", err)
}
if i == retries-1 {
return nil, errorf("Couldn't create Kafka producer. %v", err)
}
} else {
time.Sleep(1 * time.Second)
}
}
return &KafkaAdapter{
route: route,
brokers: brokers,
topic: topic,
producer: producer,
tmpl: tmpl,
}, nil
}
func (a *KafkaAdapter) Stream(logstream chan *router.Message) {
defer a.producer.Close()
for rm := range logstream {
message, err := a.formatMessage(rm)
if err != nil {
log.Println("kafka:", err)
a.route.Close()
break
}
a.producer.Input() <- message
}
}
func newConfig() *sarama.Config {
config := sarama.NewConfig()
config.ClientID = "logspout"
config.Producer.Return.Errors = false
config.Producer.Return.Successes = false
config.Producer.Flush.Frequency = 1 * time.Second
config.Producer.RequiredAcks = sarama.WaitForLocal
if opt := os.Getenv("KAFKA_COMPRESSION_CODEC"); opt != "" {
switch opt {
case "gzip":
config.Producer.Compression = sarama.CompressionGZIP
case "snappy":
config.Producer.Compression = sarama.CompressionSnappy
}
}
return config
}
func (a *KafkaAdapter) formatMessage(message *router.Message) (*sarama.ProducerMessage, error) {
var encoder sarama.Encoder
if a.tmpl != nil {
var w bytes.Buffer
if err := a.tmpl.Execute(&w, message); err != nil {
return nil, err
}
encoder = sarama.ByteEncoder(w.Bytes())
} else {
encoder = sarama.StringEncoder(message.Data)
}
return &sarama.ProducerMessage{
Topic: a.topic,
Value: encoder,
}, nil
}
func readBrokers(address string) []string {
if strings.Contains(address, "/") {
slash := strings.Index(address, "/")
address = address[:slash]
}
return strings.Split(address, ",")
}
func readTopic(address string, options map[string]string) string {
var topic string
if !strings.Contains(address, "/") {
topic = options["topic"]
} else {
slash := strings.Index(address, "/")
topic = address[slash+1:]
}
return topic
}
func errorf(format string, a ...interface{}) (err error) {
err = fmt.Errorf(format, a...)
if os.Getenv("DEBUG") != "" {
fmt.Println(err.Error())
}
return
}