2015-06-27 03:56:14 +00:00
|
|
|
package kafka_consumer
|
|
|
|
|
|
|
|
import (
|
2018-11-05 21:34:28 +00:00
|
|
|
"context"
|
2017-03-24 19:03:36 +00:00
|
|
|
"fmt"
|
2015-11-16 20:12:45 +00:00
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
"github.com/Shopify/sarama"
|
|
|
|
cluster "github.com/bsm/sarama-cluster"
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2018-05-04 23:33:23 +00:00
|
|
|
"github.com/influxdata/telegraf/internal/tls"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2016-02-06 00:36:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers"
|
2018-11-05 21:34:28 +00:00
|
|
|
)
|
2015-11-16 20:12:45 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
const (
|
|
|
|
defaultMaxUndeliveredMessages = 1000
|
2015-06-27 03:56:14 +00:00
|
|
|
)
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
type empty struct{}
|
|
|
|
type semaphore chan empty
|
2017-06-08 01:22:28 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
type Consumer interface {
|
|
|
|
Errors() <-chan error
|
|
|
|
Messages() <-chan *sarama.ConsumerMessage
|
|
|
|
MarkOffset(msg *sarama.ConsumerMessage, metadata string)
|
|
|
|
Close() error
|
|
|
|
}
|
2017-06-08 01:22:28 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
type Kafka struct {
|
|
|
|
ConsumerGroup string `toml:"consumer_group"`
|
|
|
|
ClientID string `toml:"client_id"`
|
|
|
|
Topics []string `toml:"topics"`
|
|
|
|
Brokers []string `toml:"brokers"`
|
|
|
|
MaxMessageLen int `toml:"max_message_len"`
|
|
|
|
Version string `toml:"version"`
|
|
|
|
MaxUndeliveredMessages int `toml:"max_undelivered_messages"`
|
|
|
|
Offset string `toml:"offset"`
|
|
|
|
SASLUsername string `toml:"sasl_username"`
|
|
|
|
SASLPassword string `toml:"sasl_password"`
|
2018-05-04 23:33:23 +00:00
|
|
|
tls.ClientConfig
|
2017-06-08 01:22:28 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
cluster Consumer
|
|
|
|
parser parsers.Parser
|
|
|
|
wg *sync.WaitGroup
|
|
|
|
cancel context.CancelFunc
|
2016-02-06 00:36:35 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
// Unconfirmed messages
|
|
|
|
messages map[telegraf.TrackingID]*sarama.ConsumerMessage
|
2015-11-16 20:12:45 +00:00
|
|
|
|
|
|
|
// doNotCommitMsgs tells the parser not to call CommitUpTo on the consumer
|
|
|
|
// this is mostly for test purposes, but there may be a use-case for it later.
|
|
|
|
doNotCommitMsgs bool
|
2015-06-27 03:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2017-06-08 01:22:28 +00:00
|
|
|
## kafka servers
|
|
|
|
brokers = ["localhost:9092"]
|
2016-02-18 21:26:51 +00:00
|
|
|
## topic(s) to consume
|
2015-11-16 20:12:45 +00:00
|
|
|
topics = ["telegraf"]
|
2018-07-13 20:59:45 +00:00
|
|
|
|
2018-07-13 20:53:56 +00:00
|
|
|
## Optional Client id
|
|
|
|
# client_id = "Telegraf"
|
2017-06-08 01:22:28 +00:00
|
|
|
|
2018-08-17 20:51:21 +00:00
|
|
|
## Set the minimal supported Kafka version. Setting this enables the use of new
|
|
|
|
## Kafka features and APIs. Of particular interest, lz4 compression
|
|
|
|
## requires at least version 0.10.0.0.
|
|
|
|
## ex: version = "1.1.0"
|
|
|
|
# version = ""
|
|
|
|
|
2018-05-04 23:33:23 +00:00
|
|
|
## Optional TLS Config
|
|
|
|
# tls_ca = "/etc/telegraf/ca.pem"
|
|
|
|
# tls_cert = "/etc/telegraf/cert.pem"
|
|
|
|
# tls_key = "/etc/telegraf/key.pem"
|
|
|
|
## Use TLS but skip chain & host verification
|
2017-06-08 01:22:28 +00:00
|
|
|
# insecure_skip_verify = false
|
|
|
|
|
|
|
|
## Optional SASL Config
|
|
|
|
# sasl_username = "kafka"
|
|
|
|
# sasl_password = "secret"
|
|
|
|
|
2016-02-18 21:26:51 +00:00
|
|
|
## the name of the consumer group
|
2015-11-16 20:12:45 +00:00
|
|
|
consumer_group = "telegraf_metrics_consumers"
|
2016-02-18 21:26:51 +00:00
|
|
|
## Offset (must be either "oldest" or "newest")
|
2015-11-16 20:12:45 +00:00
|
|
|
offset = "oldest"
|
2018-11-05 21:34:28 +00:00
|
|
|
## Maximum length of a message to consume, in bytes (default 0/unlimited);
|
|
|
|
## larger messages are dropped
|
|
|
|
max_message_len = 1000000
|
|
|
|
|
|
|
|
## Maximum messages to read from the broker that have not been written by an
|
|
|
|
## output. For best throughput set based on the number of metrics within
|
|
|
|
## each message and the size of the output's metric_batch_size.
|
|
|
|
##
|
|
|
|
## For example, if each message from the queue contains 10 metrics and the
|
|
|
|
## output metric_batch_size is 1000, setting this to 100 will ensure that a
|
|
|
|
## full batch is collected and the write is triggered immediately without
|
|
|
|
## waiting until the next flush_interval.
|
|
|
|
# max_undelivered_messages = 1000
|
2016-02-06 00:36:35 +00:00
|
|
|
|
2016-03-31 23:50:24 +00:00
|
|
|
## Data format to consume.
|
2017-04-11 19:05:39 +00:00
|
|
|
## Each data format has its own unique set of configuration options, read
|
2016-02-18 21:26:51 +00:00
|
|
|
## more about them here:
|
|
|
|
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
|
2016-02-06 00:36:35 +00:00
|
|
|
data_format = "influx"
|
2015-08-26 15:21:39 +00:00
|
|
|
`
|
2015-06-27 03:56:14 +00:00
|
|
|
|
|
|
|
func (k *Kafka) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *Kafka) Description() string {
|
2016-02-06 00:36:35 +00:00
|
|
|
return "Read metrics from Kafka topic(s)"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *Kafka) SetParser(parser parsers.Parser) {
|
|
|
|
k.parser = parser
|
2015-06-27 03:56:14 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 00:21:38 +00:00
|
|
|
func (k *Kafka) Start(acc telegraf.Accumulator) error {
|
2017-06-08 01:22:28 +00:00
|
|
|
var clusterErr error
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2017-06-08 01:22:28 +00:00
|
|
|
config := cluster.NewConfig()
|
2018-08-17 20:51:21 +00:00
|
|
|
|
|
|
|
if k.Version != "" {
|
|
|
|
version, err := sarama.ParseKafkaVersion(k.Version)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config.Version = version
|
|
|
|
}
|
|
|
|
|
2017-06-08 01:22:28 +00:00
|
|
|
config.Consumer.Return.Errors = true
|
|
|
|
|
2018-05-04 23:33:23 +00:00
|
|
|
tlsConfig, err := k.ClientConfig.TLSConfig()
|
2017-06-08 01:22:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-13 20:53:56 +00:00
|
|
|
if k.ClientID != "" {
|
|
|
|
config.ClientID = k.ClientID
|
|
|
|
} else {
|
|
|
|
config.ClientID = "Telegraf"
|
|
|
|
}
|
|
|
|
|
2017-06-08 01:22:28 +00:00
|
|
|
if tlsConfig != nil {
|
|
|
|
log.Printf("D! TLS Enabled")
|
|
|
|
config.Net.TLS.Config = tlsConfig
|
|
|
|
config.Net.TLS.Enable = true
|
|
|
|
}
|
|
|
|
if k.SASLUsername != "" && k.SASLPassword != "" {
|
|
|
|
log.Printf("D! Using SASL auth with username '%s',",
|
|
|
|
k.SASLUsername)
|
|
|
|
config.Net.SASL.User = k.SASLUsername
|
|
|
|
config.Net.SASL.Password = k.SASLPassword
|
|
|
|
config.Net.SASL.Enable = true
|
|
|
|
}
|
|
|
|
|
2015-11-16 20:12:45 +00:00
|
|
|
switch strings.ToLower(k.Offset) {
|
|
|
|
case "oldest", "":
|
2017-06-08 01:22:28 +00:00
|
|
|
config.Consumer.Offsets.Initial = sarama.OffsetOldest
|
2015-11-16 20:12:45 +00:00
|
|
|
case "newest":
|
2017-06-08 01:22:28 +00:00
|
|
|
config.Consumer.Offsets.Initial = sarama.OffsetNewest
|
2015-11-16 20:12:45 +00:00
|
|
|
default:
|
2018-11-05 21:34:28 +00:00
|
|
|
log.Printf("I! WARNING: Kafka consumer invalid offset '%s', using 'oldest'",
|
2015-11-16 20:12:45 +00:00
|
|
|
k.Offset)
|
2017-06-08 01:22:28 +00:00
|
|
|
config.Consumer.Offsets.Initial = sarama.OffsetOldest
|
2015-11-16 20:12:45 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
if k.cluster == nil {
|
|
|
|
k.cluster, clusterErr = cluster.NewConsumer(
|
2017-06-08 01:22:28 +00:00
|
|
|
k.Brokers,
|
2015-11-16 20:12:45 +00:00
|
|
|
k.ConsumerGroup,
|
|
|
|
k.Topics,
|
|
|
|
config,
|
2015-06-27 03:56:14 +00:00
|
|
|
)
|
2017-06-08 01:22:28 +00:00
|
|
|
|
|
|
|
if clusterErr != nil {
|
2018-11-05 21:34:28 +00:00
|
|
|
log.Printf("E! Error when creating Kafka Consumer, brokers: %v, topics: %v",
|
2017-06-08 01:22:28 +00:00
|
|
|
k.Brokers, k.Topics)
|
|
|
|
return clusterErr
|
2015-06-27 03:56:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
k.cancel = cancel
|
|
|
|
|
|
|
|
// Start consumer goroutine
|
|
|
|
k.wg = &sync.WaitGroup{}
|
|
|
|
k.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer k.wg.Done()
|
|
|
|
k.receiver(ctx, acc)
|
|
|
|
}()
|
|
|
|
|
|
|
|
log.Printf("I! Started the kafka consumer service, brokers: %v, topics: %v",
|
2017-06-08 01:22:28 +00:00
|
|
|
k.Brokers, k.Topics)
|
2015-11-16 20:12:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2016-02-06 00:36:35 +00:00
|
|
|
// receiver() reads all incoming messages from the consumer, and parses them into
|
2015-11-16 20:12:45 +00:00
|
|
|
// influxdb metric points.
|
2018-11-05 21:34:28 +00:00
|
|
|
func (k *Kafka) receiver(ctx context.Context, ac telegraf.Accumulator) {
|
|
|
|
k.messages = make(map[telegraf.TrackingID]*sarama.ConsumerMessage)
|
|
|
|
|
|
|
|
acc := ac.WithTracking(k.MaxUndeliveredMessages)
|
|
|
|
sem := make(semaphore, k.MaxUndeliveredMessages)
|
|
|
|
|
2015-06-27 03:56:14 +00:00
|
|
|
for {
|
|
|
|
select {
|
2018-11-05 21:34:28 +00:00
|
|
|
case <-ctx.Done():
|
2015-11-16 20:12:45 +00:00
|
|
|
return
|
2018-11-05 21:34:28 +00:00
|
|
|
case track := <-acc.Delivered():
|
|
|
|
<-sem
|
|
|
|
k.onDelivery(track)
|
|
|
|
case err := <-k.cluster.Errors():
|
|
|
|
acc.AddError(err)
|
|
|
|
case sem <- empty{}:
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case track := <-acc.Delivered():
|
|
|
|
// Once for the delivered message, once to leave the case
|
|
|
|
<-sem
|
|
|
|
<-sem
|
|
|
|
k.onDelivery(track)
|
|
|
|
case err := <-k.cluster.Errors():
|
|
|
|
<-sem
|
|
|
|
acc.AddError(err)
|
|
|
|
case msg := <-k.cluster.Messages():
|
|
|
|
err := k.onMessage(acc, msg)
|
2017-04-11 19:05:39 +00:00
|
|
|
if err != nil {
|
2018-11-05 21:34:28 +00:00
|
|
|
acc.AddError(err)
|
|
|
|
<-sem
|
2017-04-11 19:05:39 +00:00
|
|
|
}
|
2015-11-16 20:12:45 +00:00
|
|
|
}
|
2015-06-27 03:56:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
func (k *Kafka) markOffset(msg *sarama.ConsumerMessage) {
|
|
|
|
if !k.doNotCommitMsgs {
|
|
|
|
k.cluster.MarkOffset(msg, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *Kafka) onMessage(acc telegraf.TrackingAccumulator, msg *sarama.ConsumerMessage) error {
|
|
|
|
if k.MaxMessageLen != 0 && len(msg.Value) > k.MaxMessageLen {
|
|
|
|
k.markOffset(msg)
|
|
|
|
return fmt.Errorf("Message longer than max_message_len (%d > %d)",
|
|
|
|
len(msg.Value), k.MaxMessageLen)
|
|
|
|
}
|
|
|
|
|
|
|
|
metrics, err := k.parser.Parse(msg.Value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
id := acc.AddTrackingMetricGroup(metrics)
|
|
|
|
k.messages[id] = msg
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *Kafka) onDelivery(track telegraf.DeliveryInfo) {
|
|
|
|
msg, ok := k.messages[track.ID()]
|
|
|
|
if !ok {
|
|
|
|
log.Printf("E! [inputs.kafka_consumer] Could not mark message delivered: %d", track.ID())
|
|
|
|
}
|
|
|
|
|
|
|
|
if track.Delivered() {
|
|
|
|
k.markOffset(msg)
|
|
|
|
}
|
|
|
|
delete(k.messages, track.ID())
|
|
|
|
}
|
|
|
|
|
2015-11-16 20:12:45 +00:00
|
|
|
func (k *Kafka) Stop() {
|
2018-11-05 21:34:28 +00:00
|
|
|
k.cancel()
|
|
|
|
k.wg.Wait()
|
|
|
|
|
|
|
|
if err := k.cluster.Close(); err != nil {
|
|
|
|
log.Printf("E! [inputs.kafka_consumer] Error closing consumer: %v", err)
|
2015-11-16 20:12:45 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
func (k *Kafka) Gather(acc telegraf.Accumulator) error {
|
2015-11-16 20:12:45 +00:00
|
|
|
return nil
|
2015-06-27 03:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
inputs.Add("kafka_consumer", func() telegraf.Input {
|
2018-11-05 21:34:28 +00:00
|
|
|
return &Kafka{
|
|
|
|
MaxUndeliveredMessages: defaultMaxUndeliveredMessages,
|
|
|
|
}
|
2015-06-27 03:56:14 +00:00
|
|
|
})
|
|
|
|
}
|