Fix delete in place of keys in stackdriver output (#5465)

This commit is contained in:
Nicolas Bazire 2019-02-21 21:19:50 +01:00 committed by Daniel Nelson
parent 0a2cc3ac3f
commit 33dfbfdf5e
2 changed files with 75 additions and 2 deletions

View File

@ -199,12 +199,14 @@ func (s *Stackdriver) Write(metrics []telegraf.Metric) error {
for len(buckets) != 0 { for len(buckets) != 0 {
// can send up to 200 time series to stackdriver // can send up to 200 time series to stackdriver
timeSeries := make([]*monitoringpb.TimeSeries, 0, 200) timeSeries := make([]*monitoringpb.TimeSeries, 0, 200)
for i, k := range keys { for i := 0; i < len(keys); i++ {
k := keys[i]
s := buckets[k] s := buckets[k]
timeSeries = append(timeSeries, s[0]) timeSeries = append(timeSeries, s[0])
if len(s) == 1 { if len(s) == 1 {
delete(buckets, k) delete(buckets, k)
keys = append(keys[:i], keys[i+1:]...) keys = append(keys[:i], keys[i+1:]...)
i--
continue continue
} }

View File

@ -253,6 +253,51 @@ func TestWriteBatchable(t *testing.T) {
}, },
time.Unix(1, 0), 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() err = s.Connect()
@ -262,7 +307,7 @@ func TestWriteBatchable(t *testing.T) {
require.Len(t, mockMetric.reqs, 2) require.Len(t, mockMetric.reqs, 2)
request := mockMetric.reqs[0].(*monitoringpb.CreateTimeSeriesRequest) request := mockMetric.reqs[0].(*monitoringpb.CreateTimeSeriesRequest)
require.Len(t, request.TimeSeries, 2) require.Len(t, request.TimeSeries, 6)
ts := request.TimeSeries[0] ts := request.TimeSeries[0]
require.Len(t, ts.Points, 1) require.Len(t, ts.Points, 1)
require.Equal(t, ts.Points[0].Interval, &monitoringpb.TimeInterval{ require.Equal(t, ts.Points[0].Interval, &monitoringpb.TimeInterval{
@ -288,6 +333,32 @@ func TestWriteBatchable(t *testing.T) {
Int64Value: int64(43), 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) { func TestWriteIgnoredErrors(t *testing.T) {