From 7a521ff73b2da7dc38a84fae5134bea2d6ee7e9e Mon Sep 17 00:00:00 2001 From: Sven Rebhan <36194019+srebhan@users.noreply.github.com> Date: Fri, 26 May 2023 15:51:12 +0200 Subject: [PATCH] chore(serializers.prometheusremotewrite): Migrate to new-style framework (#13341) --- .../serializers/all/prometheusremotewrite.go | 7 +++ .../prometheusremotewrite.go | 49 ++++++++--------- .../prometheusremotewrite_test.go | 54 +++++++------------ plugins/serializers/registry.go | 20 ------- 4 files changed, 48 insertions(+), 82 deletions(-) create mode 100644 plugins/serializers/all/prometheusremotewrite.go diff --git a/plugins/serializers/all/prometheusremotewrite.go b/plugins/serializers/all/prometheusremotewrite.go new file mode 100644 index 000000000..5fa3b4ce9 --- /dev/null +++ b/plugins/serializers/all/prometheusremotewrite.go @@ -0,0 +1,7 @@ +//go:build !custom || serializers || serializers.prometheusremotewrite + +package all + +import ( + _ "github.com/influxdata/telegraf/plugins/serializers/prometheusremotewrite" // register plugin +) diff --git a/plugins/serializers/prometheusremotewrite/prometheusremotewrite.go b/plugins/serializers/prometheusremotewrite/prometheusremotewrite.go index a36247b1f..587a4b16f 100644 --- a/plugins/serializers/prometheusremotewrite/prometheusremotewrite.go +++ b/plugins/serializers/prometheusremotewrite/prometheusremotewrite.go @@ -13,38 +13,15 @@ import ( "github.com/prometheus/prometheus/prompb" "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/plugins/serializers" "github.com/influxdata/telegraf/plugins/serializers/prometheus" ) type MetricKey uint64 -// MetricSortOrder controls if the output is sorted. -type MetricSortOrder int - -const ( - NoSortMetrics MetricSortOrder = iota - SortMetrics -) - -// StringHandling defines how to process string fields. -type StringHandling int - -const ( - DiscardStrings StringHandling = iota - StringAsLabel -) - -type FormatConfig struct { - MetricSortOrder MetricSortOrder - StringHandling StringHandling -} - type Serializer struct { - config FormatConfig -} - -func NewSerializer(config FormatConfig) *Serializer { - return &Serializer{config: config} + SortMetrics bool `toml:"prometheus_sort_metrics"` + StringAsLabel bool `toml:"prometheus_string_as_label"` } func (s *Serializer) Serialize(metric telegraf.Metric) ([]byte, error) { @@ -204,7 +181,7 @@ func (s *Serializer) SerializeBatch(metrics []telegraf.Metric) ([]byte, error) { i++ } - if s.config.MetricSortOrder == SortMetrics { + if s.SortMetrics { sort.Slice(promTS, func(i, j int) bool { lhs := promTS[i].Labels rhs := promTS[j].Labels @@ -274,7 +251,7 @@ func (s *Serializer) appendCommonLabels(labels []prompb.Label, metric telegraf.M labels = append(labels, prompb.Label{Name: name, Value: tag.Value}) } - if s.config.StringHandling != StringAsLabel { + if !s.StringAsLabel { return labels } @@ -342,3 +319,19 @@ func (sl sortableLabels) Less(i, j int) bool { func (sl sortableLabels) Swap(i, j int) { sl[i], sl[j] = sl[j], sl[i] } + +func init() { + serializers.Add("prometheusremotewrite", + func() serializers.Serializer { + return &Serializer{} + }, + ) +} + +// InitFromConfig is a compatibility function to construct the parser the old way +func (s *Serializer) InitFromConfig(cfg *serializers.Config) error { + s.SortMetrics = cfg.PrometheusSortMetrics + s.StringAsLabel = cfg.PrometheusStringAsLabel + + return nil +} diff --git a/plugins/serializers/prometheusremotewrite/prometheusremotewrite_test.go b/plugins/serializers/prometheusremotewrite/prometheusremotewrite_test.go index 77a10204a..d8eab63dc 100644 --- a/plugins/serializers/prometheusremotewrite/prometheusremotewrite_test.go +++ b/plugins/serializers/prometheusremotewrite/prometheusremotewrite_test.go @@ -32,7 +32,7 @@ func BenchmarkRemoteWrite(b *testing.B) { time.Unix(0, 0), ) } - s := NewSerializer(FormatConfig{}) + s := &Serializer{} for n := 0; n < b.N; n++ { _, _ = s.SerializeBatch(batch) } @@ -41,7 +41,6 @@ func BenchmarkRemoteWrite(b *testing.B) { func TestRemoteWriteSerialize(t *testing.T) { tests := []struct { name string - config FormatConfig metric telegraf.Metric expected []byte }{ @@ -186,10 +185,9 @@ http_request_duration_seconds_bucket{le="0.5"} 129389 } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - s := NewSerializer(FormatConfig{ - MetricSortOrder: SortMetrics, - StringHandling: tt.config.StringHandling, - }) + s := &Serializer{ + SortMetrics: true, + } data, err := s.Serialize(tt.metric) require.NoError(t, err) actual, err := prompbToText(data) @@ -203,10 +201,10 @@ http_request_duration_seconds_bucket{le="0.5"} 129389 func TestRemoteWriteSerializeBatch(t *testing.T) { tests := []struct { - name string - config FormatConfig - metrics []telegraf.Metric - expected []byte + name string + metrics []telegraf.Metric + stringAsLabel bool + expected []byte }{ { name: "simple", @@ -515,11 +513,8 @@ cpu_time_idle 42 `), }, { - name: "string as label", - config: FormatConfig{ - MetricSortOrder: SortMetrics, - StringHandling: StringAsLabel, - }, + name: "string as label", + stringAsLabel: true, metrics: []telegraf.Metric{ testutil.MustMetric( "cpu", @@ -536,11 +531,8 @@ cpu_time_idle{cpu="cpu0"} 42 `), }, { - name: "string as label duplicate tag", - config: FormatConfig{ - MetricSortOrder: SortMetrics, - StringHandling: StringAsLabel, - }, + name: "string as label duplicate tag", + stringAsLabel: true, metrics: []telegraf.Metric{ testutil.MustMetric( "cpu", @@ -559,11 +551,8 @@ cpu_time_idle{cpu="cpu0"} 42 `), }, { - name: "replace characters when using string as label", - config: FormatConfig{ - MetricSortOrder: SortMetrics, - StringHandling: StringAsLabel, - }, + name: "replace characters when using string as label", + stringAsLabel: true, metrics: []telegraf.Metric{ testutil.MustMetric( "cpu", @@ -666,11 +655,8 @@ rpc_duration_seconds_sum 17560473 `), }, { - name: "empty label string value", - config: FormatConfig{ - MetricSortOrder: SortMetrics, - StringHandling: StringAsLabel, - }, + name: "empty label string value", + stringAsLabel: true, metrics: []telegraf.Metric{ testutil.MustMetric( "prometheus", @@ -690,10 +676,10 @@ rpc_duration_seconds_sum 17560473 } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - s := NewSerializer(FormatConfig{ - MetricSortOrder: SortMetrics, - StringHandling: tt.config.StringHandling, - }) + s := &Serializer{ + SortMetrics: true, + StringAsLabel: tt.stringAsLabel, + } data, err := s.SerializeBatch(tt.metrics) require.NoError(t, err) actual, err := prompbToText(data) diff --git a/plugins/serializers/registry.go b/plugins/serializers/registry.go index eafcedd4c..33e2900ab 100644 --- a/plugins/serializers/registry.go +++ b/plugins/serializers/registry.go @@ -6,7 +6,6 @@ import ( "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/plugins/serializers/prometheus" - "github.com/influxdata/telegraf/plugins/serializers/prometheusremotewrite" "github.com/influxdata/telegraf/plugins/serializers/splunkmetric" "github.com/influxdata/telegraf/plugins/serializers/wavefront" ) @@ -171,8 +170,6 @@ func NewSerializer(config *Config) (Serializer, error) { ), nil case "prometheus": serializer, err = NewPrometheusSerializer(config), nil - case "prometheusremotewrite": - serializer, err = NewPrometheusRemoteWriteSerializer(config), nil default: creator, found := Serializers[config.DataFormat] if !found { @@ -192,23 +189,6 @@ func NewSerializer(config *Config) (Serializer, error) { return serializer, err } -func NewPrometheusRemoteWriteSerializer(config *Config) Serializer { - sortMetrics := prometheusremotewrite.NoSortMetrics - if config.PrometheusExportTimestamp { - sortMetrics = prometheusremotewrite.SortMetrics - } - - stringAsLabels := prometheusremotewrite.DiscardStrings - if config.PrometheusStringAsLabel { - stringAsLabels = prometheusremotewrite.StringAsLabel - } - - return prometheusremotewrite.NewSerializer(prometheusremotewrite.FormatConfig{ - MetricSortOrder: sortMetrics, - StringHandling: stringAsLabels, - }) -} - func NewPrometheusSerializer(config *Config) Serializer { exportTimestamp := prometheus.NoExportTimestamp if config.PrometheusExportTimestamp {