2016-02-10 22:50:07 +00:00
|
|
|
package serializers
|
|
|
|
|
|
|
|
import (
|
2017-03-17 17:14:03 +00:00
|
|
|
"fmt"
|
2017-03-30 00:12:29 +00:00
|
|
|
"time"
|
2017-03-17 17:14:03 +00:00
|
|
|
|
2016-02-10 22:50:07 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2019-01-26 02:06:08 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/serializers/carbon2"
|
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"
|
2019-01-08 23:28:00 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/serializers/nowmetric"
|
2019-11-26 23:46:31 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/serializers/prometheus"
|
2018-09-11 20:01:08 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/serializers/splunkmetric"
|
2019-04-05 21:46:12 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/serializers/wavefront"
|
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.
|
2019-05-17 20:44:08 +00:00
|
|
|
//
|
|
|
|
// Implementations of this interface should be reentrant but are not required
|
|
|
|
// to be thread-safe.
|
2016-02-10 22:50:07 +00:00
|
|
|
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.
|
2019-06-04 00:34:48 +00:00
|
|
|
//
|
|
|
|
// New plugins should use SerializeBatch instead to allow for non-line
|
|
|
|
// delimited metrics.
|
2016-11-22 12:51:57 +00:00
|
|
|
Serialize(metric telegraf.Metric) ([]byte, error)
|
2018-05-05 01:27:31 +00:00
|
|
|
|
|
|
|
// 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)
|
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 {
|
2019-06-04 00:34:48 +00:00
|
|
|
// Dataformat can be one of the serializer types listed in NewSerializer.
|
2019-11-26 23:46:31 +00:00
|
|
|
DataFormat string `toml:"data_format"`
|
2016-02-10 22:50:07 +00:00
|
|
|
|
2018-05-21 22:59:56 +00:00
|
|
|
// Support tags in graphite protocol
|
2019-11-26 23:46:31 +00:00
|
|
|
GraphiteTagSupport bool `toml:"graphite_tag_support"`
|
2018-05-21 22:59:56 +00:00
|
|
|
|
2020-05-21 00:15:18 +00:00
|
|
|
// Character for separating metric name and field for Graphite tags
|
|
|
|
GraphiteSeparator string `toml:"graphite_separator"`
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
// Maximum line length in bytes; influx format only
|
2019-11-26 23:46:31 +00:00
|
|
|
InfluxMaxLineBytes int `toml:"influx_max_line_bytes"`
|
2018-03-28 00:30:51 +00:00
|
|
|
|
|
|
|
// Sort field keys, set to true only when debugging as it less performant
|
|
|
|
// than unsorted fields; influx format only
|
2019-11-26 23:46:31 +00:00
|
|
|
InfluxSortFields bool `toml:"influx_sort_fields"`
|
2018-03-28 00:30:51 +00:00
|
|
|
|
2018-03-29 20:31:43 +00:00
|
|
|
// Support unsigned integer output; influx format only
|
2019-11-26 23:46:31 +00:00
|
|
|
InfluxUintSupport bool `toml:"influx_uint_support"`
|
2018-03-29 20:31:43 +00:00
|
|
|
|
2016-02-10 22:50:07 +00:00
|
|
|
// Prefix to add to all measurements, only supports Graphite
|
2019-11-26 23:46:31 +00:00
|
|
|
Prefix string `toml:"prefix"`
|
2016-04-08 22:04:45 +00:00
|
|
|
|
|
|
|
// Template for converting telegraf metrics into Graphite
|
|
|
|
// only supports Graphite
|
2019-11-26 23:46:31 +00:00
|
|
|
Template string `toml:"template"`
|
2017-03-30 00:12:29 +00:00
|
|
|
|
2020-03-31 18:30:21 +00:00
|
|
|
// Templates same Template, but multiple
|
|
|
|
Templates []string `toml:"templates"`
|
|
|
|
|
2017-03-30 00:12:29 +00:00
|
|
|
// Timestamp units to use for JSON formatted output
|
2019-11-26 23:46:31 +00:00
|
|
|
TimestampUnits time.Duration `toml:"timestamp_units"`
|
2018-09-11 20:01:08 +00:00
|
|
|
|
|
|
|
// Include HEC routing fields for splunkmetric output
|
2019-11-26 23:46:31 +00:00
|
|
|
HecRouting bool `toml:"hec_routing"`
|
2019-04-05 21:46:12 +00:00
|
|
|
|
2019-11-18 20:38:34 +00:00
|
|
|
// Enable Splunk MultiMetric output (Splunk 8.0+)
|
2019-11-26 23:46:31 +00:00
|
|
|
SplunkmetricMultiMetric bool `toml:"splunkmetric_multi_metric"`
|
2019-11-18 20:38:34 +00:00
|
|
|
|
2019-04-05 21:46:12 +00:00
|
|
|
// Point tags to use as the source name for Wavefront (if none found, host will be used).
|
2019-11-26 23:46:31 +00:00
|
|
|
WavefrontSourceOverride []string `toml:"wavefront_source_override"`
|
2019-04-05 21:46:12 +00:00
|
|
|
|
|
|
|
// Use Strict rules to sanitize metric and tag names from invalid characters for Wavefront
|
|
|
|
// When enabled forward slash (/) and comma (,) will be accepted
|
2019-11-26 23:46:31 +00:00
|
|
|
WavefrontUseStrict bool `toml:"wavefront_use_strict"`
|
|
|
|
|
|
|
|
// Include the metric timestamp on each sample.
|
|
|
|
PrometheusExportTimestamp bool `toml:"prometheus_export_timestamp"`
|
|
|
|
|
|
|
|
// Sort prometheus metric families and metric samples. Useful for
|
|
|
|
// debugging.
|
|
|
|
PrometheusSortMetrics bool `toml:"prometheus_sort_metrics"`
|
|
|
|
|
|
|
|
// Output string fields as metric labels; when false string fields are
|
|
|
|
// discarded.
|
|
|
|
PrometheusStringAsLabel bool `toml:"prometheus_string_as_label"`
|
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":
|
2018-03-28 00:30:51 +00:00
|
|
|
serializer, err = NewInfluxSerializerConfig(config)
|
2016-02-10 22:50:07 +00:00
|
|
|
case "graphite":
|
2020-05-21 00:15:18 +00:00
|
|
|
serializer, err = NewGraphiteSerializer(config.Prefix, config.Template, config.GraphiteTagSupport, config.GraphiteSeparator, config.Templates)
|
2016-03-17 17:50:39 +00:00
|
|
|
case "json":
|
2017-03-30 00:12:29 +00:00
|
|
|
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()
|
2019-04-05 21:46:12 +00:00
|
|
|
case "wavefront":
|
|
|
|
serializer, err = NewWavefrontSerializer(config.Prefix, config.WavefrontUseStrict, config.WavefrontSourceOverride)
|
2019-11-26 23:46:31 +00:00
|
|
|
case "prometheus":
|
|
|
|
serializer, err = NewPrometheusSerializer(config)
|
2017-03-17 17:14:03 +00:00
|
|
|
default:
|
|
|
|
err = fmt.Errorf("Invalid data format: %s", config.DataFormat)
|
2016-02-10 22:50:07 +00:00
|
|
|
}
|
|
|
|
return serializer, err
|
|
|
|
}
|
|
|
|
|
2019-11-26 23:46:31 +00:00
|
|
|
func NewPrometheusSerializer(config *Config) (Serializer, error) {
|
|
|
|
exportTimestamp := prometheus.NoExportTimestamp
|
|
|
|
if config.PrometheusExportTimestamp {
|
|
|
|
exportTimestamp = prometheus.ExportTimestamp
|
|
|
|
}
|
|
|
|
|
|
|
|
sortMetrics := prometheus.NoSortMetrics
|
|
|
|
if config.PrometheusExportTimestamp {
|
|
|
|
sortMetrics = prometheus.SortMetrics
|
|
|
|
}
|
|
|
|
|
|
|
|
stringAsLabels := prometheus.DiscardStrings
|
|
|
|
if config.PrometheusStringAsLabel {
|
|
|
|
stringAsLabels = prometheus.StringAsLabel
|
|
|
|
}
|
|
|
|
|
|
|
|
return prometheus.NewSerializer(prometheus.FormatConfig{
|
|
|
|
TimestampExport: exportTimestamp,
|
|
|
|
MetricSortOrder: sortMetrics,
|
|
|
|
StringHandling: stringAsLabels,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-05 21:46:12 +00:00
|
|
|
func NewWavefrontSerializer(prefix string, useStrict bool, sourceOverride []string) (Serializer, error) {
|
|
|
|
return wavefront.NewSerializer(prefix, useStrict, sourceOverride)
|
|
|
|
}
|
|
|
|
|
2017-03-30 00:12:29 +00:00
|
|
|
func NewJsonSerializer(timestampUnits time.Duration) (Serializer, error) {
|
2018-05-05 01:27:31 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func NewInfluxSerializerConfig(config *Config) (Serializer, error) {
|
|
|
|
var sort influx.FieldSortOrder
|
|
|
|
if config.InfluxSortFields {
|
|
|
|
sort = influx.SortFields
|
|
|
|
}
|
2018-03-29 20:31:43 +00:00
|
|
|
|
|
|
|
var typeSupport influx.FieldTypeSupport
|
|
|
|
if config.InfluxUintSupport {
|
|
|
|
typeSupport = typeSupport + influx.UintSupport
|
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
s := influx.NewSerializer()
|
|
|
|
s.SetMaxLineBytes(config.InfluxMaxLineBytes)
|
|
|
|
s.SetFieldSortOrder(sort)
|
2018-03-29 20:31:43 +00:00
|
|
|
s.SetFieldTypeSupport(typeSupport)
|
2018-03-28 00:30:51 +00:00
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2016-02-10 22:50:07 +00:00
|
|
|
func NewInfluxSerializer() (Serializer, error) {
|
2018-03-28 00:30:51 +00:00
|
|
|
return influx.NewSerializer(), nil
|
2016-02-10 22:50:07 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 00:15:18 +00:00
|
|
|
func NewGraphiteSerializer(prefix, template string, tag_support bool, separator string, templates []string) (Serializer, error) {
|
2020-03-31 18:30:21 +00:00
|
|
|
graphiteTemplates, defaultTemplate, err := graphite.InitGraphiteTemplates(templates)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if defaultTemplate != "" {
|
|
|
|
template = defaultTemplate
|
|
|
|
}
|
|
|
|
|
2020-05-21 00:15:18 +00:00
|
|
|
if separator == "" {
|
|
|
|
separator = "."
|
|
|
|
}
|
|
|
|
|
2016-02-10 22:50:07 +00:00
|
|
|
return &graphite.GraphiteSerializer{
|
2018-05-21 22:59:56 +00:00
|
|
|
Prefix: prefix,
|
|
|
|
Template: template,
|
|
|
|
TagSupport: tag_support,
|
2020-05-21 00:15:18 +00:00
|
|
|
Separator: separator,
|
2020-03-31 18:30:21 +00:00
|
|
|
Templates: graphiteTemplates,
|
2016-02-10 22:50:07 +00:00
|
|
|
}, nil
|
|
|
|
}
|