telegraf/plugins/serializers/registry.go

161 lines
5.2 KiB
Go
Raw Normal View History

package serializers
import (
"fmt"
"time"
"github.com/influxdata/telegraf"
2019-01-26 02:06:08 +00:00
"github.com/influxdata/telegraf/plugins/serializers/carbon2"
"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"
2019-01-08 23:28:00 +00:00
"github.com/influxdata/telegraf/plugins/serializers/nowmetric"
2018-09-11 20:01:08 +00:00
"github.com/influxdata/telegraf/plugins/serializers/splunkmetric"
"github.com/influxdata/telegraf/plugins/serializers/wavefront"
)
// 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.
//
// Implementations of this interface should be reentrant but are not required
// to be thread-safe.
type Serializer interface {
// 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.
//
// New plugins should use SerializeBatch instead to allow for non-line
// delimited metrics.
Serialize(metric telegraf.Metric) ([]byte, error)
// SerializeBatch takes an array of telegraf metric and serializes it into
// a byte buffer. This method is not required to be suitable for use with
// line oriented framing.
SerializeBatch(metrics []telegraf.Metric) ([]byte, error)
}
// 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 the serializer types listed in NewSerializer.
DataFormat string
// Support tags in graphite protocol
GraphiteTagSupport bool
// Maximum line length in bytes; influx format only
InfluxMaxLineBytes int
// Sort field keys, set to true only when debugging as it less performant
// than unsorted fields; influx format only
InfluxSortFields bool
// Support unsigned integer output; influx format only
InfluxUintSupport bool
// Prefix to add to all measurements, only supports Graphite
Prefix string
// Template for converting telegraf metrics into Graphite
// only supports Graphite
Template string
// Timestamp units to use for JSON formatted output
TimestampUnits time.Duration
2018-09-11 20:01:08 +00:00
// Include HEC routing fields for splunkmetric output
HecRouting bool
2019-11-18 20:38:34 +00:00
// Enable Splunk MultiMetric output (Splunk 8.0+)
SplunkmetricMultiMetric bool
// Point tags to use as the source name for Wavefront (if none found, host will be used).
WavefrontSourceOverride []string
// Use Strict rules to sanitize metric and tag names from invalid characters for Wavefront
// When enabled forward slash (/) and comma (,) will be accepted
WavefrontUseStrict bool
}
// 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 = NewInfluxSerializerConfig(config)
case "graphite":
serializer, err = NewGraphiteSerializer(config.Prefix, config.Template, config.GraphiteTagSupport)
2016-03-17 17:50:39 +00:00
case "json":
serializer, err = NewJsonSerializer(config.TimestampUnits)
2018-09-11 20:01:08 +00:00
case "splunkmetric":
2019-11-18 20:38:34 +00:00
serializer, err = NewSplunkmetricSerializer(config.HecRouting, config.SplunkmetricMultiMetric)
2019-01-08 23:28:00 +00:00
case "nowmetric":
serializer, err = NewNowSerializer()
2019-01-26 02:06:08 +00:00
case "carbon2":
serializer, err = NewCarbon2Serializer()
case "wavefront":
serializer, err = NewWavefrontSerializer(config.Prefix, config.WavefrontUseStrict, config.WavefrontSourceOverride)
default:
err = fmt.Errorf("Invalid data format: %s", config.DataFormat)
}
return serializer, err
}
func NewWavefrontSerializer(prefix string, useStrict bool, sourceOverride []string) (Serializer, error) {
return wavefront.NewSerializer(prefix, useStrict, sourceOverride)
}
func NewJsonSerializer(timestampUnits time.Duration) (Serializer, error) {
return json.NewSerializer(timestampUnits)
2016-03-17 17:50:39 +00:00
}
2019-01-26 02:06:08 +00:00
func NewCarbon2Serializer() (Serializer, error) {
return carbon2.NewSerializer()
}
2019-11-18 20:38:34 +00:00
func NewSplunkmetricSerializer(splunkmetric_hec_routing bool, splunkmetric_multimetric bool) (Serializer, error) {
return splunkmetric.NewSerializer(splunkmetric_hec_routing, splunkmetric_multimetric)
2018-09-11 20:01:08 +00:00
}
2019-01-08 23:28:00 +00:00
func NewNowSerializer() (Serializer, error) {
return nowmetric.NewSerializer()
}
func NewInfluxSerializerConfig(config *Config) (Serializer, error) {
var sort influx.FieldSortOrder
if config.InfluxSortFields {
sort = influx.SortFields
}
var typeSupport influx.FieldTypeSupport
if config.InfluxUintSupport {
typeSupport = typeSupport + influx.UintSupport
}
s := influx.NewSerializer()
s.SetMaxLineBytes(config.InfluxMaxLineBytes)
s.SetFieldSortOrder(sort)
s.SetFieldTypeSupport(typeSupport)
return s, nil
}
func NewInfluxSerializer() (Serializer, error) {
return influx.NewSerializer(), nil
}
func NewGraphiteSerializer(prefix, template string, tag_support bool) (Serializer, error) {
return &graphite.GraphiteSerializer{
Prefix: prefix,
Template: template,
TagSupport: tag_support,
}, nil
}