diff --git a/pkg/tsdb/cloudwatch/log_query.go b/pkg/tsdb/cloudwatch/log_query.go index 1b9bc517b3b..575421f23c2 100644 --- a/pkg/tsdb/cloudwatch/log_query.go +++ b/pkg/tsdb/cloudwatch/log_query.go @@ -205,14 +205,16 @@ func groupResults(results *data.Frame, groupingFieldNames []string, fromSyncQuer if fromSyncQuery { // remove grouping indices newFrame.Fields = removeFieldsByIndex(newFrame.Fields, removeFieldIndices) + groupLabels := generateLabels(groupingFields, i) // set the group key as the display name for sync queries - for i := 1; i < len(newFrame.Fields); i++ { - valueField := newFrame.Fields[i] + for j := 1; j < len(newFrame.Fields); j++ { + valueField := newFrame.Fields[j] if valueField.Config == nil { valueField.Config = &data.FieldConfig{} } valueField.Config.DisplayNameFromDS = groupKey + valueField.Labels = groupLabels } } @@ -275,10 +277,21 @@ func generateGroupKey(fields []*data.Field, row int) string { } } } - return groupKey } +func generateLabels(fields []*data.Field, row int) data.Labels { + labels := data.Labels{} + for _, field := range fields { + if strField, ok := field.At(row).(*string); ok { + if strField != nil { + labels[field.Name] = *strField + } + } + } + return labels +} + func numericFieldToStringField(field *data.Field) (*data.Field, error) { if !field.Type().Numeric() { return nil, fmt.Errorf("field is not numeric") diff --git a/pkg/tsdb/cloudwatch/log_query_test.go b/pkg/tsdb/cloudwatch/log_query_test.go index 125c6c31577..1a72ded1e09 100644 --- a/pkg/tsdb/cloudwatch/log_query_test.go +++ b/pkg/tsdb/cloudwatch/log_query_test.go @@ -583,7 +583,7 @@ func TestGroupingResultsWithFromSyncQueryTrue(t *testing.T) { Name: "fakelog-a1", Fields: []*data.Field{ data.NewField("@timestamp", data.Labels{}, []*time.Time{&timeA, &timeB}), - data.NewField("count", data.Labels{}, []*string{ + data.NewField("count", data.Labels{"@log": "fakelog-a", "stream": "1"}, []*string{ aws.String("100"), aws.String("57"), }), @@ -594,7 +594,7 @@ func TestGroupingResultsWithFromSyncQueryTrue(t *testing.T) { Name: "fakelog-b1", Fields: []*data.Field{ data.NewField("@timestamp", data.Labels{}, []*time.Time{&timeA, &timeB}), - data.NewField("count", data.Labels{}, []*string{ + data.NewField("count", data.Labels{"@log": "fakelog-b", "stream": "1"}, []*string{ aws.String("150"), aws.String("62"), }),