Add prometheus serializer and use it in prometheus output (#6703)
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/influxdata/telegraf/plugins/serializers/influx"
|
||||
"github.com/influxdata/telegraf/plugins/serializers/json"
|
||||
"github.com/influxdata/telegraf/plugins/serializers/nowmetric"
|
||||
"github.com/influxdata/telegraf/plugins/serializers/prometheus"
|
||||
"github.com/influxdata/telegraf/plugins/serializers/splunkmetric"
|
||||
"github.com/influxdata/telegraf/plugins/serializers/wavefront"
|
||||
)
|
||||
@@ -45,43 +46,54 @@ type Serializer interface {
|
||||
// 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
|
||||
DataFormat string `toml:"data_format"`
|
||||
|
||||
// Support tags in graphite protocol
|
||||
GraphiteTagSupport bool
|
||||
GraphiteTagSupport bool `toml:"graphite_tag_support"`
|
||||
|
||||
// Maximum line length in bytes; influx format only
|
||||
InfluxMaxLineBytes int
|
||||
InfluxMaxLineBytes int `toml:"influx_max_line_bytes"`
|
||||
|
||||
// Sort field keys, set to true only when debugging as it less performant
|
||||
// than unsorted fields; influx format only
|
||||
InfluxSortFields bool
|
||||
InfluxSortFields bool `toml:"influx_sort_fields"`
|
||||
|
||||
// Support unsigned integer output; influx format only
|
||||
InfluxUintSupport bool
|
||||
InfluxUintSupport bool `toml:"influx_uint_support"`
|
||||
|
||||
// Prefix to add to all measurements, only supports Graphite
|
||||
Prefix string
|
||||
Prefix string `toml:"prefix"`
|
||||
|
||||
// Template for converting telegraf metrics into Graphite
|
||||
// only supports Graphite
|
||||
Template string
|
||||
Template string `toml:"template"`
|
||||
|
||||
// Timestamp units to use for JSON formatted output
|
||||
TimestampUnits time.Duration
|
||||
TimestampUnits time.Duration `toml:"timestamp_units"`
|
||||
|
||||
// Include HEC routing fields for splunkmetric output
|
||||
HecRouting bool
|
||||
HecRouting bool `toml:"hec_routing"`
|
||||
|
||||
// Enable Splunk MultiMetric output (Splunk 8.0+)
|
||||
SplunkmetricMultiMetric bool
|
||||
SplunkmetricMultiMetric bool `toml:"splunkmetric_multi_metric"`
|
||||
|
||||
// Point tags to use as the source name for Wavefront (if none found, host will be used).
|
||||
WavefrontSourceOverride []string
|
||||
WavefrontSourceOverride []string `toml:"wavefront_source_override"`
|
||||
|
||||
// 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
|
||||
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"`
|
||||
}
|
||||
|
||||
// NewSerializer a Serializer interface based on the given config.
|
||||
@@ -103,12 +115,37 @@ func NewSerializer(config *Config) (Serializer, error) {
|
||||
serializer, err = NewCarbon2Serializer()
|
||||
case "wavefront":
|
||||
serializer, err = NewWavefrontSerializer(config.Prefix, config.WavefrontUseStrict, config.WavefrontSourceOverride)
|
||||
case "prometheus":
|
||||
serializer, err = NewPrometheusSerializer(config)
|
||||
default:
|
||||
err = fmt.Errorf("Invalid data format: %s", config.DataFormat)
|
||||
}
|
||||
return serializer, err
|
||||
}
|
||||
|
||||
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,
|
||||
})
|
||||
}
|
||||
|
||||
func NewWavefrontSerializer(prefix string, useStrict bool, sourceOverride []string) (Serializer, error) {
|
||||
return wavefront.NewSerializer(prefix, useStrict, sourceOverride)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user