-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkafka-output.go
134 lines (105 loc) · 2.78 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 (
"fmt"
"net/http"
"strings"
"time"
"github.com/IBM/sarama"
"github.com/ninlil/butler/log"
)
type kafkaProducer struct {
msg sarama.SyncProducer
connected bool
ID string
Topic string
Endpoint *kafkaEndpoint
Auth *Auth
}
func (p *kafkaProducer) Name() string {
return p.ID
}
func (p *kafkaProducer) Validate() ([]error, bool) {
var errs []error
if p.Endpoint.err != nil {
errs = append(errs, p.Endpoint.err)
}
if (p.Auth.Bearer == "") && (len(p.Auth.Basic) == 0) && !p.Auth.Anonymous {
errs = append(errs, stringError("'auth' must have either 'anon' or 'bearer/basic'"))
}
if p.ID == "" {
errs = append(errs, requiredError(ID))
}
if p.Topic == "" {
errs = append(errs, requiredError(TOPIC))
}
return errs, true
}
// Connect to a Kafka server
func (p *kafkaProducer) Connect() (Producer, error) {
log.Info().Msgf("%s: connecting...", p.ID)
cfg := sarama.NewConfig()
cfg.ClientID = fmt.Sprintf("melp-sender-%s", p.ID)
cfg.Producer.RequiredAcks = sarama.WaitForLocal
cfg.Producer.Retry.Max = 10
cfg.Producer.Return.Successes = true
cfg.Producer.Compression = sarama.CompressionSnappy
//cfg.Producer.Flush.Frequency = 500 * time.Millisecond
p.Endpoint.SetConfig(cfg)
var list = p.Endpoint.Peers()
producer, err := sarama.NewSyncProducer(list, cfg)
if err != nil {
return nil, err
}
p.msg = producer
return p, nil
}
func (p *kafkaProducer) Close() error {
if !p.connected {
return nil
}
err := p.msg.Close()
p.connected = false
return err
}
func (p *kafkaProducer) Authorize(r *http.Request) (bool, error) {
if p.Auth == nil {
return true, nil
}
return p.Auth.Validate(r)
}
type kafkaSendResponse struct {
Partition int32 `json:"partition"`
Offset int64 `json:"offset"`
}
func (p *kafkaProducer) Send(msg Message, r *http.Request) (interface{}, error) {
var hdrs = make([]sarama.RecordHeader, 0, len(msg.Headers))
var key sarama.Encoder = nil
for k, v := range msg.Headers {
if strings.EqualFold(k, PartitionKey) {
key = sarama.StringEncoder(v)
} else {
hdrs = append(hdrs, sarama.RecordHeader{
Key: []byte(k),
Value: []byte(v),
})
}
}
var pkg = sarama.ProducerMessage{
Topic: p.Topic,
Headers: hdrs,
Timestamp: time.Now().UTC(),
Key: key,
Value: sarama.ByteEncoder(msg.Body),
}
partition, offset, err := p.msg.SendMessage(&pkg)
if err != nil {
return nil, err
}
log.Trace().Msgf("%s: msg sent: %d/%d", p.ID, partition, offset)
metrics.Send(p.Topic, partition, len(msg.Body))
log.AddRequestFields(r, "partition", partition, "offset", offset)
return &kafkaSendResponse{
Partition: partition,
Offset: offset,
}, nil
}