2015-10-27 16:59:05 +00:00
|
|
|
package nsq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-02-10 22:50:07 +00:00
|
|
|
|
|
|
|
"github.com/nsqio/go-nsq"
|
|
|
|
|
2016-12-23 15:18:27 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins"
|
2016-01-27 23:15:14 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/outputs"
|
2016-02-10 22:50:07 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/serializers"
|
2015-10-27 16:59:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type NSQ struct {
|
|
|
|
Server string
|
|
|
|
Topic string
|
|
|
|
producer *nsq.Producer
|
2016-02-10 22:50:07 +00:00
|
|
|
|
|
|
|
serializer serializers.Serializer
|
2015-10-27 16:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## Location of nsqd instance listening on TCP
|
2015-10-27 16:59:05 +00:00
|
|
|
server = "localhost:4150"
|
2016-02-18 21:26:51 +00:00
|
|
|
## NSQ topic for producer messages
|
2015-10-27 16:59:05 +00:00
|
|
|
topic = "telegraf"
|
2016-02-10 22:50:07 +00:00
|
|
|
|
2016-03-31 23:50:24 +00:00
|
|
|
## Data format to output.
|
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_OUTPUT.md
|
2016-02-10 22:50:07 +00:00
|
|
|
data_format = "influx"
|
2015-10-27 16:59:05 +00:00
|
|
|
`
|
|
|
|
|
2016-02-10 22:50:07 +00:00
|
|
|
func (n *NSQ) SetSerializer(serializer serializers.Serializer) {
|
|
|
|
n.serializer = serializer
|
|
|
|
}
|
|
|
|
|
2015-10-27 16:59:05 +00:00
|
|
|
func (n *NSQ) Connect() error {
|
|
|
|
config := nsq.NewConfig()
|
|
|
|
producer, err := nsq.NewProducer(n.Server, config)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
n.producer = producer
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NSQ) Close() error {
|
|
|
|
n.producer.Stop()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NSQ) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NSQ) Description() string {
|
|
|
|
return "Send telegraf measurements to NSQD"
|
|
|
|
}
|
|
|
|
|
2016-12-23 15:18:27 +00:00
|
|
|
func (n *NSQ) Write(metrics []plugins.Metric) error {
|
2016-01-27 23:15:14 +00:00
|
|
|
if len(metrics) == 0 {
|
2015-10-27 16:59:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-10 22:50:07 +00:00
|
|
|
for _, metric := range metrics {
|
2016-11-22 12:51:57 +00:00
|
|
|
buf, err := n.serializer.Serialize(metric)
|
2016-02-10 22:50:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-27 16:59:05 +00:00
|
|
|
|
2016-11-22 12:51:57 +00:00
|
|
|
err = n.producer.Publish(n.Topic, buf)
|
|
|
|
if err != nil {
|
2015-10-27 16:59:05 +00:00
|
|
|
return fmt.Errorf("FAILED to send NSQD message: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-12-23 15:18:27 +00:00
|
|
|
outputs.Add("nsq", func() plugins.Output {
|
2015-10-27 16:59:05 +00:00
|
|
|
return &NSQ{}
|
|
|
|
})
|
|
|
|
}
|