diff --git a/pkg/tsdb/cloudwatch/log_query.go b/pkg/tsdb/cloudwatch/log_query.go index 71ce7b19056..1b9bc517b3b 100644 --- a/pkg/tsdb/cloudwatch/log_query.go +++ b/pkg/tsdb/cloudwatch/log_query.go @@ -163,7 +163,7 @@ func changeToStringField(lengthOfValues int, rows [][]*cloudwatchlogs.ResultFiel return fieldValuesAsStrings } -func groupResults(results *data.Frame, groupingFieldNames []string, removeNonTime bool) ([]*data.Frame, error) { +func groupResults(results *data.Frame, groupingFieldNames []string, fromSyncQuery bool) ([]*data.Frame, error) { groupingFields := make([]*data.Field, 0) removeFieldIndices := make([]int, 0) @@ -180,7 +180,7 @@ func groupResults(results *data.Frame, groupingFieldNames []string, removeNonTim field = newField } // For expressions and alerts to work properly we need to remove non-time grouping fields - if removeNonTime && !field.Type().Time() { + if fromSyncQuery && !field.Type().Time() { removeFieldIndices = append(removeFieldIndices, i) } @@ -202,8 +202,20 @@ func groupResults(results *data.Frame, groupingFieldNames []string, removeNonTim newFrame := results.EmptyCopy() newFrame.Name = groupKey newFrame.Meta = results.Meta - // remove grouping indices - newFrame.Fields = removeFieldsByIndex(newFrame.Fields, removeFieldIndices) + if fromSyncQuery { + // remove grouping indices + newFrame.Fields = removeFieldsByIndex(newFrame.Fields, removeFieldIndices) + + // set the group key as the display name for sync queries + for i := 1; i < len(newFrame.Fields); i++ { + valueField := newFrame.Fields[i] + if valueField.Config == nil { + valueField.Config = &data.FieldConfig{} + } + valueField.Config.DisplayNameFromDS = groupKey + } + } + groupedDataFrames[groupKey] = newFrame } diff --git a/pkg/tsdb/cloudwatch/log_query_test.go b/pkg/tsdb/cloudwatch/log_query_test.go index b5bbeccbcd0..125c6c31577 100644 --- a/pkg/tsdb/cloudwatch/log_query_test.go +++ b/pkg/tsdb/cloudwatch/log_query_test.go @@ -543,7 +543,7 @@ func TestGroupingResultsWithNumericField(t *testing.T) { assert.ElementsMatch(t, expectedGroupedFrames, groupedResults) } -func TestGroupingResultsWithRemoveNonTimeTrue(t *testing.T) { +func TestGroupingResultsWithFromSyncQueryTrue(t *testing.T) { logField := data.NewField("@log", data.Labels{}, []*string{ aws.String("fakelog-a"), aws.String("fakelog-b"), @@ -602,6 +602,8 @@ func TestGroupingResultsWithRemoveNonTimeTrue(t *testing.T) { RefID: "", }, } + expectedGroupedFrames[0].Fields[1].Config = &data.FieldConfig{DisplayNameFromDS: "fakelog-a1"} + expectedGroupedFrames[1].Fields[1].Config = &data.FieldConfig{DisplayNameFromDS: "fakelog-b1"} groupedResults, err := groupResults(fakeDataFrame, []string{"@log", "stream"}, true) require.NoError(t, err)