From 04ae6196f228c26c942494640fa6fc36a6534b45 Mon Sep 17 00:00:00 2001 From: Tobias Schmidt Date: Wed, 7 Sep 2016 15:16:34 -0400 Subject: [PATCH 1/2] Fix parsing of label names which are also keywords The current separation between lexer and parser is a bit fuzzy when it comes to operators, aggregators and other keywords. The lexer already tries to determine the type of a token, even though that type might change depending on the context. This led to the problematic behavior that no tokens known to the lexer could be used as label names, including operators (and, by, ...), aggregators (count, quantile, ...) or other keywords (for, offset, ...). This change additionally checks whether an identifier is one of these types. We might want to check whether the specific item identification should be moved from the lexer to the parser. --- promql/lex.go | 13 +++++++++++++ promql/parse.go | 5 ++++- promql/parse_test.go | 18 ++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/promql/lex.go b/promql/lex.go index 5bbe84364..c3fd4e65e 100644 --- a/promql/lex.go +++ b/promql/lex.go @@ -889,3 +889,16 @@ func isDigit(r rune) bool { func isAlpha(r rune) bool { return r == '_' || ('a' <= r && r <= 'z') || ('A' <= r && r <= 'Z') } + +// isLabel reports whether the string can be used as label. +func isLabel(s string) bool { + if len(s) == 0 || !isAlpha(rune(s[0])) { + return false + } + for _, c := range s[1:] { + if !isAlphaNumeric(c) { + return false + } + } + return true +} diff --git a/promql/parse.go b/promql/parse.go index dbdc662ec..6acaa11de 100644 --- a/promql/parse.go +++ b/promql/parse.go @@ -673,7 +673,10 @@ func (p *parser) labels() model.LabelNames { labels := model.LabelNames{} if p.peek().typ != itemRightParen { for { - id := p.expect(itemIdentifier, ctx) + id := p.next() + if !isLabel(id.val) { + p.errorf("unexpected %s in %s, expected label", id.desc(), ctx) + } labels = append(labels, model.LabelName(id.val)) if p.peek().typ != itemComma { diff --git a/promql/parse_test.go b/promql/parse_test.go index 901c99ab3..191cd39e1 100644 --- a/promql/parse_test.go +++ b/promql/parse_test.go @@ -1225,6 +1225,24 @@ var testExpr = []struct { }, Param: &StringLiteral{"value"}, }, + }, { + // Test usage of keywords as label names. + input: "sum without(and, by, avg, count, alert, annotations)(some_metric)", + expected: &AggregateExpr{ + Op: itemSum, + Without: true, + Expr: &VectorSelector{ + Name: "some_metric", + LabelMatchers: metric.LabelMatchers{ + mustLabelMatcher(metric.Equal, model.MetricNameLabel, "some_metric"), + }, + }, + Grouping: model.LabelNames{"and", "by", "avg", "count", "alert", "annotations"}, + }, + }, { + input: "sum without(==)(some_metric)", + fail: true, + errMsg: "unexpected in grouping opts, expected label", }, { input: `sum some_metric by (test)`, fail: true, From 03831e0e1e5043444233f275be56874b924b8835 Mon Sep 17 00:00:00 2001 From: beorn7 Date: Thu, 8 Sep 2016 14:17:49 +0200 Subject: [PATCH 2/2] Cut 1.1.2 --- CHANGELOG.md | 4 ++++ VERSION | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1f5287c8..9efaae272 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.1.2 / 2016-09-08 + +* [BUGFIX] Allow label names that coincide with keywords. + ## 1.1.1 / 2016-09-07 * [BUGFIX] Fix IPv6 escaping in service discovery integrations diff --git a/VERSION b/VERSION index 524cb5524..45a1b3f44 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.1.1 +1.1.2