promql: allow 'anchored' and 'smoothed' as metric and label names

Add ANCHORED and SMOOTHED keywords to the maybe_label and
metric_identifier rules in the parser grammar, allowing them
to be used as metric names and label names, similar to other
keywords like 'offset', 'step', and 'bool'.

This fixes an issue where expressions like `anchored{job="test"}`
and `sum by (smoothed) (some_metric)` would fail to parse.

Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
This commit is contained in:
Julien Pivotto 2025-10-17 12:10:30 +02:00
parent a28ec9aca0
commit 99c926b810
3 changed files with 509 additions and 446 deletions

View File

@ -696,7 +696,7 @@ metric : metric_identifier label_set
;
metric_identifier: AVG | BOTTOMK | BY | COUNT | COUNT_VALUES | GROUP | IDENTIFIER | LAND | LOR | LUNLESS | MAX | METRIC_IDENTIFIER | MIN | OFFSET | QUANTILE | STDDEV | STDVAR | SUM | TOPK | WITHOUT | START | END | LIMITK | LIMIT_RATIO | STEP;
metric_identifier: AVG | BOTTOMK | BY | COUNT | COUNT_VALUES | GROUP | IDENTIFIER | LAND | LOR | LUNLESS | MAX | METRIC_IDENTIFIER | MIN | OFFSET | QUANTILE | STDDEV | STDVAR | SUM | TOPK | WITHOUT | START | END | LIMITK | LIMIT_RATIO | STEP | ANCHORED | SMOOTHED;
label_set : LEFT_BRACE label_set_list RIGHT_BRACE
{ $$ = labels.New($2...) }
@ -953,7 +953,7 @@ counter_reset_hint : UNKNOWN_COUNTER_RESET | COUNTER_RESET | NOT_COUNTER_RESET |
aggregate_op : AVG | BOTTOMK | COUNT | COUNT_VALUES | GROUP | MAX | MIN | QUANTILE | STDDEV | STDVAR | SUM | TOPK | LIMITK | LIMIT_RATIO;
// Inside of grouping options label names can be recognized as keywords by the lexer. This is a list of keywords that could also be a label name.
maybe_label : AVG | BOOL | BOTTOMK | BY | COUNT | COUNT_VALUES | GROUP | GROUP_LEFT | GROUP_RIGHT | IDENTIFIER | IGNORING | LAND | LOR | LUNLESS | MAX | METRIC_IDENTIFIER | MIN | OFFSET | ON | QUANTILE | STDDEV | STDVAR | SUM | TOPK | START | END | ATAN2 | LIMITK | LIMIT_RATIO | STEP;
maybe_label : AVG | BOOL | BOTTOMK | BY | COUNT | COUNT_VALUES | GROUP | GROUP_LEFT | GROUP_RIGHT | IDENTIFIER | IGNORING | LAND | LOR | LUNLESS | MAX | METRIC_IDENTIFIER | MIN | OFFSET | ON | QUANTILE | STDDEV | STDVAR | SUM | TOPK | START | END | ATAN2 | LIMITK | LIMIT_RATIO | STEP | ANCHORED | SMOOTHED;
unary_op : ADD | SUB;

File diff suppressed because it is too large Load Diff

View File

@ -798,6 +798,28 @@ var testExpr = []struct {
EndPos: 21,
},
},
{
input: `anchored{job="test"}`,
expected: &VectorSelector{
Name: "anchored",
LabelMatchers: []*labels.Matcher{
MustLabelMatcher(labels.MatchEqual, "job", "test"),
MustLabelMatcher(labels.MatchEqual, model.MetricNameLabel, "anchored"),
},
PosRange: posrange.PositionRange{Start: 0, End: 20},
},
},
{
input: `smoothed{job="test"}`,
expected: &VectorSelector{
Name: "smoothed",
LabelMatchers: []*labels.Matcher{
MustLabelMatcher(labels.MatchEqual, "job", "test"),
MustLabelMatcher(labels.MatchEqual, model.MetricNameLabel, "smoothed"),
},
PosRange: posrange.PositionRange{Start: 0, End: 20},
},
},
// Vector binary operations.
{
input: "foo * bar",
@ -2773,6 +2795,36 @@ var testExpr = []struct {
PosRange: posrange.PositionRange{Start: 0, End: 25},
},
},
{
input: "sum by (anchored)(some_metric)",
expected: &AggregateExpr{
Op: SUM,
Expr: &VectorSelector{
Name: "some_metric",
LabelMatchers: []*labels.Matcher{
MustLabelMatcher(labels.MatchEqual, model.MetricNameLabel, "some_metric"),
},
PosRange: posrange.PositionRange{Start: 18, End: 29},
},
Grouping: []string{"anchored"},
PosRange: posrange.PositionRange{Start: 0, End: 30},
},
},
{
input: "sum by (smoothed)(some_metric)",
expected: &AggregateExpr{
Op: SUM,
Expr: &VectorSelector{
Name: "some_metric",
LabelMatchers: []*labels.Matcher{
MustLabelMatcher(labels.MatchEqual, model.MetricNameLabel, "some_metric"),
},
PosRange: posrange.PositionRange{Start: 18, End: 29},
},
Grouping: []string{"smoothed"},
PosRange: posrange.PositionRange{Start: 0, End: 30},
},
},
{
input: `sum by ("foo bar")({"some.metric"})`,
expected: &AggregateExpr{