From c60765a178dcb4ad8b4527262d6abb10aa5b0a54 Mon Sep 17 00:00:00 2001 From: Andrej Ocenas Date: Thu, 14 May 2020 14:10:23 +0200 Subject: [PATCH] CloudWatch/Logs: Fix panic on multiple aggregations queries (#24683) --- pkg/tsdb/cloudwatch/log_query.go | 11 +++++------ pkg/tsdb/cloudwatch/log_query_test.go | 2 ++ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/pkg/tsdb/cloudwatch/log_query.go b/pkg/tsdb/cloudwatch/log_query.go index 2236d2c6e61..e7f68488b50 100644 --- a/pkg/tsdb/cloudwatch/log_query.go +++ b/pkg/tsdb/cloudwatch/log_query.go @@ -8,14 +8,13 @@ import ( ) func logsResultsToDataframes(response *cloudwatchlogs.GetQueryResultsOutput) (*data.Frame, error) { - rowCount := len(response.Results) fieldValues := make(map[string]interface{}) // Maintaining a list of field names in the order returned from CloudWatch // as just iterating over fieldValues would not give a consistent order fieldNames := make([]*string, 0) - for i, row := range response.Results { + for _, row := range response.Results { for _, resultField := range row { // Strip @ptr field from results as it's not needed if *resultField.Field == "@ptr" { @@ -27,9 +26,9 @@ func logsResultsToDataframes(response *cloudwatchlogs.GetQueryResultsOutput) (*d // Check if field is time field if _, err := time.Parse(cloudWatchTSFormat, *resultField.Value); err == nil { - fieldValues[*resultField.Field] = make([]*time.Time, rowCount) + fieldValues[*resultField.Field] = make([]*time.Time, 0) } else { - fieldValues[*resultField.Field] = make([]*string, rowCount) + fieldValues[*resultField.Field] = make([]*string, 0) } } @@ -39,9 +38,9 @@ func logsResultsToDataframes(response *cloudwatchlogs.GetQueryResultsOutput) (*d return nil, err } - timeField[i] = &parsedTime + fieldValues[*resultField.Field] = append(timeField, &parsedTime) } else { - fieldValues[*resultField.Field].([]*string)[i] = resultField.Value + fieldValues[*resultField.Field] = append(fieldValues[*resultField.Field].([]*string), resultField.Value) } } } diff --git a/pkg/tsdb/cloudwatch/log_query_test.go b/pkg/tsdb/cloudwatch/log_query_test.go index 3c6a8168f51..9819be363fe 100644 --- a/pkg/tsdb/cloudwatch/log_query_test.go +++ b/pkg/tsdb/cloudwatch/log_query_test.go @@ -78,6 +78,8 @@ func TestLogsResultsToDataframes(t *testing.T) { Value: aws.String("fakelog"), }, }, + // Sometimes cloudwatch returns empty row + {}, { &cloudwatchlogs.ResultField{ Field: aws.String("@ptr"),