forked from beatlabs/patron
/
kafka.go
259 lines (219 loc) · 6.11 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
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
package kafka
import (
"context"
"fmt"
"os"
"strconv"
"github.com/Shopify/sarama"
"github.com/mantzas/patron/async"
"github.com/mantzas/patron/encoding"
"github.com/mantzas/patron/log"
"github.com/mantzas/patron/trace"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
)
type message struct {
span opentracing.Span
ctx context.Context
dec encoding.DecodeRawFunc
val []byte
}
func (m *message) Context() context.Context {
return m.ctx
}
func (m *message) Decode(v interface{}) error {
return m.dec(m.val, v)
}
func (m *message) Ack() error {
trace.SpanSuccess(m.span)
return nil
}
func (m *message) Nack() error {
trace.SpanError(m.span)
return nil
}
// Offset defines the offset of messages inside a topic.
type Offset int64
const (
// OffsetNewest starts consuming from the newest available message in the topic.
OffsetNewest Offset = -1
// OffsetOldest starts consuming from the oldest available message in the topic.
OffsetOldest Offset = -2
)
var topicPartitionOffsetDiff *prometheus.GaugeVec
// Consumer definition of a Kafka consumer.
type Consumer struct {
brokers []string
topic string
buffer int
start Offset
cfg *sarama.Config
contentType string
cnl context.CancelFunc
ms sarama.Consumer
}
// New creates a ew Kafka consumer with defaults. To override those default you should provide a option.
func New(name, ct, topic string, brokers []string, oo ...OptionFunc) (*Consumer, error) {
if name == "" {
return nil, errors.New("name is required")
}
if len(brokers) == 0 {
return nil, errors.New("provide at least one broker")
}
if topic == "" {
return nil, errors.New("topic is required")
}
host, err := os.Hostname()
if err != nil {
return nil, errors.New("failed to get hostname")
}
config := sarama.NewConfig()
config.ClientID = fmt.Sprintf("%s-%s", host, name)
config.Consumer.Return.Errors = true
config.Version = sarama.V0_11_0_0
c := &Consumer{
brokers: brokers,
topic: topic,
cfg: config,
contentType: ct,
buffer: 1000,
start: OffsetNewest,
}
for _, o := range oo {
err = o(c)
if err != nil {
return nil, err
}
}
err = setupMetrics(name)
if err != nil {
return nil, err
}
return c, nil
}
// Consume starts consuming messages from a Kafka topic.
func (c *Consumer) Consume(ctx context.Context) (<-chan async.Message, <-chan error, error) {
ctx, cnl := context.WithCancel(ctx)
c.cnl = cnl
pcs, err := c.consumers()
if err != nil {
return nil, nil, errors.Wrap(err, "failed to get partitions")
}
log.Infof("consuming messages for topic '%s'", c.topic)
chMsg := make(chan async.Message, c.buffer)
chErr := make(chan error, c.buffer)
for _, pc := range pcs {
go func(consumer sarama.PartitionConsumer) {
for {
select {
case <-ctx.Done():
log.Info("canceling consuming messages requested")
return
case consumerError := <-consumer.Errors():
chErr <- consumerError
case m := <-consumer.Messages():
log.Debugf("data received from topic %s", m.Topic)
topicPartitionOffsetDiffGaugeSet(m.Topic, m.Partition, consumer.HighWaterMarkOffset(), m.Offset)
go func(msg *sarama.ConsumerMessage) {
sp, chCtx := trace.ConsumerSpan(
ctx,
trace.ComponentOpName(trace.KafkaConsumerComponent, msg.Topic),
trace.KafkaConsumerComponent,
mapHeader(msg.Headers),
)
var ct string
if c.contentType != "" {
ct = c.contentType
} else {
ct, err = determineContentType(msg.Headers)
if err != nil {
trace.SpanError(sp)
chErr <- errors.Wrap(err, "failed to determine content type")
return
}
}
dec, err := async.DetermineDecoder(ct)
if err != nil {
trace.SpanError(sp)
chErr <- errors.Wrapf(err, "failed to determine decoder for %s", ct)
return
}
chMsg <- &message{
ctx: chCtx,
dec: dec,
span: sp,
val: msg.Value,
}
}(m)
}
}
}(pc)
}
return chMsg, chErr, nil
}
// Close handles closing channel and connection of AMQP.
func (c *Consumer) Close() error {
if c.ms == nil {
return nil
}
return errors.Wrap(c.ms.Close(), "failed to close consumer")
}
func (c *Consumer) consumers() ([]sarama.PartitionConsumer, error) {
ms, err := sarama.NewConsumer(c.brokers, c.cfg)
if err != nil {
return nil, errors.Wrap(err, "failed to create consumer")
}
c.ms = ms
partitions, err := c.ms.Partitions(c.topic)
if err != nil {
return nil, errors.Wrap(err, "failed to get partitions")
}
pcs := make([]sarama.PartitionConsumer, len(partitions))
for i, partition := range partitions {
pc, err := c.ms.ConsumePartition(c.topic, partition, int64(c.start))
if nil != err {
return nil, errors.Wrap(err, "failed to get partition consumer")
}
pcs[i] = pc
}
return pcs, nil
}
func determineContentType(hdr []*sarama.RecordHeader) (string, error) {
for _, h := range hdr {
if string(h.Key) == encoding.ContentTypeHeader {
return string(h.Value), nil
}
}
return "", errors.New("content type header is missing")
}
func mapHeader(hh []*sarama.RecordHeader) map[string]string {
mp := make(map[string]string)
for _, h := range hh {
mp[string(h.Key)] = string(h.Value)
}
return mp
}
func setupMetrics(namespace string) error {
if topicPartitionOffsetDiff != nil {
return nil
}
topicPartitionOffsetDiff = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "kafka_consumer",
Name: "offset_diff",
Help: "Message offset difference with high watermark, classified by topic and partition",
},
[]string{"topic", "partition"},
)
if err := prometheus.Register(topicPartitionOffsetDiff); err != nil {
if _, ok := err.(prometheus.AlreadyRegisteredError); ok {
return errors.Wrap(err, "failed to register kafka consumer metrics")
}
}
return nil
}
func topicPartitionOffsetDiffGaugeSet(topic string, partition int32, high, offset int64) {
topicPartitionOffsetDiff.WithLabelValues(topic, strconv.FormatInt(int64(partition), 10)).Set(float64(high - offset))
}