Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PromQL: Support trailing commas in grouping opts #6480

Merged
merged 4 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/querying/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ metric name that also have the `job` label set to `prometheus` and their

http_requests_total{job="prometheus",group="canary"}

The list of label matchers may include a trailing comma:
slrtbtfs marked this conversation as resolved.
Show resolved Hide resolved

http_requests_total{job="prometheus",group="canary",}

It is also possible to negatively match a label value, or to match label values
against regular expressions. The following label matching operators exist:

Expand Down
3 changes: 3 additions & 0 deletions docs/querying/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ all other labels are preserved the output. `by` does the opposite and drops
labels that are not listed in the `by` clause, even if their label values are
identical between all elements of the vector.

`label_list` is a list of unquoted labels that may include a trailing comma, i.e.
slrtbtfs marked this conversation as resolved.
Show resolved Hide resolved
both `(label1, label2)` and `(label1, label2,)` are valid syntax.

`count_values` outputs one time series per unique sample value. Each series has
an additional label. The name of that label is given by the aggregation
parameter, and the label value is the unique sample value. The value of each
Expand Down
2 changes: 2 additions & 0 deletions promql/generated_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ label_set_item :
grouping_labels :
LEFT_PAREN grouping_label_list RIGHT_PAREN
{ $$ = $2 }
| LEFT_PAREN grouping_label_list COMMA RIGHT_PAREN
{ $$ = $2 }
| LEFT_PAREN RIGHT_PAREN
{ $$ = []string{} }
| error
Expand Down
107 changes: 58 additions & 49 deletions promql/generated_parser.y.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions promql/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,30 @@ var testExpr = []struct {
},
Grouping: []string{},
},
}, {
input: "sum by (foo,bar,)(some_metric)",
expected: &AggregateExpr{
Op: SUM,
Expr: &VectorSelector{
Name: "some_metric",
LabelMatchers: []*labels.Matcher{
mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"),
},
},
Grouping: []string{"foo", "bar"},
},
}, {
input: "sum by (foo,)(some_metric)",
expected: &AggregateExpr{
Op: SUM,
Expr: &VectorSelector{
Name: "some_metric",
LabelMatchers: []*labels.Matcher{
mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"),
},
},
Grouping: []string{"foo"},
},
}, {
input: "topk(5, some_metric)",
expected: &AggregateExpr{
Expand Down Expand Up @@ -1232,6 +1256,14 @@ var testExpr = []struct {
input: "sum without(==)(some_metric)",
fail: true,
errMsg: "unexpected <op:==> in grouping opts, expected label",
}, {
input: "sum without(,)(some_metric)",
fail: true,
errMsg: `unexpected "," in grouping opts, expected label`,
}, {
input: "sum without(foo,,)(some_metric)",
fail: true,
errMsg: `unexpected "," in grouping opts, expected label`,
}, {
input: `sum some_metric by (test)`,
fail: true,
Expand Down