diff --git a/rules/ast/ast.go b/rules/ast/ast.go index a6f9e0195..7e891edd8 100644 --- a/rules/ast/ast.go +++ b/rules/ast/ast.go @@ -376,6 +376,7 @@ func (node *VectorAggregation) labelsToGroupingKey(labels clientmodel.Metric) ui summer := fnv.New64a() for _, label := range node.groupBy { fmt.Fprint(summer, labels[label]) + fmt.Fprint(summer, []byte{0}) } return summer.Sum64() diff --git a/rules/helpers_test.go b/rules/helpers_test.go index ab1da04e8..867f66891 100644 --- a/rules/helpers_test.go +++ b/rules/helpers_test.go @@ -184,6 +184,27 @@ var testMatrix = ast.Matrix{ }, Values: append(getTestValueStream(0, 90, 10, testStartTime), getTestValueStream(0, 0, 10, testStartTime.Add(testSampleInterval*10))...), }, + // For label-key grouping regression test. + { + Metric: clientmodel.COWMetric{ + Metric: clientmodel.Metric{ + clientmodel.MetricNameLabel: "label_grouping_test", + "a": "aa", + "b": "bb", + }, + }, + Values: getTestValueStream(0, 100, 10, testStartTime), + }, + { + Metric: clientmodel.COWMetric{ + Metric: clientmodel.Metric{ + clientmodel.MetricNameLabel: "label_grouping_test", + "a": "a", + "b": "abb", + }, + }, + Values: getTestValueStream(0, 200, 20, testStartTime), + }, } var testVector = getTestVectorFromTestMatrix(testMatrix) diff --git a/rules/rules_test.go b/rules/rules_test.go index 2661e24ab..61f3b9c4e 100644 --- a/rules/rules_test.go +++ b/rules/rules_test.go @@ -553,6 +553,8 @@ func TestExpressions(t *testing.T) { `testcounter_reset_end => 0 @[%v]`, `testcounter_reset_middle => 50 @[%v]`, `x{y="testvalue"} => 100 @[%v]`, + `label_grouping_test{a="a", b="abb"} => 200 @[%v]`, + `label_grouping_test{a="aa", b="bb"} => 100 @[%v]`, }, }, { @@ -641,6 +643,14 @@ func TestExpressions(t *testing.T) { expr: `sum(http_requests) offset 5m`, shouldFail: true, }, + // Regression test for missing separator byte in labelsToGroupingKey. + { + expr: `sum(label_grouping_test) by (a, b)`, + output: []string{ + `{a="a", b="abb"} => 200 @[%v]`, + `{a="aa", b="bb"} => 100 @[%v]`, + }, + }, } storage, closer := newTestStorage(t)