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-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/influxdb/models"
|
2016-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/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 {
|
2015-11-16 20:12:45 +00:00
|
|
|
ConsumerGroup string
|
|
|
|
Topics []string
|
|
|
|
ZookeeperPeers []string
|
|
|
|
Consumer *consumergroup.ConsumerGroup
|
|
|
|
PointBuffer int
|
|
|
|
Offset string
|
|
|
|
|
|
|
|
sync.Mutex
|
|
|
|
|
|
|
|
// channel for all incoming kafka messages
|
|
|
|
in <-chan *sarama.ConsumerMessage
|
|
|
|
// channel for all kafka consumer errors
|
|
|
|
errs <-chan *sarama.ConsumerError
|
|
|
|
// channel for all incoming parsed kafka points
|
|
|
|
pointChan chan models.Point
|
|
|
|
done chan struct{}
|
|
|
|
|
|
|
|
// 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 = `
|
2015-11-16 20:12:45 +00:00
|
|
|
# topic(s) to consume
|
|
|
|
topics = ["telegraf"]
|
2015-10-15 21:53:29 +00:00
|
|
|
# an array of Zookeeper connection strings
|
2015-11-16 20:12:45 +00:00
|
|
|
zookeeper_peers = ["localhost:2181"]
|
|
|
|
# the name of the consumer group
|
|
|
|
consumer_group = "telegraf_metrics_consumers"
|
|
|
|
# Maximum number of points to buffer between collection intervals
|
|
|
|
point_buffer = 100000
|
|
|
|
# Offset (must be either "oldest" or "newest")
|
|
|
|
offset = "oldest"
|
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 {
|
2015-11-16 20:12:45 +00:00
|
|
|
return "Read line-protocol metrics from Kafka topic(s)"
|
2015-06-27 03:56:14 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 20:12:45 +00:00
|
|
|
func (k *Kafka) Start() error {
|
|
|
|
k.Lock()
|
|
|
|
defer k.Unlock()
|
2015-06-27 03:56:14 +00:00
|
|
|
var consumerErr error
|
|
|
|
|
2015-11-16 20:12:45 +00:00
|
|
|
config := consumergroup.NewConfig()
|
|
|
|
switch strings.ToLower(k.Offset) {
|
|
|
|
case "oldest", "":
|
|
|
|
config.Offsets.Initial = sarama.OffsetOldest
|
|
|
|
case "newest":
|
|
|
|
config.Offsets.Initial = sarama.OffsetNewest
|
|
|
|
default:
|
|
|
|
log.Printf("WARNING: Kafka consumer invalid offset '%s', using 'oldest'\n",
|
|
|
|
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{})
|
|
|
|
if k.PointBuffer == 0 {
|
|
|
|
k.PointBuffer = 100000
|
|
|
|
}
|
|
|
|
k.pointChan = make(chan models.Point, k.PointBuffer)
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2015-11-16 20:12:45 +00:00
|
|
|
// Start the kafka message reader
|
|
|
|
go k.parser()
|
|
|
|
log.Printf("Started the kafka consumer service, peers: %v, topics: %v\n",
|
|
|
|
k.ZookeeperPeers, k.Topics)
|
|
|
|
return nil
|
|
|
|
}
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2015-11-16 20:12:45 +00:00
|
|
|
// parser() reads all incoming messages from the consumer, and parses them into
|
|
|
|
// influxdb metric points.
|
|
|
|
func (k *Kafka) parser() {
|
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:
|
|
|
|
log.Printf("Kafka Consumer Error: %s\n", err.Error())
|
|
|
|
case msg := <-k.in:
|
|
|
|
points, err := models.ParsePoints(msg.Value)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Could not parse kafka message: %s, error: %s",
|
|
|
|
string(msg.Value), err.Error())
|
2015-06-27 03:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, point := range points {
|
2015-11-16 20:12:45 +00:00
|
|
|
select {
|
|
|
|
case k.pointChan <- point:
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
log.Printf("Kafka Consumer buffer is full, dropping a point." +
|
|
|
|
" You may want to increase the point_buffer setting")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
log.Printf("Error closing kafka consumer: %s\n", err.Error())
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
k.Lock()
|
|
|
|
defer k.Unlock()
|
|
|
|
npoints := len(k.pointChan)
|
|
|
|
for i := 0; i < npoints; i++ {
|
|
|
|
point := <-k.pointChan
|
|
|
|
acc.AddFields(point.Name(), point.Fields(), point.Tags(), point.Time())
|
2015-06-27 03:56:14 +00:00
|
|
|
}
|
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{}
|
|
|
|
})
|
|
|
|
}
|