2015-06-27 03:56:14 +00:00
|
|
|
package kafka_consumer
|
|
|
|
|
|
|
|
import (
|
2015-11-16 20:12:45 +00:00
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-02-06 00:36:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers"
|
2017-02-04 15:58:02 +00:00
|
|
|
"github.com/influxdata/telegraf/registry/inputs"
|
2015-11-16 20:12:45 +00:00
|
|
|
|
|
|
|
"github.com/Shopify/sarama"
|
2015-06-27 03:56:14 +00:00
|
|
|
"github.com/wvanbergen/kafka/consumergroup"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Kafka struct {
|
2016-03-01 19:38:12 +00:00
|
|
|
ConsumerGroup string
|
|
|
|
Topics []string
|
|
|
|
ZookeeperPeers []string
|
2016-03-01 19:11:18 +00:00
|
|
|
ZookeeperChroot string
|
2016-03-01 19:38:12 +00:00
|
|
|
Consumer *consumergroup.ConsumerGroup
|
2016-02-16 00:21:38 +00:00
|
|
|
|
|
|
|
// Legacy metric buffer support
|
|
|
|
MetricBuffer int
|
2016-02-09 22:03:46 +00:00
|
|
|
// TODO remove PointBuffer, legacy support
|
|
|
|
PointBuffer int
|
2015-11-16 20:12:45 +00:00
|
|
|
|
2016-02-16 00:21:38 +00:00
|
|
|
Offset string
|
2016-02-06 00:36:35 +00:00
|
|
|
parser parsers.Parser
|
|
|
|
|
2015-11-16 20:12:45 +00:00
|
|
|
sync.Mutex
|
|
|
|
|
|
|
|
// channel for all incoming kafka messages
|
|
|
|
in <-chan *sarama.ConsumerMessage
|
|
|
|
// channel for all kafka consumer errors
|
2017-01-11 16:24:09 +00:00
|
|
|
errs <-chan error
|
2016-02-16 00:21:38 +00:00
|
|
|
done chan struct{}
|
|
|
|
|
|
|
|
// keep the accumulator internally:
|
|
|
|
acc telegraf.Accumulator
|
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 = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## topic(s) to consume
|
2015-11-16 20:12:45 +00:00
|
|
|
topics = ["telegraf"]
|
2016-02-18 21:26:51 +00:00
|
|
|
## an array of Zookeeper connection strings
|
2015-11-16 20:12:45 +00:00
|
|
|
zookeeper_peers = ["localhost:2181"]
|
2016-03-01 19:11:18 +00:00
|
|
|
## Zookeeper Chroot
|
2016-06-10 11:07:36 +00:00
|
|
|
zookeeper_chroot = ""
|
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"
|
2016-02-06 00:36:35 +00:00
|
|
|
|
2016-03-31 23:50:24 +00:00
|
|
|
## Data format to consume.
|
2016-02-18 21:26:51 +00:00
|
|
|
## Each data format has it's own unique set of configuration options, read
|
|
|
|
## 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 {
|
2015-11-16 20:12:45 +00:00
|
|
|
k.Lock()
|
|
|
|
defer k.Unlock()
|
2015-06-27 03:56:14 +00:00
|
|
|
var consumerErr error
|
|
|
|
|
2016-02-16 00:21:38 +00:00
|
|
|
k.acc = acc
|
|
|
|
|
2015-11-16 20:12:45 +00:00
|
|
|
config := consumergroup.NewConfig()
|
2016-03-01 19:11:18 +00:00
|
|
|
config.Zookeeper.Chroot = k.ZookeeperChroot
|
2015-11-16 20:12:45 +00:00
|
|
|
switch strings.ToLower(k.Offset) {
|
|
|
|
case "oldest", "":
|
|
|
|
config.Offsets.Initial = sarama.OffsetOldest
|
|
|
|
case "newest":
|
|
|
|
config.Offsets.Initial = sarama.OffsetNewest
|
|
|
|
default:
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("I! WARNING: Kafka consumer invalid offset '%s', using 'oldest'\n",
|
2015-11-16 20:12:45 +00:00
|
|
|
k.Offset)
|
|
|
|
config.Offsets.Initial = sarama.OffsetOldest
|
|
|
|
}
|
|
|
|
|
|
|
|
if k.Consumer == nil || k.Consumer.Closed() {
|
2015-06-27 03:56:14 +00:00
|
|
|
k.Consumer, consumerErr = consumergroup.JoinConsumerGroup(
|
2015-11-16 20:12:45 +00:00
|
|
|
k.ConsumerGroup,
|
|
|
|
k.Topics,
|
2015-06-27 03:56:14 +00:00
|
|
|
k.ZookeeperPeers,
|
2015-11-16 20:12:45 +00:00
|
|
|
config,
|
2015-06-27 03:56:14 +00:00
|
|
|
)
|
|
|
|
if consumerErr != nil {
|
|
|
|
return consumerErr
|
|
|
|
}
|
|
|
|
|
2015-11-16 20:12:45 +00:00
|
|
|
// Setup message and error channels
|
|
|
|
k.in = k.Consumer.Messages()
|
|
|
|
k.errs = k.Consumer.Errors()
|
2015-06-27 03:56:14 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 20:12:45 +00:00
|
|
|
k.done = make(chan struct{})
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2015-11-16 20:12:45 +00:00
|
|
|
// Start the kafka message reader
|
2016-02-06 00:36:35 +00:00
|
|
|
go k.receiver()
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("I! Started the kafka consumer service, peers: %v, topics: %v\n",
|
2015-11-16 20:12:45 +00:00
|
|
|
k.ZookeeperPeers, k.Topics)
|
|
|
|
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.
|
2016-02-06 00:36:35 +00:00
|
|
|
func (k *Kafka) receiver() {
|
2015-06-27 03:56:14 +00:00
|
|
|
for {
|
|
|
|
select {
|
2015-11-16 20:12:45 +00:00
|
|
|
case <-k.done:
|
|
|
|
return
|
|
|
|
case err := <-k.errs:
|
2016-09-14 07:52:58 +00:00
|
|
|
if err != nil {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("E! Kafka Consumer Error: %s\n", err)
|
2016-09-14 07:52:58 +00:00
|
|
|
}
|
2015-11-16 20:12:45 +00:00
|
|
|
case msg := <-k.in:
|
2016-02-06 00:36:35 +00:00
|
|
|
metrics, err := k.parser.Parse(msg.Value)
|
2015-11-16 20:12:45 +00:00
|
|
|
if err != nil {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("E! Kafka Message Parse Error\nmessage: %s\nerror: %s",
|
2015-11-16 20:12:45 +00:00
|
|
|
string(msg.Value), err.Error())
|
2015-06-27 03:56:14 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
for _, metric := range metrics {
|
2016-02-16 00:21:38 +00:00
|
|
|
k.acc.AddFields(metric.Name(), metric.Fields(), metric.Tags(), metric.Time())
|
2015-11-16 20:12:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !k.doNotCommitMsgs {
|
|
|
|
// TODO(cam) this locking can be removed if this PR gets merged:
|
|
|
|
// https://github.com/wvanbergen/kafka/pull/84
|
|
|
|
k.Lock()
|
|
|
|
k.Consumer.CommitUpto(msg)
|
|
|
|
k.Unlock()
|
2015-06-27 03:56:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-16 20:12:45 +00:00
|
|
|
func (k *Kafka) Stop() {
|
|
|
|
k.Lock()
|
|
|
|
defer k.Unlock()
|
|
|
|
close(k.done)
|
|
|
|
if err := k.Consumer.Close(); err != nil {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("E! Error closing kafka consumer: %s\n", err.Error())
|
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 {
|
2015-06-27 03:56:14 +00:00
|
|
|
return &Kafka{}
|
|
|
|
})
|
|
|
|
}
|