From c90e7146b0ebe2d2b943e3173b12ba366429f018 Mon Sep 17 00:00:00 2001 From: Daniel Mitterdorfer Date: Tue, 9 Jan 2024 14:55:51 +0100 Subject: [PATCH] Write metrics tags only once to Elasticsearch With this commit we remove the duplicate `MetricTags` and `SampleTags` properties and replace them with a `Tags` property. Relates #3 Closes #20 --- pkg/esoutput/esoutput.go | 51 ++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/pkg/esoutput/esoutput.go b/pkg/esoutput/esoutput.go index d4192e4..d320f62 100644 --- a/pkg/esoutput/esoutput.go +++ b/pkg/esoutput/esoutput.go @@ -47,8 +47,7 @@ type elasticMetricEntry struct { MetricName string MetricType string Value float64 - MetricTags map[string]string - SampleTags map[string]string + Tags map[string]string Time time.Time } @@ -183,33 +182,29 @@ func (o *Output) flush() { samples := samplesContainer.GetSamples() for _, sample := range samples { - for _, entry := range sample.GetSamples() { - mappedEntry := elasticMetricEntry{ - MetricName: entry.Metric.Name, - MetricType: entry.Metric.Type.String(), - Value: entry.Value, - MetricTags: entry.GetTags().Map(), - SampleTags: sample.GetTags().Map(), - Time: sample.Time, - } - data, err := json.Marshal(mappedEntry) - if err != nil { - o.logger.Fatalf("Cannot encode document: %s, %s", err, mappedEntry) - } - var item = esutil.BulkIndexerItem{ - Action: "index", - Body: bytes.NewReader(data), - OnFailure: o.blkItemErrHandler, - } - err = o.bulkIndexer.Add( - context.Background(), - item, - ) - if err != nil { - log.Fatalf("Unexpected error: %s", err) - } + mappedEntry := elasticMetricEntry{ + MetricName: sample.Metric.Name, + MetricType: sample.Metric.Type.String(), + Value: sample.Value, + Tags: sample.GetTags().Map(), + Time: sample.Time, + } + data, err := json.Marshal(mappedEntry) + if err != nil { + o.logger.Fatalf("Cannot encode document: %s, %s", err, mappedEntry) + } + var item = esutil.BulkIndexerItem{ + Action: "index", + Body: bytes.NewReader(data), + OnFailure: o.blkItemErrHandler, + } + err = o.bulkIndexer.Add( + context.Background(), + item, + ) + if err != nil { + log.Fatalf("Unexpected error: %s", err) } - } } }