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-01-27 21:21:36 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
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-03 19:59:34 +00:00
|
|
|
### Location of nsqd instance listening on TCP
|
2015-10-27 16:59:05 +00:00
|
|
|
server = "localhost:4150"
|
2016-02-03 19:59:34 +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
|
|
|
|
|
|
|
### Data format to output. This can be "influx" or "graphite"
|
|
|
|
### Each data format has it's own unique set of configuration options, read
|
|
|
|
### more about them here:
|
|
|
|
### https://github.com/influxdata/telegraf/blob/master/DATA_FORMATS_OUTPUT.md
|
|
|
|
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-01-27 23:15:14 +00:00
|
|
|
func (n *NSQ) Write(metrics []telegraf.Metric) error {
|
|
|
|
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 {
|
|
|
|
values, err := n.serializer.Serialize(metric)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-27 16:59:05 +00:00
|
|
|
|
2016-02-10 22:50:07 +00:00
|
|
|
var pubErr error
|
|
|
|
for _, value := range values {
|
|
|
|
err = n.producer.Publish(n.Topic, []byte(value))
|
|
|
|
if err != nil {
|
|
|
|
pubErr = err
|
|
|
|
}
|
|
|
|
}
|
2015-10-27 16:59:05 +00:00
|
|
|
|
2016-02-10 22:50:07 +00:00
|
|
|
if pubErr != nil {
|
2015-10-27 16:59:05 +00:00
|
|
|
return fmt.Errorf("FAILED to send NSQD message: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
outputs.Add("nsq", func() telegraf.Output {
|
2015-10-27 16:59:05 +00:00
|
|
|
return &NSQ{}
|
|
|
|
})
|
|
|
|
}
|