From 33dfbfdf5e7652ac7703e4cec48c7b65e1704e78 Mon Sep 17 00:00:00 2001 From: Nicolas Bazire Date: Thu, 21 Feb 2019 21:19:50 +0100 Subject: [PATCH] Fix delete in place of keys in stackdriver output (#5465) --- plugins/outputs/stackdriver/stackdriver.go | 4 +- .../outputs/stackdriver/stackdriver_test.go | 73 ++++++++++++++++++- 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/plugins/outputs/stackdriver/stackdriver.go b/plugins/outputs/stackdriver/stackdriver.go index c7d9e45bc..d57675bc3 100644 --- a/plugins/outputs/stackdriver/stackdriver.go +++ b/plugins/outputs/stackdriver/stackdriver.go @@ -199,12 +199,14 @@ func (s *Stackdriver) Write(metrics []telegraf.Metric) error { for len(buckets) != 0 { // can send up to 200 time series to stackdriver timeSeries := make([]*monitoringpb.TimeSeries, 0, 200) - for i, k := range keys { + for i := 0; i < len(keys); i++ { + k := keys[i] s := buckets[k] timeSeries = append(timeSeries, s[0]) if len(s) == 1 { delete(buckets, k) keys = append(keys[:i], keys[i+1:]...) + i-- continue } diff --git a/plugins/outputs/stackdriver/stackdriver_test.go b/plugins/outputs/stackdriver/stackdriver_test.go index 151c84020..7ddaa4485 100644 --- a/plugins/outputs/stackdriver/stackdriver_test.go +++ b/plugins/outputs/stackdriver/stackdriver_test.go @@ -253,6 +253,51 @@ func TestWriteBatchable(t *testing.T) { }, time.Unix(1, 0), ), + testutil.MustMetric("ram", + map[string]string{ + "foo": "bar", + }, + map[string]interface{}{ + "value": 42, + }, + time.Unix(4, 0), + ), + testutil.MustMetric("ram", + map[string]string{ + "foo": "foo", + }, + map[string]interface{}{ + "value": 43, + }, + time.Unix(5, 0), + ), + testutil.MustMetric("ram", + map[string]string{ + "foo": "bar", + }, + map[string]interface{}{ + "value": 43, + }, + time.Unix(3, 0), + ), + testutil.MustMetric("disk", + map[string]string{ + "foo": "foo", + }, + map[string]interface{}{ + "value": 43, + }, + time.Unix(3, 0), + ), + testutil.MustMetric("disk", + map[string]string{ + "foo": "bar", + }, + map[string]interface{}{ + "value": 43, + }, + time.Unix(1, 0), + ), } err = s.Connect() @@ -262,7 +307,7 @@ func TestWriteBatchable(t *testing.T) { require.Len(t, mockMetric.reqs, 2) request := mockMetric.reqs[0].(*monitoringpb.CreateTimeSeriesRequest) - require.Len(t, request.TimeSeries, 2) + require.Len(t, request.TimeSeries, 6) ts := request.TimeSeries[0] require.Len(t, ts.Points, 1) require.Equal(t, ts.Points[0].Interval, &monitoringpb.TimeInterval{ @@ -288,6 +333,32 @@ func TestWriteBatchable(t *testing.T) { Int64Value: int64(43), }, }) + + ts = request.TimeSeries[2] + require.Len(t, ts.Points, 1) + require.Equal(t, ts.Points[0].Interval, &monitoringpb.TimeInterval{ + EndTime: &googlepb.Timestamp{ + Seconds: 3, + }, + }) + require.Equal(t, ts.Points[0].Value, &monitoringpb.TypedValue{ + Value: &monitoringpb.TypedValue_Int64Value{ + Int64Value: int64(43), + }, + }) + + ts = request.TimeSeries[4] + require.Len(t, ts.Points, 1) + require.Equal(t, ts.Points[0].Interval, &monitoringpb.TimeInterval{ + EndTime: &googlepb.Timestamp{ + Seconds: 5, + }, + }) + require.Equal(t, ts.Points[0].Value, &monitoringpb.TypedValue{ + Value: &monitoringpb.TypedValue_Int64Value{ + Int64Value: int64(43), + }, + }) } func TestWriteIgnoredErrors(t *testing.T) {