Merge pull request #17353 from roidelapluie/anchored_smoothed_name_label

promql: allow 'anchored' and 'smoothed' as metric and label names
This commit is contained in:
Julius Volz 2025-10-17 12:33:03 +02:00 committed by GitHub
commit 8bbfdf212c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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{