CloudWatch Logs: Fix log query display name when used with expressions (#74497)

pull/74859/head^2
Isabella Siu 2 years ago committed by GitHub
parent 2d8f5c1488
commit 4b7b323061
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      pkg/tsdb/cloudwatch/log_query.go
  2. 4
      pkg/tsdb/cloudwatch/log_query_test.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
}

@ -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)

Loading…
Cancel
Save