diff --git a/promql/ast.go b/promql/ast.go index f8eaa3e15..fa9812fdd 100644 --- a/promql/ast.go +++ b/promql/ast.go @@ -101,11 +101,11 @@ type Expressions []Expr // AggregateExpr represents an aggregation operation on a vector. type AggregateExpr struct { - Op itemType // The used aggregation operation. - Expr Expr // The vector expression over which is aggregated. - Grouping model.LabelNames // The labels by which to group the vector. - Without bool // Whether to drop the given labels rather than keep them. - KeepExtraLabels bool // Whether to keep extra labels common among result elements. + Op itemType // The used aggregation operation. + Expr Expr // The vector expression over which is aggregated. + Grouping model.LabelNames // The labels by which to group the vector. + Without bool // Whether to drop the given labels rather than keep them. + KeepCommonLabels bool // Whether to keep common labels among result elements. } // BinaryExpr represents a binary expression between two child expressions. diff --git a/promql/engine.go b/promql/engine.go index b0742ad8d..0bfcbe2d8 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -610,7 +610,7 @@ func (ev *evaluator) eval(expr Expr) model.Value { switch e := expr.(type) { case *AggregateExpr: vector := ev.evalVector(e.Expr) - return ev.aggregation(e.Op, e.Grouping, e.Without, e.KeepExtraLabels, vector) + return ev.aggregation(e.Op, e.Grouping, e.Without, e.KeepCommonLabels, vector) case *BinaryExpr: lhs := ev.evalOneOf(e.LHS, model.ValScalar, model.ValVector) @@ -1062,7 +1062,7 @@ type groupedAggregation struct { } // aggregation evaluates an aggregation operation on a vector. -func (ev *evaluator) aggregation(op itemType, grouping model.LabelNames, without bool, keepExtra bool, vec vector) vector { +func (ev *evaluator) aggregation(op itemType, grouping model.LabelNames, without bool, keepCommon bool, vec vector) vector { result := map[uint64]*groupedAggregation{} @@ -1086,7 +1086,7 @@ func (ev *evaluator) aggregation(op itemType, grouping model.LabelNames, without // Add a new group if it doesn't exist. if !ok { var m metric.Metric - if keepExtra { + if keepCommon { m = sample.Metric m.Del(model.MetricNameLabel) } else if without { @@ -1111,7 +1111,7 @@ func (ev *evaluator) aggregation(op itemType, grouping model.LabelNames, without continue } // Add the sample to the existing group. - if keepExtra { + if keepCommon { groupedResult.labels = labelIntersection(groupedResult.labels, sample.Metric) } diff --git a/promql/lex.go b/promql/lex.go index 7201fbf37..e37460e3b 100644 --- a/promql/lex.go +++ b/promql/lex.go @@ -179,6 +179,7 @@ const ( itemSummary itemDescription itemRunbook + itemKeepExtra keywordsEnd ) @@ -198,25 +199,25 @@ var key = map[string]itemType{ "stdvar": itemStdvar, // Keywords. - "alert": itemAlert, - "if": itemIf, - "for": itemFor, - "labels": itemLabels, - "annotations": itemAnnotations, - "offset": itemOffset, - "by": itemBy, - "without": itemWithout, - "keeping_extra": itemKeepCommon, - "keep_common": itemKeepCommon, - "on": itemOn, - "ignoring": itemIgnoring, - "group_left": itemGroupLeft, - "group_right": itemGroupRight, - "bool": itemBool, + "alert": itemAlert, + "if": itemIf, + "for": itemFor, + "labels": itemLabels, + "annotations": itemAnnotations, + "offset": itemOffset, + "by": itemBy, + "without": itemWithout, + "keep_common": itemKeepCommon, + "on": itemOn, + "ignoring": itemIgnoring, + "group_left": itemGroupLeft, + "group_right": itemGroupRight, + "bool": itemBool, // Removed keywords. Just here to detect and print errors. - "summary": itemSummary, - "description": itemDescription, - "runbook": itemRunbook, + "summary": itemSummary, + "description": itemDescription, + "runbook": itemRunbook, + "keeping_extra": itemKeepExtra, } // These are the default string representations for common items. It does not @@ -411,6 +412,8 @@ func (l *lexer) nextItem() item { t := item.typ if t == itemSummary || t == itemDescription || t == itemRunbook { log.Errorf("Token %q is not valid anymore. Alerting rule syntax has changed with version 0.17.0. Please read https://prometheus.io/docs/alerting/rules/.", item) + } else if t == itemKeepExtra { + log.Error("Token 'keeping_extra' is not valid anymore. Use 'keep_common' instead.") } return item } diff --git a/promql/lex_test.go b/promql/lex_test.go index 49ab711f9..492a3aefc 100644 --- a/promql/lex_test.go +++ b/promql/lex_test.go @@ -248,9 +248,6 @@ var tests = []struct { { input: "alert", expected: []item{{itemAlert, 0, "alert"}}, - }, { - input: "keeping_extra", - expected: []item{{itemKeepCommon, 0, "keeping_extra"}}, }, { input: "keep_common", expected: []item{{itemKeepCommon, 0, "keep_common"}}, diff --git a/promql/parse.go b/promql/parse.go index 2ee4e7f7e..cac58617e 100644 --- a/promql/parse.go +++ b/promql/parse.go @@ -695,7 +695,7 @@ func (p *parser) aggrExpr() *AggregateExpr { p.errorf("expected aggregation operator but got %s", agop) } var grouping model.LabelNames - var keepExtra, without bool + var keepCommon, without bool modifiersFirst := false @@ -709,7 +709,7 @@ func (p *parser) aggrExpr() *AggregateExpr { } if p.peek().typ == itemKeepCommon { p.next() - keepExtra = true + keepCommon = true modifiersFirst = true } @@ -730,20 +730,20 @@ func (p *parser) aggrExpr() *AggregateExpr { } if p.peek().typ == itemKeepCommon { p.next() - keepExtra = true + keepCommon = true } } - if keepExtra && without { + if keepCommon && without { p.errorf("cannot use 'keep_common' with 'without'") } return &AggregateExpr{ - Op: agop.typ, - Expr: e, - Grouping: grouping, - Without: without, - KeepExtraLabels: keepExtra, + Op: agop.typ, + Expr: e, + Grouping: grouping, + Without: without, + KeepCommonLabels: keepCommon, } } diff --git a/promql/parse_test.go b/promql/parse_test.go index e5d1ea8a3..4b9791fe2 100644 --- a/promql/parse_test.go +++ b/promql/parse_test.go @@ -1020,8 +1020,8 @@ var testExpr = []struct { }, { input: "sum by (foo) keep_common (some_metric)", expected: &AggregateExpr{ - Op: itemSum, - KeepExtraLabels: true, + Op: itemSum, + KeepCommonLabels: true, Expr: &VectorSelector{ Name: "some_metric", LabelMatchers: metric.LabelMatchers{ @@ -1033,8 +1033,8 @@ var testExpr = []struct { }, { input: "sum (some_metric) by (foo,bar) keep_common", expected: &AggregateExpr{ - Op: itemSum, - KeepExtraLabels: true, + Op: itemSum, + KeepCommonLabels: true, Expr: &VectorSelector{ Name: "some_metric", LabelMatchers: metric.LabelMatchers{ @@ -1065,8 +1065,8 @@ var testExpr = []struct { {Type: metric.Equal, Name: model.MetricNameLabel, Value: "some_metric"}, }, }, - Grouping: model.LabelNames{"foo"}, - KeepExtraLabels: true, + Grouping: model.LabelNames{"foo"}, + KeepCommonLabels: true, }, }, { input: "MIN (some_metric) by (foo) keep_common", @@ -1078,8 +1078,8 @@ var testExpr = []struct { {Type: metric.Equal, Name: model.MetricNameLabel, Value: "some_metric"}, }, }, - Grouping: model.LabelNames{"foo"}, - KeepExtraLabels: true, + Grouping: model.LabelNames{"foo"}, + KeepCommonLabels: true, }, }, { input: "max by (foo)(some_metric)", diff --git a/promql/printer.go b/promql/printer.go index 82ac53812..a6d38b1ad 100644 --- a/promql/printer.go +++ b/promql/printer.go @@ -145,7 +145,7 @@ func (node *AggregateExpr) String() string { } aggrString = fmt.Sprintf(format, aggrString, node.Grouping) } - if node.KeepExtraLabels { + if node.KeepCommonLabels { aggrString += " KEEP_COMMON" } return aggrString