2016-02-10 22:50:07 +00:00
|
|
|
package serializers
|
|
|
|
|
|
|
|
import (
|
2016-12-23 15:18:27 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins"
|
2016-02-10 22:50:07 +00:00
|
|
|
|
|
|
|
"github.com/influxdata/telegraf/plugins/serializers/graphite"
|
|
|
|
"github.com/influxdata/telegraf/plugins/serializers/influx"
|
2016-03-17 17:50:39 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/serializers/json"
|
2016-02-10 22:50:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SerializerOutput is an interface for output plugins that are able to
|
|
|
|
// serialize telegraf metrics into arbitrary data formats.
|
|
|
|
type SerializerOutput interface {
|
|
|
|
// SetSerializer sets the serializer function for the interface.
|
|
|
|
SetSerializer(serializer Serializer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serializer is an interface defining functions that a serializer plugin must
|
|
|
|
// satisfy.
|
|
|
|
type Serializer interface {
|
2016-11-22 12:51:57 +00:00
|
|
|
// Serialize takes a single telegraf metric and turns it into a byte buffer.
|
|
|
|
// separate metrics should be separated by a newline, and there should be
|
|
|
|
// a newline at the end of the buffer.
|
2016-12-23 15:18:27 +00:00
|
|
|
Serialize(metric plugins.Metric) ([]byte, error)
|
2016-02-10 22:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config is a struct that covers the data types needed for all serializer types,
|
|
|
|
// and can be used to instantiate _any_ of the serializers.
|
|
|
|
type Config struct {
|
|
|
|
// Dataformat can be one of: influx, graphite
|
|
|
|
DataFormat string
|
|
|
|
|
|
|
|
// Prefix to add to all measurements, only supports Graphite
|
|
|
|
Prefix string
|
2016-04-08 22:04:45 +00:00
|
|
|
|
|
|
|
// Template for converting telegraf metrics into Graphite
|
|
|
|
// only supports Graphite
|
|
|
|
Template string
|
2016-02-10 22:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSerializer a Serializer interface based on the given config.
|
|
|
|
func NewSerializer(config *Config) (Serializer, error) {
|
|
|
|
var err error
|
|
|
|
var serializer Serializer
|
|
|
|
switch config.DataFormat {
|
|
|
|
case "influx":
|
|
|
|
serializer, err = NewInfluxSerializer()
|
|
|
|
case "graphite":
|
2016-04-08 22:04:45 +00:00
|
|
|
serializer, err = NewGraphiteSerializer(config.Prefix, config.Template)
|
2016-03-17 17:50:39 +00:00
|
|
|
case "json":
|
|
|
|
serializer, err = NewJsonSerializer()
|
2016-02-10 22:50:07 +00:00
|
|
|
}
|
|
|
|
return serializer, err
|
|
|
|
}
|
|
|
|
|
2016-03-17 17:50:39 +00:00
|
|
|
func NewJsonSerializer() (Serializer, error) {
|
|
|
|
return &json.JsonSerializer{}, nil
|
|
|
|
}
|
|
|
|
|
2016-02-10 22:50:07 +00:00
|
|
|
func NewInfluxSerializer() (Serializer, error) {
|
|
|
|
return &influx.InfluxSerializer{}, nil
|
|
|
|
}
|
|
|
|
|
2016-04-08 22:04:45 +00:00
|
|
|
func NewGraphiteSerializer(prefix, template string) (Serializer, error) {
|
2016-02-10 22:50:07 +00:00
|
|
|
return &graphite.GraphiteSerializer{
|
2016-04-08 22:04:45 +00:00
|
|
|
Prefix: prefix,
|
|
|
|
Template: template,
|
2016-02-10 22:50:07 +00:00
|
|
|
}, nil
|
|
|
|
}
|