2016-06-09 19:31:05 +00:00
|
|
|
package nsq_consumer
|
|
|
|
|
|
|
|
import (
|
2017-04-24 18:13:26 +00:00
|
|
|
"fmt"
|
2016-06-09 19:31:05 +00:00
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
|
|
|
"github.com/influxdata/telegraf/plugins/parsers"
|
2017-09-25 23:33:05 +00:00
|
|
|
nsq "github.com/nsqio/go-nsq"
|
2016-06-09 19:31:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
//NSQConsumer represents the configuration of the plugin
|
|
|
|
type NSQConsumer struct {
|
|
|
|
Server string
|
2017-09-25 23:33:05 +00:00
|
|
|
Nsqd []string
|
|
|
|
Nsqlookupd []string
|
2016-06-09 19:31:05 +00:00
|
|
|
Topic string
|
|
|
|
Channel string
|
|
|
|
MaxInFlight int
|
|
|
|
parser parsers.Parser
|
|
|
|
consumer *nsq.Consumer
|
|
|
|
acc telegraf.Accumulator
|
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2017-09-25 23:33:05 +00:00
|
|
|
## Server option still works but is deprecated, we just prepend it to the nsqd array.
|
|
|
|
# server = "localhost:4150"
|
|
|
|
## An array representing the NSQD TCP HTTP Endpoints
|
|
|
|
nsqd = ["localhost:4150"]
|
|
|
|
## An array representing the NSQLookupd HTTP Endpoints
|
|
|
|
nsqlookupd = ["localhost:4161"]
|
2016-06-09 19:31:05 +00:00
|
|
|
topic = "telegraf"
|
|
|
|
channel = "consumer"
|
|
|
|
max_in_flight = 100
|
|
|
|
|
|
|
|
## Data format to consume.
|
2017-04-27 21:59:18 +00:00
|
|
|
## Each data format has its own unique set of configuration options, read
|
2016-06-09 19:31:05 +00:00
|
|
|
## more about them here:
|
|
|
|
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
|
|
|
|
data_format = "influx"
|
|
|
|
`
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
inputs.Add("nsq_consumer", func() telegraf.Input {
|
|
|
|
return &NSQConsumer{}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetParser takes the data_format from the config and finds the right parser for that format
|
|
|
|
func (n *NSQConsumer) SetParser(parser parsers.Parser) {
|
|
|
|
n.parser = parser
|
|
|
|
}
|
|
|
|
|
|
|
|
// SampleConfig returns config values for generating a sample configuration file
|
|
|
|
func (n *NSQConsumer) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
// Description prints description string
|
|
|
|
func (n *NSQConsumer) Description() string {
|
|
|
|
return "Read NSQ topic for metrics."
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start pulls data from nsq
|
|
|
|
func (n *NSQConsumer) Start(acc telegraf.Accumulator) error {
|
|
|
|
n.acc = acc
|
|
|
|
n.connect()
|
|
|
|
n.consumer.AddConcurrentHandlers(nsq.HandlerFunc(func(message *nsq.Message) error {
|
|
|
|
metrics, err := n.parser.Parse(message.Body)
|
|
|
|
if err != nil {
|
2017-04-24 18:13:26 +00:00
|
|
|
acc.AddError(fmt.Errorf("E! NSQConsumer Parse Error\nmessage:%s\nerror:%s", string(message.Body), err.Error()))
|
2016-06-09 19:31:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, metric := range metrics {
|
|
|
|
n.acc.AddFields(metric.Name(), metric.Fields(), metric.Tags(), metric.Time())
|
|
|
|
}
|
|
|
|
message.Finish()
|
|
|
|
return nil
|
|
|
|
}), n.MaxInFlight)
|
2017-09-25 23:33:05 +00:00
|
|
|
|
|
|
|
if len(n.Nsqlookupd) > 0 {
|
|
|
|
n.consumer.ConnectToNSQLookupds(n.Nsqlookupd)
|
|
|
|
}
|
|
|
|
n.consumer.ConnectToNSQDs(append(n.Nsqd, n.Server))
|
2016-06-09 19:31:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop processing messages
|
|
|
|
func (n *NSQConsumer) Stop() {
|
|
|
|
n.consumer.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gather is a noop
|
|
|
|
func (n *NSQConsumer) Gather(acc telegraf.Accumulator) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NSQConsumer) connect() error {
|
|
|
|
if n.consumer == nil {
|
|
|
|
config := nsq.NewConfig()
|
|
|
|
config.MaxInFlight = n.MaxInFlight
|
|
|
|
consumer, err := nsq.NewConsumer(n.Topic, n.Channel, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n.consumer = consumer
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|