2015-06-27 11:56:14 +08:00
|
|
|
package kafka_consumer
|
|
|
|
|
|
|
|
|
|
import (
|
2018-11-06 05:34:28 +08:00
|
|
|
"context"
|
2017-03-25 03:03:36 +08:00
|
|
|
"fmt"
|
2015-11-17 04:12:45 +08:00
|
|
|
"strings"
|
|
|
|
|
"sync"
|
2020-03-11 04:38:26 +08:00
|
|
|
"time"
|
2015-06-27 11:56:14 +08:00
|
|
|
|
2018-11-06 05:34:28 +08:00
|
|
|
"github.com/Shopify/sarama"
|
2021-10-27 23:48:57 +08:00
|
|
|
|
2016-01-28 05:21:36 +08:00
|
|
|
"github.com/influxdata/telegraf"
|
2021-10-29 04:35:22 +08:00
|
|
|
"github.com/influxdata/telegraf/config"
|
2020-03-11 04:38:26 +08:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2020-01-03 08:27:26 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/common/kafka"
|
2016-01-21 02:57:35 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2016-02-06 08:36:35 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers"
|
2018-11-06 05:34:28 +08:00
|
|
|
)
|
2015-11-17 04:12:45 +08:00
|
|
|
|
2019-07-30 11:41:12 +08:00
|
|
|
const (
|
|
|
|
|
defaultMaxUndeliveredMessages = 1000
|
2021-10-29 04:35:22 +08:00
|
|
|
defaultMaxProcessingTime = config.Duration(100 * time.Millisecond)
|
2019-07-30 11:41:12 +08:00
|
|
|
defaultConsumerGroup = "telegraf_metrics_consumers"
|
2020-03-11 04:38:26 +08:00
|
|
|
reconnectDelay = 5 * time.Second
|
2019-07-30 11:41:12 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type empty struct{}
|
|
|
|
|
type semaphore chan empty
|
|
|
|
|
|
|
|
|
|
type KafkaConsumer struct {
|
2021-10-29 04:35:22 +08:00
|
|
|
Brokers []string `toml:"brokers"`
|
|
|
|
|
ConsumerGroup string `toml:"consumer_group"`
|
|
|
|
|
MaxMessageLen int `toml:"max_message_len"`
|
|
|
|
|
MaxUndeliveredMessages int `toml:"max_undelivered_messages"`
|
|
|
|
|
MaxProcessingTime config.Duration `toml:"max_processing_time"`
|
|
|
|
|
Offset string `toml:"offset"`
|
|
|
|
|
BalanceStrategy string `toml:"balance_strategy"`
|
|
|
|
|
Topics []string `toml:"topics"`
|
|
|
|
|
TopicTag string `toml:"topic_tag"`
|
2020-10-29 00:16:59 +08:00
|
|
|
|
2020-11-24 04:51:58 +08:00
|
|
|
kafka.ReadConfig
|
2019-07-30 11:41:12 +08:00
|
|
|
|
2020-01-03 08:27:26 +08:00
|
|
|
Log telegraf.Logger `toml:"-"`
|
|
|
|
|
|
2019-07-30 11:41:12 +08:00
|
|
|
ConsumerCreator ConsumerGroupCreator `toml:"-"`
|
|
|
|
|
consumer ConsumerGroup
|
|
|
|
|
config *sarama.Config
|
|
|
|
|
|
|
|
|
|
parser parsers.Parser
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
cancel context.CancelFunc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ConsumerGroup interface {
|
|
|
|
|
Consume(ctx context.Context, topics []string, handler sarama.ConsumerGroupHandler) error
|
|
|
|
|
Errors() <-chan error
|
|
|
|
|
Close() error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ConsumerGroupCreator interface {
|
2021-12-01 05:59:24 +08:00
|
|
|
Create(brokers []string, group string, cfg *sarama.Config) (ConsumerGroup, error)
|
2019-07-30 11:41:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SaramaCreator struct{}
|
|
|
|
|
|
2021-12-01 05:59:24 +08:00
|
|
|
func (*SaramaCreator) Create(brokers []string, group string, cfg *sarama.Config) (ConsumerGroup, error) {
|
|
|
|
|
return sarama.NewConsumerGroup(brokers, group, cfg)
|
2019-07-30 11:41:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (k *KafkaConsumer) SetParser(parser parsers.Parser) {
|
2016-02-06 08:36:35 +08:00
|
|
|
k.parser = parser
|
2015-06-27 11:56:14 +08:00
|
|
|
}
|
|
|
|
|
|
2019-07-30 11:41:12 +08:00
|
|
|
func (k *KafkaConsumer) Init() error {
|
|
|
|
|
if k.MaxUndeliveredMessages == 0 {
|
|
|
|
|
k.MaxUndeliveredMessages = defaultMaxUndeliveredMessages
|
|
|
|
|
}
|
2021-10-29 04:35:22 +08:00
|
|
|
if time.Duration(k.MaxProcessingTime) == 0 {
|
|
|
|
|
k.MaxProcessingTime = defaultMaxProcessingTime
|
|
|
|
|
}
|
2019-07-30 11:41:12 +08:00
|
|
|
if k.ConsumerGroup == "" {
|
|
|
|
|
k.ConsumerGroup = defaultConsumerGroup
|
|
|
|
|
}
|
2015-06-27 11:56:14 +08:00
|
|
|
|
2021-12-01 05:59:24 +08:00
|
|
|
cfg := sarama.NewConfig()
|
2018-08-18 04:51:21 +08:00
|
|
|
|
2019-07-31 12:33:29 +08:00
|
|
|
// Kafka version 0.10.2.0 is required for consumer groups.
|
2021-12-01 05:59:24 +08:00
|
|
|
cfg.Version = sarama.V0_10_2_0
|
2019-07-31 12:33:29 +08:00
|
|
|
|
2021-12-01 05:59:24 +08:00
|
|
|
if err := k.SetConfig(cfg); err != nil {
|
2020-10-29 00:16:59 +08:00
|
|
|
return err
|
2017-06-08 09:22:28 +08:00
|
|
|
}
|
|
|
|
|
|
2015-11-17 04:12:45 +08:00
|
|
|
switch strings.ToLower(k.Offset) {
|
|
|
|
|
case "oldest", "":
|
2021-12-01 05:59:24 +08:00
|
|
|
cfg.Consumer.Offsets.Initial = sarama.OffsetOldest
|
2015-11-17 04:12:45 +08:00
|
|
|
case "newest":
|
2021-12-01 05:59:24 +08:00
|
|
|
cfg.Consumer.Offsets.Initial = sarama.OffsetNewest
|
2015-11-17 04:12:45 +08:00
|
|
|
default:
|
2019-07-30 11:41:12 +08:00
|
|
|
return fmt.Errorf("invalid offset %q", k.Offset)
|
2015-11-17 04:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
2019-11-28 02:54:29 +08:00
|
|
|
switch strings.ToLower(k.BalanceStrategy) {
|
|
|
|
|
case "range", "":
|
2021-12-01 05:59:24 +08:00
|
|
|
cfg.Consumer.Group.Rebalance.Strategy = sarama.BalanceStrategyRange
|
2019-11-28 02:54:29 +08:00
|
|
|
case "roundrobin":
|
2021-12-01 05:59:24 +08:00
|
|
|
cfg.Consumer.Group.Rebalance.Strategy = sarama.BalanceStrategyRoundRobin
|
2019-11-28 02:54:29 +08:00
|
|
|
case "sticky":
|
2021-12-01 05:59:24 +08:00
|
|
|
cfg.Consumer.Group.Rebalance.Strategy = sarama.BalanceStrategySticky
|
2019-11-28 02:54:29 +08:00
|
|
|
default:
|
|
|
|
|
return fmt.Errorf("invalid balance strategy %q", k.BalanceStrategy)
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-30 11:41:12 +08:00
|
|
|
if k.ConsumerCreator == nil {
|
|
|
|
|
k.ConsumerCreator = &SaramaCreator{}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-01 05:59:24 +08:00
|
|
|
cfg.Consumer.MaxProcessingTime = time.Duration(k.MaxProcessingTime)
|
2021-10-29 04:35:22 +08:00
|
|
|
|
2021-12-01 05:59:24 +08:00
|
|
|
k.config = cfg
|
2019-07-30 11:41:12 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (k *KafkaConsumer) Start(acc telegraf.Accumulator) error {
|
|
|
|
|
var err error
|
|
|
|
|
k.consumer, err = k.ConsumerCreator.Create(
|
|
|
|
|
k.Brokers,
|
|
|
|
|
k.ConsumerGroup,
|
|
|
|
|
k.config,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2015-06-27 11:56:14 +08:00
|
|
|
}
|
|
|
|
|
|
2018-11-06 05:34:28 +08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
k.cancel = cancel
|
|
|
|
|
|
|
|
|
|
// Start consumer goroutine
|
|
|
|
|
k.wg.Add(1)
|
|
|
|
|
go func() {
|
|
|
|
|
defer k.wg.Done()
|
2019-07-30 11:41:12 +08:00
|
|
|
for ctx.Err() == nil {
|
2021-10-27 23:48:57 +08:00
|
|
|
handler := NewConsumerGroupHandler(acc, k.MaxUndeliveredMessages, k.parser, k.Log)
|
2019-07-30 11:41:12 +08:00
|
|
|
handler.MaxMessageLen = k.MaxMessageLen
|
|
|
|
|
handler.TopicTag = k.TopicTag
|
|
|
|
|
err := k.consumer.Consume(ctx, k.Topics, handler)
|
|
|
|
|
if err != nil {
|
|
|
|
|
acc.AddError(err)
|
2021-04-09 00:43:39 +08:00
|
|
|
// Ignore returned error as we cannot do anything about it anyway
|
|
|
|
|
//nolint:errcheck,revive
|
2020-03-11 04:38:26 +08:00
|
|
|
internal.SleepContext(ctx, reconnectDelay)
|
2019-07-30 11:41:12 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
err = k.consumer.Close()
|
|
|
|
|
if err != nil {
|
|
|
|
|
acc.AddError(err)
|
|
|
|
|
}
|
2018-11-06 05:34:28 +08:00
|
|
|
}()
|
|
|
|
|
|
2019-07-30 11:41:12 +08:00
|
|
|
k.wg.Add(1)
|
|
|
|
|
go func() {
|
|
|
|
|
defer k.wg.Done()
|
|
|
|
|
for err := range k.consumer.Errors() {
|
|
|
|
|
acc.AddError(err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 01:21:36 +08:00
|
|
|
func (k *KafkaConsumer) Gather(_ telegraf.Accumulator) error {
|
2015-11-17 04:12:45 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
2015-06-27 11:56:14 +08:00
|
|
|
|
2019-07-30 11:41:12 +08:00
|
|
|
func (k *KafkaConsumer) Stop() {
|
|
|
|
|
k.cancel()
|
|
|
|
|
k.wg.Wait()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Message is an aggregate type binding the Kafka message and the session so
|
|
|
|
|
// that offsets can be updated.
|
|
|
|
|
type Message struct {
|
|
|
|
|
message *sarama.ConsumerMessage
|
|
|
|
|
session sarama.ConsumerGroupSession
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 23:48:57 +08:00
|
|
|
func NewConsumerGroupHandler(acc telegraf.Accumulator, maxUndelivered int, parser parsers.Parser, log telegraf.Logger) *ConsumerGroupHandler {
|
2019-07-30 11:41:12 +08:00
|
|
|
handler := &ConsumerGroupHandler{
|
|
|
|
|
acc: acc.WithTracking(maxUndelivered),
|
|
|
|
|
sem: make(chan empty, maxUndelivered),
|
|
|
|
|
undelivered: make(map[telegraf.TrackingID]Message, maxUndelivered),
|
|
|
|
|
parser: parser,
|
2021-10-27 23:48:57 +08:00
|
|
|
log: log,
|
2019-07-30 11:41:12 +08:00
|
|
|
}
|
|
|
|
|
return handler
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ConsumerGroupHandler is a sarama.ConsumerGroupHandler implementation.
|
|
|
|
|
type ConsumerGroupHandler struct {
|
|
|
|
|
MaxMessageLen int
|
|
|
|
|
TopicTag string
|
|
|
|
|
|
|
|
|
|
acc telegraf.TrackingAccumulator
|
|
|
|
|
sem semaphore
|
|
|
|
|
parser parsers.Parser
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
cancel context.CancelFunc
|
|
|
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
|
undelivered map[telegraf.TrackingID]Message
|
2021-10-27 23:48:57 +08:00
|
|
|
|
|
|
|
|
log telegraf.Logger
|
2019-07-30 11:41:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Setup is called once when a new session is opened. It setups up the handler
|
|
|
|
|
// and begins processing delivered messages.
|
|
|
|
|
func (h *ConsumerGroupHandler) Setup(sarama.ConsumerGroupSession) error {
|
|
|
|
|
h.undelivered = make(map[telegraf.TrackingID]Message)
|
|
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
h.cancel = cancel
|
2018-11-06 05:34:28 +08:00
|
|
|
|
2019-07-30 11:41:12 +08:00
|
|
|
h.wg.Add(1)
|
|
|
|
|
go func() {
|
|
|
|
|
defer h.wg.Done()
|
|
|
|
|
h.run(ctx)
|
|
|
|
|
}()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-11-06 05:34:28 +08:00
|
|
|
|
2019-07-30 11:41:12 +08:00
|
|
|
// Run processes any delivered metrics during the lifetime of the session.
|
2021-03-23 01:21:36 +08:00
|
|
|
func (h *ConsumerGroupHandler) run(ctx context.Context) {
|
2015-06-27 11:56:14 +08:00
|
|
|
for {
|
|
|
|
|
select {
|
2018-11-06 05:34:28 +08:00
|
|
|
case <-ctx.Done():
|
2021-03-23 01:21:36 +08:00
|
|
|
return
|
2019-07-30 11:41:12 +08:00
|
|
|
case track := <-h.acc.Delivered():
|
|
|
|
|
h.onDelivery(track)
|
2015-06-27 11:56:14 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-30 11:41:12 +08:00
|
|
|
func (h *ConsumerGroupHandler) onDelivery(track telegraf.DeliveryInfo) {
|
|
|
|
|
h.mu.Lock()
|
|
|
|
|
defer h.mu.Unlock()
|
|
|
|
|
|
|
|
|
|
msg, ok := h.undelivered[track.ID()]
|
|
|
|
|
if !ok {
|
2021-10-27 23:48:57 +08:00
|
|
|
h.log.Errorf("Could not mark message delivered: %d", track.ID())
|
2019-07-30 11:41:12 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if track.Delivered() {
|
|
|
|
|
msg.session.MarkMessage(msg.message, "")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete(h.undelivered, track.ID())
|
|
|
|
|
<-h.sem
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reserve blocks until there is an available slot for a new message.
|
|
|
|
|
func (h *ConsumerGroupHandler) Reserve(ctx context.Context) error {
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return ctx.Err()
|
|
|
|
|
case h.sem <- empty{}:
|
|
|
|
|
return nil
|
2018-11-06 05:34:28 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-30 11:41:12 +08:00
|
|
|
func (h *ConsumerGroupHandler) release() {
|
|
|
|
|
<-h.sem
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle processes a message and if successful saves it to be acknowledged
|
|
|
|
|
// after delivery.
|
|
|
|
|
func (h *ConsumerGroupHandler) Handle(session sarama.ConsumerGroupSession, msg *sarama.ConsumerMessage) error {
|
|
|
|
|
if h.MaxMessageLen != 0 && len(msg.Value) > h.MaxMessageLen {
|
|
|
|
|
session.MarkMessage(msg, "")
|
|
|
|
|
h.release()
|
|
|
|
|
return fmt.Errorf("message exceeds max_message_len (actual %d, max %d)",
|
|
|
|
|
len(msg.Value), h.MaxMessageLen)
|
2018-11-06 05:34:28 +08:00
|
|
|
}
|
|
|
|
|
|
2019-07-30 11:41:12 +08:00
|
|
|
metrics, err := h.parser.Parse(msg.Value)
|
2018-11-06 05:34:28 +08:00
|
|
|
if err != nil {
|
2019-07-30 11:41:12 +08:00
|
|
|
h.release()
|
2018-11-06 05:34:28 +08:00
|
|
|
return err
|
|
|
|
|
}
|
2019-07-30 11:41:12 +08:00
|
|
|
|
|
|
|
|
if len(h.TopicTag) > 0 {
|
2018-11-29 08:29:26 +08:00
|
|
|
for _, metric := range metrics {
|
2019-07-30 11:41:12 +08:00
|
|
|
metric.AddTag(h.TopicTag, msg.Topic)
|
2018-11-29 08:29:26 +08:00
|
|
|
}
|
|
|
|
|
}
|
2018-11-06 05:34:28 +08:00
|
|
|
|
2019-07-30 11:41:12 +08:00
|
|
|
h.mu.Lock()
|
2019-09-07 03:35:33 +08:00
|
|
|
id := h.acc.AddTrackingMetricGroup(metrics)
|
2019-07-30 11:41:12 +08:00
|
|
|
h.undelivered[id] = Message{session: session, message: msg}
|
|
|
|
|
h.mu.Unlock()
|
2018-11-06 05:34:28 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-30 11:41:12 +08:00
|
|
|
// ConsumeClaim is called once each claim in a goroutine and must be
|
|
|
|
|
// thread-safe. Should run until the claim is closed.
|
|
|
|
|
func (h *ConsumerGroupHandler) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
|
|
|
|
|
ctx := session.Context()
|
2018-11-06 05:34:28 +08:00
|
|
|
|
2019-07-30 11:41:12 +08:00
|
|
|
for {
|
|
|
|
|
err := h.Reserve(ctx)
|
|
|
|
|
if err != nil {
|
2021-04-09 00:43:39 +08:00
|
|
|
return err
|
2019-07-30 11:41:12 +08:00
|
|
|
}
|
2018-11-06 05:34:28 +08:00
|
|
|
|
2019-07-30 11:41:12 +08:00
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return nil
|
|
|
|
|
case msg, ok := <-claim.Messages():
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
err := h.Handle(session, msg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.acc.AddError(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-17 04:12:45 +08:00
|
|
|
}
|
|
|
|
|
}
|
2015-06-27 11:56:14 +08:00
|
|
|
|
2019-07-30 11:41:12 +08:00
|
|
|
// Cleanup stops the internal goroutine and is called after all ConsumeClaim
|
|
|
|
|
// functions have completed.
|
|
|
|
|
func (h *ConsumerGroupHandler) Cleanup(sarama.ConsumerGroupSession) error {
|
|
|
|
|
h.cancel()
|
|
|
|
|
h.wg.Wait()
|
2015-11-17 04:12:45 +08:00
|
|
|
return nil
|
2015-06-27 11:56:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
2016-01-28 05:21:36 +08:00
|
|
|
inputs.Add("kafka_consumer", func() telegraf.Input {
|
2019-07-30 11:41:12 +08:00
|
|
|
return &KafkaConsumer{}
|
2015-06-27 11:56:14 +08:00
|
|
|
})
|
|
|
|
|
}
|