Support UTF-8 label matchers: Add new parser ()

* Add label matchers parser

This commit adds the new label matchers parser as proposed in .
Included is a number of compliance tests comparing the grammar
supported in the new parser with the existing parser in pkg/labels.

Signed-off-by: George Robinson <george.robinson@grafana.com>
---------

Signed-off-by: George Robinson <george.robinson@grafana.com>
This commit is contained in:
George Robinson 2023-09-05 11:32:58 +01:00 committed by GitHub
parent 87d3ee7554
commit 353c0a1304
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 2148 additions and 0 deletions

View File

@ -0,0 +1,393 @@
// Copyright 2023 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package compliance
import (
"reflect"
"testing"
"github.com/prometheus/alertmanager/matchers/parse"
"github.com/prometheus/alertmanager/pkg/labels"
)
func TestCompliance(t *testing.T) {
for _, tc := range []struct {
input string
want labels.Matchers
err string
skip bool
}{
{
input: `{}`,
want: labels.Matchers{},
skip: true,
},
{
input: `{foo='}`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "'")
return append(ms, m)
}(),
skip: true,
},
{
input: "{foo=`}",
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "`")
return append(ms, m)
}(),
skip: true,
},
{
input: "{foo=\\\"}",
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "\"")
return append(ms, m)
}(),
skip: true,
},
{
input: `{foo=bar}`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "bar")
return append(ms, m)
}(),
},
{
input: `{foo="bar"}`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "bar")
return append(ms, m)
}(),
},
{
input: `{foo=~bar.*}`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchRegexp, "foo", "bar.*")
return append(ms, m)
}(),
},
{
input: `{foo=~"bar.*"}`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchRegexp, "foo", "bar.*")
return append(ms, m)
}(),
},
{
input: `{foo!=bar}`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchNotEqual, "foo", "bar")
return append(ms, m)
}(),
},
{
input: `{foo!="bar"}`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchNotEqual, "foo", "bar")
return append(ms, m)
}(),
},
{
input: `{foo!~bar.*}`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchNotRegexp, "foo", "bar.*")
return append(ms, m)
}(),
},
{
input: `{foo!~"bar.*"}`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchNotRegexp, "foo", "bar.*")
return append(ms, m)
}(),
},
{
input: `{foo="bar", baz!="quux"}`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "bar")
m2, _ := labels.NewMatcher(labels.MatchNotEqual, "baz", "quux")
return append(ms, m, m2)
}(),
},
{
input: `{foo="bar", baz!~"quux.*"}`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "bar")
m2, _ := labels.NewMatcher(labels.MatchNotRegexp, "baz", "quux.*")
return append(ms, m, m2)
}(),
},
{
input: `{foo="bar",baz!~".*quux", derp="wat"}`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "bar")
m2, _ := labels.NewMatcher(labels.MatchNotRegexp, "baz", ".*quux")
m3, _ := labels.NewMatcher(labels.MatchEqual, "derp", "wat")
return append(ms, m, m2, m3)
}(),
},
{
input: `{foo="bar", baz!="quux", derp="wat"}`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "bar")
m2, _ := labels.NewMatcher(labels.MatchNotEqual, "baz", "quux")
m3, _ := labels.NewMatcher(labels.MatchEqual, "derp", "wat")
return append(ms, m, m2, m3)
}(),
},
{
input: `{foo="bar", baz!~".*quux.*", derp="wat"}`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "bar")
m2, _ := labels.NewMatcher(labels.MatchNotRegexp, "baz", ".*quux.*")
m3, _ := labels.NewMatcher(labels.MatchEqual, "derp", "wat")
return append(ms, m, m2, m3)
}(),
},
{
input: `{foo="bar", instance=~"some-api.*"}`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "bar")
m2, _ := labels.NewMatcher(labels.MatchRegexp, "instance", "some-api.*")
return append(ms, m, m2)
}(),
},
{
input: `{foo=""}`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "")
return append(ms, m)
}(),
},
{
input: `{foo="bar,quux", job="job1"}`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "bar,quux")
m2, _ := labels.NewMatcher(labels.MatchEqual, "job", "job1")
return append(ms, m, m2)
}(),
},
{
input: `{foo = "bar", dings != "bums", }`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "bar")
m2, _ := labels.NewMatcher(labels.MatchNotEqual, "dings", "bums")
return append(ms, m, m2)
}(),
},
{
input: `foo=bar,dings!=bums`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "bar")
m2, _ := labels.NewMatcher(labels.MatchNotEqual, "dings", "bums")
return append(ms, m, m2)
}(),
},
{
input: `{quote="She said: \"Hi, ladies! That's gender-neutral…\""}`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchEqual, "quote", `She said: "Hi, ladies! That's gender-neutral…"`)
return append(ms, m)
}(),
},
{
input: `statuscode=~"5.."`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchRegexp, "statuscode", "5..")
return append(ms, m)
}(),
},
{
input: `tricky=~~~`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchRegexp, "tricky", "~~")
return append(ms, m)
}(),
skip: true,
},
{
input: `trickier==\\=\=\"`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchEqual, "trickier", `=\=\="`)
return append(ms, m)
}(),
skip: true,
},
{
input: `contains_quote != "\"" , contains_comma !~ "foo,bar" , `,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchNotEqual, "contains_quote", `"`)
m2, _ := labels.NewMatcher(labels.MatchNotRegexp, "contains_comma", "foo,bar")
return append(ms, m, m2)
}(),
},
{
input: `{foo=bar}}`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "bar}")
return append(ms, m)
}(),
skip: true,
},
{
input: `{foo=bar}},}`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "bar}}")
return append(ms, m)
}(),
skip: true,
},
{
input: `{foo=,bar=}}`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m1, _ := labels.NewMatcher(labels.MatchEqual, "foo", "")
m2, _ := labels.NewMatcher(labels.MatchEqual, "bar", "}")
return append(ms, m1, m2)
}(),
skip: true,
},
{
input: `job=`,
want: func() labels.Matchers {
m, _ := labels.NewMatcher(labels.MatchEqual, "job", "")
return labels.Matchers{m}
}(),
skip: true,
},
{
input: `{name-with-dashes = "bar"}`,
want: func() labels.Matchers {
m, _ := labels.NewMatcher(labels.MatchEqual, "name-with-dashes", "bar")
return labels.Matchers{m}
}(),
},
{
input: `{,}`,
err: "bad matcher format: ",
},
{
input: `job="value`,
err: `matcher value contains unescaped double quote: "value`,
},
{
input: `job=value"`,
err: `matcher value contains unescaped double quote: value"`,
},
{
input: `trickier==\\=\=\""`,
err: `matcher value contains unescaped double quote: =\\=\=\""`,
},
{
input: `contains_unescaped_quote = foo"bar`,
err: `matcher value contains unescaped double quote: foo"bar`,
},
{
input: `{foo=~"invalid[regexp"}`,
err: "error parsing regexp: missing closing ]: `[regexp)$`",
},
// Double escaped strings.
{
input: `"{foo=\"bar"}`,
err: `bad matcher format: "{foo=\"bar"`,
},
{
input: `"foo=\"bar"`,
err: `bad matcher format: "foo=\"bar"`,
},
{
input: `"foo=\"bar\""`,
err: `bad matcher format: "foo=\"bar\""`,
},
{
input: `"foo=\"bar\"`,
err: `bad matcher format: "foo=\"bar\"`,
},
{
input: `"{foo=\"bar\"}"`,
err: `bad matcher format: "{foo=\"bar\"}"`,
},
{
input: `"foo="bar""`,
err: `bad matcher format: "foo="bar""`,
},
{
input: `{{foo=`,
err: `bad matcher format: {foo=`,
},
{
input: `{foo=`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "")
return append(ms, m)
}(),
skip: true,
},
{
input: `{foo=}b`,
want: func() labels.Matchers {
ms := labels.Matchers{}
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "}b")
return append(ms, m)
}(),
skip: true,
},
} {
t.Run(tc.input, func(t *testing.T) {
if tc.skip {
t.Skip()
}
got, err := parse.Matchers(tc.input)
if err != nil && tc.err == "" {
t.Fatalf("got error where none expected: %v", err)
}
if err == nil && tc.err != "" {
t.Fatalf("expected error but got none: %v", tc.err)
}
if !reflect.DeepEqual(got, tc.want) {
t.Fatalf("labels not equal:\ngot %#v\nwant %#v", got, tc.want)
}
})
}
}

View File

@ -0,0 +1,39 @@
// Copyright 2023 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package parse
import (
"testing"
)
const (
simpleExample = "{foo=\"bar\"}"
complexExample = "{foo=\"bar\",bar=~\"[a-zA-Z0-9+]\"}"
)
func BenchmarkParseSimple(b *testing.B) {
for i := 0; i < b.N; i++ {
if _, err := Matchers(simpleExample); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkParseComplex(b *testing.B) {
for i := 0; i < b.N; i++ {
if _, err := Matchers(complexExample); err != nil {
b.Fatal(err)
}
}
}

View File

@ -0,0 +1,29 @@
// Copyright 2023 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package parse
import (
"testing"
)
// FuzzParse fuzz tests the parser to see if we can make it panic.
func FuzzParse(f *testing.F) {
f.Add("{foo=bar,bar=~[a-zA-Z]+,baz!=qux,qux!~[0-9]+")
f.Fuzz(func(t *testing.T, s string) {
matchers, err := Matchers(s)
if matchers != nil && err != nil {
t.Errorf("Unexpected matchers and err: %v %s", matchers, err)
}
})
}

309
matchers/parse/lexer.go Normal file
View File

@ -0,0 +1,309 @@
// Copyright 2023 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package parse
import (
"fmt"
"strings"
"unicode"
"unicode/utf8"
)
const (
eof rune = -1
)
func isReserved(r rune) bool {
return unicode.IsSpace(r) || strings.ContainsRune("{}!=~,\"'`", r)
}
// expectedError is returned when the next rune does not match what is expected.
type expectedError struct {
position
input string
expected string
}
func (e expectedError) Error() string {
if e.offsetEnd >= len(e.input) {
return fmt.Sprintf("%d:%d: unexpected end of input, expected one of '%s'",
e.columnStart,
e.columnEnd,
e.expected,
)
}
return fmt.Sprintf("%d:%d: %s: expected one of '%s'",
e.columnStart,
e.columnEnd,
e.input[e.offsetStart:e.offsetEnd],
e.expected,
)
}
// invalidInputError is returned when the next rune in the input does not match
// the grammar of Prometheus-like matchers.
type invalidInputError struct {
position
input string
}
func (e invalidInputError) Error() string {
return fmt.Sprintf("%d:%d: %s: invalid input",
e.columnStart,
e.columnEnd,
e.input[e.offsetStart:e.offsetEnd],
)
}
// unterminatedError is returned when text in quotes does not have a closing quote.
type unterminatedError struct {
position
input string
quote rune
}
func (e unterminatedError) Error() string {
return fmt.Sprintf("%d:%d: %s: missing end %c",
e.columnStart,
e.columnEnd,
e.input[e.offsetStart:e.offsetEnd],
e.quote,
)
}
// lexer scans a sequence of tokens that match the grammar of Prometheus-like
// matchers. A token is emitted for each call to scan() which returns the
// next token in the input or an error if the input does not conform to the
// grammar. A token can be one of a number of kinds and corresponds to a
// subslice of the input. Once the input has been consumed successive calls to
// scan() return a tokenEOF token.
type lexer struct {
input string
err error
start int // The offset of the current token.
pos int // The position of the cursor in the input.
width int // The width of the last rune.
column int // The column offset of the current token.
cols int // The number of columns (runes) decoded from the input.
}
// Scans the next token in the input or an error if the input does not
// conform to the grammar. Once the input has been consumed successive
// calls scan() return a tokenEOF token.
func (l *lexer) scan() (token, error) {
t := token{}
// Do not attempt to emit more tokens if the input is invalid.
if l.err != nil {
return t, l.err
}
// Iterate over each rune in the input and either emit a token or an error.
for r := l.next(); r != eof; r = l.next() {
switch {
case r == '{':
t = l.emit(tokenOpenBrace)
return t, l.err
case r == '}':
t = l.emit(tokenCloseBrace)
return t, l.err
case r == ',':
t = l.emit(tokenComma)
return t, l.err
case r == '=' || r == '!':
l.rewind()
t, l.err = l.scanOperator()
return t, l.err
case r == '"':
l.rewind()
t, l.err = l.scanQuoted()
return t, l.err
case !isReserved(r):
l.rewind()
t, l.err = l.scanUnquoted()
return t, l.err
case unicode.IsSpace(r):
l.skip()
default:
l.err = invalidInputError{
position: l.position(),
input: l.input,
}
return t, l.err
}
}
return t, l.err
}
func (l *lexer) scanOperator() (token, error) {
// If the first rune is an '!' then it must be followed with either an
// '=' or '~' to not match a string or regex.
if l.accept("!") {
if l.accept("=") {
return l.emit(tokenNotEquals), nil
}
if l.accept("~") {
return l.emit(tokenNotMatches), nil
}
return token{}, expectedError{
position: l.position(),
input: l.input,
expected: "=~",
}
}
// If the first rune is an '=' then it can be followed with an optional
// '~' to match a regex.
if l.accept("=") {
if l.accept("~") {
return l.emit(tokenMatches), nil
}
return l.emit(tokenEquals), nil
}
return token{}, expectedError{
position: l.position(),
input: l.input,
expected: "!=",
}
}
func (l *lexer) scanQuoted() (token, error) {
if err := l.expect("\""); err != nil {
return token{}, err
}
var isEscaped bool
for r := l.next(); r != eof; r = l.next() {
if isEscaped {
isEscaped = false
} else if r == '\\' {
isEscaped = true
} else if r == '"' {
l.rewind()
break
}
}
if err := l.expect("\""); err != nil {
return token{}, unterminatedError{
position: l.position(),
input: l.input,
quote: '"',
}
}
return l.emit(tokenQuoted), nil
}
func (l *lexer) scanUnquoted() (token, error) {
for r := l.next(); r != eof; r = l.next() {
if isReserved(r) {
l.rewind()
break
}
}
return l.emit(tokenUnquoted), nil
}
// peek the next token in the input or an error if the input does not
// conform to the grammar. Once the input has been consumed successive
// calls peek() return a tokenEOF token.
func (l *lexer) peek() (token, error) {
start := l.start
pos := l.pos
width := l.width
column := l.column
cols := l.cols
// Do not reset l.err because we can return it on the next call to scan().
defer func() {
l.start = start
l.pos = pos
l.width = width
l.column = column
l.cols = cols
}()
return l.scan()
}
// position returns the position of the last emitted token.
func (l *lexer) position() position {
return position{
offsetStart: l.start,
offsetEnd: l.pos,
columnStart: l.column,
columnEnd: l.cols,
}
}
// accept consumes the next if its one of the valid runes.
// It returns true if the next rune was accepted, otherwise false.
func (l *lexer) accept(valid string) bool {
if strings.ContainsRune(valid, l.next()) {
return true
}
l.rewind()
return false
}
// expect consumes the next rune if its one of the valid runes.
// it returns nil if the next rune is valid, otherwise an expectedError
// error.
func (l *lexer) expect(valid string) error {
if strings.ContainsRune(valid, l.next()) {
return nil
}
l.rewind()
return expectedError{
position: l.position(),
input: l.input,
expected: valid,
}
}
// emits returns the scanned input as a token.
func (l *lexer) emit(kind tokenKind) token {
t := token{
kind: kind,
value: l.input[l.start:l.pos],
position: l.position(),
}
l.start = l.pos
l.column = l.cols
return t
}
// next returns the next rune in the input or eof.
func (l *lexer) next() rune {
if l.pos >= len(l.input) {
l.width = 0
return eof
}
r, width := utf8.DecodeRuneInString(l.input[l.pos:])
l.width = width
l.pos += width
l.cols++
return r
}
// rewind the last rune in the input. It should not be called more than once
// between consecutive calls of next.
func (l *lexer) rewind() {
l.pos -= l.width
// When the next rune in the input is eof the width is zero. This check
// prevents cols from being decremented when the next rune being accepted
// is instead eof.
if l.width > 0 {
l.cols--
}
}
// skip the scanned input between start and pos.
func (l *lexer) skip() {
l.start = l.pos
l.column = l.cols
}

View File

@ -0,0 +1,626 @@
// Copyright 2023 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package parse
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestLexer_Scan(t *testing.T) {
tests := []struct {
name string
input string
expected []token
err string
}{{
name: "no input",
input: "",
}, {
name: "open brace",
input: "{",
expected: []token{{
kind: tokenOpenBrace,
value: "{",
position: position{
offsetStart: 0,
offsetEnd: 1,
columnStart: 0,
columnEnd: 1,
},
}},
}, {
name: "open brace with leading space",
input: " {",
expected: []token{{
kind: tokenOpenBrace,
value: "{",
position: position{
offsetStart: 1,
offsetEnd: 2,
columnStart: 1,
columnEnd: 2,
},
}},
}, {
name: "close brace",
input: "}",
expected: []token{{
kind: tokenCloseBrace,
value: "}",
position: position{
offsetStart: 0,
offsetEnd: 1,
columnStart: 0,
columnEnd: 1,
},
}},
}, {
name: "close brace with leading space",
input: " }",
expected: []token{{
kind: tokenCloseBrace,
value: "}",
position: position{
offsetStart: 1,
offsetEnd: 2,
columnStart: 1,
columnEnd: 2,
},
}},
}, {
name: "open and closing braces",
input: "{}",
expected: []token{{
kind: tokenOpenBrace,
value: "{",
position: position{
offsetStart: 0,
offsetEnd: 1,
columnStart: 0,
columnEnd: 1,
},
}, {
kind: tokenCloseBrace,
value: "}",
position: position{
offsetStart: 1,
offsetEnd: 2,
columnStart: 1,
columnEnd: 2,
},
}},
}, {
name: "open and closing braces with space",
input: "{ }",
expected: []token{{
kind: tokenOpenBrace,
value: "{",
position: position{
offsetStart: 0,
offsetEnd: 1,
columnStart: 0,
columnEnd: 1,
},
}, {
kind: tokenCloseBrace,
value: "}",
position: position{
offsetStart: 2,
offsetEnd: 3,
columnStart: 2,
columnEnd: 3,
},
}},
}, {
name: "unquoted",
input: "hello",
expected: []token{{
kind: tokenUnquoted,
value: "hello",
position: position{
offsetStart: 0,
offsetEnd: 5,
columnStart: 0,
columnEnd: 5,
},
}},
}, {
name: "unquoted with underscore",
input: "hello_world",
expected: []token{{
kind: tokenUnquoted,
value: "hello_world",
position: position{
offsetStart: 0,
offsetEnd: 11,
columnStart: 0,
columnEnd: 11,
},
}},
}, {
name: "unquoted with colon",
input: "hello:world",
expected: []token{{
kind: tokenUnquoted,
value: "hello:world",
position: position{
offsetStart: 0,
offsetEnd: 11,
columnStart: 0,
columnEnd: 11,
},
}},
}, {
name: "unquoted with numbers",
input: "hello0123456789",
expected: []token{{
kind: tokenUnquoted,
value: "hello0123456789",
position: position{
offsetStart: 0,
offsetEnd: 15,
columnStart: 0,
columnEnd: 15,
},
}},
}, {
name: "unquoted can start with underscore",
input: "_hello",
expected: []token{{
kind: tokenUnquoted,
value: "_hello",
position: position{
offsetStart: 0,
offsetEnd: 6,
columnStart: 0,
columnEnd: 6,
},
}},
}, {
name: "unquoted separated with space",
input: "hello world",
expected: []token{{
kind: tokenUnquoted,
value: "hello",
position: position{
offsetStart: 0,
offsetEnd: 5,
columnStart: 0,
columnEnd: 5,
},
}, {
kind: tokenUnquoted,
value: "world",
position: position{
offsetStart: 6,
offsetEnd: 11,
columnStart: 6,
columnEnd: 11,
},
}},
}, {
name: "unquoted $",
input: "$",
expected: []token{{
kind: tokenUnquoted,
value: "$",
position: position{
offsetStart: 0,
offsetEnd: 1,
columnStart: 0,
columnEnd: 1,
},
}},
}, {
name: "unquoted emoji",
input: "🙂",
expected: []token{{
kind: tokenUnquoted,
value: "🙂",
position: position{
offsetStart: 0,
offsetEnd: 4,
columnStart: 0,
columnEnd: 1,
},
}},
}, {
name: "unquoted unicode",
input: "Σ",
expected: []token{{
kind: tokenUnquoted,
value: "Σ",
position: position{
offsetStart: 0,
offsetEnd: 2,
columnStart: 0,
columnEnd: 1,
},
}},
}, {
name: "unquoted unicode sentence",
input: "hello🙂Σ world",
expected: []token{{
kind: tokenUnquoted,
value: "hello🙂Σ",
position: position{
offsetStart: 0,
offsetEnd: 11,
columnStart: 0,
columnEnd: 7,
},
}, {
kind: tokenUnquoted,
value: "world",
position: position{
offsetStart: 12,
offsetEnd: 17,
columnStart: 8,
columnEnd: 13,
},
}},
}, {
name: "unquoted unicode sentence with unicode space",
input: "hello🙂Σ\u202fworld",
expected: []token{{
kind: tokenUnquoted,
value: "hello🙂Σ",
position: position{
offsetStart: 0,
offsetEnd: 11,
columnStart: 0,
columnEnd: 7,
},
}, {
kind: tokenUnquoted,
value: "world",
position: position{
offsetStart: 14,
offsetEnd: 19,
columnStart: 8,
columnEnd: 13,
},
}},
}, {
name: "quoted",
input: "\"hello\"",
expected: []token{{
kind: tokenQuoted,
value: "\"hello\"",
position: position{
offsetStart: 0,
offsetEnd: 7,
columnStart: 0,
columnEnd: 7,
},
}},
}, {
name: "quoted with unicode",
input: "\"hello 🙂\"",
expected: []token{{
kind: tokenQuoted,
value: "\"hello 🙂\"",
position: position{
offsetStart: 0,
offsetEnd: 12,
columnStart: 0,
columnEnd: 9,
},
}},
}, {
name: "quoted with space",
input: "\"hello world\"",
expected: []token{{
kind: tokenQuoted,
value: "\"hello world\"",
position: position{
offsetStart: 0,
offsetEnd: 13,
columnStart: 0,
columnEnd: 13,
},
}},
}, {
name: "quoted with unicode space",
input: "\"hello\u202fworld\"",
expected: []token{{
kind: tokenQuoted,
value: "\"hello\u202fworld\"",
position: position{
offsetStart: 0,
offsetEnd: 15,
columnStart: 0,
columnEnd: 13,
},
}},
}, {
name: "quoted with newline",
input: "\"hello\nworld\"",
expected: []token{{
kind: tokenQuoted,
value: "\"hello\nworld\"",
position: position{
offsetStart: 0,
offsetEnd: 13,
columnStart: 0,
columnEnd: 13,
},
}},
}, {
name: "quoted with tab",
input: "\"hello\tworld\"",
expected: []token{{
kind: tokenQuoted,
value: "\"hello\tworld\"",
position: position{
offsetStart: 0,
offsetEnd: 13,
columnStart: 0,
columnEnd: 13,
},
}},
}, {
name: "quoted with escaped quotes",
input: "\"hello \\\"world\\\"\"",
expected: []token{{
kind: tokenQuoted,
value: "\"hello \\\"world\\\"\"",
position: position{
offsetStart: 0,
offsetEnd: 17,
columnStart: 0,
columnEnd: 17,
},
}},
}, {
name: "quoted with escaped backslash",
input: "\"hello world\\\\\"",
expected: []token{{
kind: tokenQuoted,
value: "\"hello world\\\\\"",
position: position{
offsetStart: 0,
offsetEnd: 15,
columnStart: 0,
columnEnd: 15,
},
}},
}, {
name: "equals operator",
input: "=",
expected: []token{{
kind: tokenEquals,
value: "=",
position: position{
offsetStart: 0,
offsetEnd: 1,
columnStart: 0,
columnEnd: 1,
},
}},
}, {
name: "not equals operator",
input: "!=",
expected: []token{{
kind: tokenNotEquals,
value: "!=",
position: position{
offsetStart: 0,
offsetEnd: 2,
columnStart: 0,
columnEnd: 2,
},
}},
}, {
name: "matches regex operator",
input: "=~",
expected: []token{{
kind: tokenMatches,
value: "=~",
position: position{
offsetStart: 0,
offsetEnd: 2,
columnStart: 0,
columnEnd: 2,
},
}},
}, {
name: "not matches regex operator",
input: "!~",
expected: []token{{
kind: tokenNotMatches,
value: "!~",
position: position{
offsetStart: 0,
offsetEnd: 2,
columnStart: 0,
columnEnd: 2,
},
}},
}, {
name: "invalid operator",
input: "!",
err: "0:1: unexpected end of input, expected one of '=~'",
}, {
name: "another invalid operator",
input: "~",
err: "0:1: ~: invalid input",
}, {
name: "unexpected ! after unquoted",
input: "hello!",
expected: []token{{
kind: tokenUnquoted,
value: "hello",
position: position{
offsetStart: 0,
offsetEnd: 5,
columnStart: 0,
columnEnd: 5,
},
}},
err: "5:6: unexpected end of input, expected one of '=~'",
}, {
name: "unexpected ! after operator",
input: "=!",
expected: []token{{
kind: tokenEquals,
value: "=",
position: position{
offsetStart: 0,
offsetEnd: 1,
columnStart: 0,
columnEnd: 1,
},
}},
err: "1:2: unexpected end of input, expected one of '=~'",
}, {
name: "unexpected !! after operator",
input: "!=!!",
expected: []token{{
kind: tokenNotEquals,
value: "!=",
position: position{
offsetStart: 0,
offsetEnd: 2,
columnStart: 0,
columnEnd: 2,
},
}},
err: "2:3: !: expected one of '=~'",
}, {
name: "unterminated quoted",
input: "\"hello",
err: "0:6: \"hello: missing end \"",
}, {
name: "unterminated quoted with escaped quote",
input: "\"hello\\\"",
err: "0:8: \"hello\\\": missing end \"",
}}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
l := lexer{input: test.input}
// scan all expected tokens.
for i := 0; i < len(test.expected); i++ {
tok, err := l.scan()
require.NoError(t, err)
require.Equal(t, test.expected[i], tok)
}
if test.err == "" {
// Check there are no more tokens.
tok, err := l.scan()
require.NoError(t, err)
require.Equal(t, token{}, tok)
} else {
// Check if expected error is returned.
tok, err := l.scan()
require.Equal(t, token{}, tok)
require.EqualError(t, err, test.err)
}
})
}
}
// This test asserts that the lexer does not emit more tokens after an
// error has occurred.
func TestLexer_ScanError(t *testing.T) {
l := lexer{input: "\"hello"}
for i := 0; i < 10; i++ {
tok, err := l.scan()
require.Equal(t, token{}, tok)
require.EqualError(t, err, "0:6: \"hello: missing end \"")
}
}
func TestLexer_Peek(t *testing.T) {
l := lexer{input: "hello world"}
expected1 := token{
kind: tokenUnquoted,
value: "hello",
position: position{
offsetStart: 0,
offsetEnd: 5,
columnStart: 0,
columnEnd: 5,
},
}
expected2 := token{
kind: tokenUnquoted,
value: "world",
position: position{
offsetStart: 6,
offsetEnd: 11,
columnStart: 6,
columnEnd: 11,
},
}
// Check that peek() returns the first token.
tok, err := l.peek()
require.NoError(t, err)
require.Equal(t, expected1, tok)
// Check that scan() returns the peeked token.
tok, err = l.scan()
require.NoError(t, err)
require.Equal(t, expected1, tok)
// Check that peek() returns the second token until the next scan().
for i := 0; i < 10; i++ {
tok, err = l.peek()
require.NoError(t, err)
require.Equal(t, expected2, tok)
}
// Check that scan() returns the last token.
tok, err = l.scan()
require.NoError(t, err)
require.Equal(t, expected2, tok)
// Should not be able to peek() further tokens.
for i := 0; i < 10; i++ {
tok, err = l.peek()
require.NoError(t, err)
require.Equal(t, token{}, tok)
}
}
// This test asserts that the lexer does not emit more tokens after an
// error has occurred.
func TestLexer_PeekError(t *testing.T) {
l := lexer{input: "\"hello"}
for i := 0; i < 10; i++ {
tok, err := l.peek()
require.Equal(t, token{}, tok)
require.EqualError(t, err, "0:6: \"hello: missing end \"")
}
}
func TestLexer_Pos(t *testing.T) {
l := lexer{input: "hello🙂"}
// The start position should be the zero-value.
require.Equal(t, position{}, l.position())
_, err := l.scan()
require.NoError(t, err)
// The position should contain the offset and column of the end.
expected := position{
offsetStart: 9,
offsetEnd: 9,
columnStart: 6,
columnEnd: 6,
}
require.Equal(t, expected, l.position())
// The position should not change once the input has been consumed.
tok, err := l.scan()
require.NoError(t, err)
require.True(t, tok.isEOF())
require.Equal(t, expected, l.position())
}

304
matchers/parse/parse.go Normal file
View File

@ -0,0 +1,304 @@
// Copyright 2023 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package parse
import (
"errors"
"fmt"
"os"
"runtime/debug"
"github.com/prometheus/alertmanager/pkg/labels"
)
var (
errEOF = errors.New("end of input")
errExpectedEOF = errors.New("expected end of input")
errNoOpenBrace = errors.New("expected opening brace")
errNoCloseBrace = errors.New("expected close brace")
errNoLabelName = errors.New("expected label name")
errNoLabelValue = errors.New("expected label value")
errNoOperator = errors.New("expected an operator such as '=', '!=', '=~' or '!~'")
errExpectedComma = errors.New("expected a comma")
errExpectedCommaOrCloseBrace = errors.New("expected a comma or close brace")
errExpectedMatcherOrCloseBrace = errors.New("expected a matcher or close brace after comma")
)
// Matchers parses one or more matchers in the input string. It returns an error
// if the input is invalid.
func Matchers(input string) (matchers labels.Matchers, err error) {
defer func() {
if r := recover(); r != nil {
fmt.Fprintf(os.Stderr, "parser panic: %s, %s", r, debug.Stack())
err = errors.New("parser panic: this should never happen, check stderr for the stack trace")
}
}()
p := parser{lexer: lexer{input: input}}
return p.parse()
}
// Matcher parses the matcher in the input string. It returns an error
// if the input is invalid or contains two or more matchers.
func Matcher(input string) (*labels.Matcher, error) {
m, err := Matchers(input)
if err != nil {
return nil, err
}
switch len(m) {
case 1:
return m[0], nil
case 0:
return nil, fmt.Errorf("no matchers")
default:
return nil, fmt.Errorf("expected 1 matcher, found %d", len(m))
}
}
// parseFunc is state in the finite state automata.
type parseFunc func(l *lexer) (parseFunc, error)
// parser reads the sequence of tokens from the lexer and returns either a
// series of matchers or an error. It works as a finite state automata, where
// each state in the automata is a parseFunc. The finite state automata can move
// from one state to another by returning the next parseFunc. It terminates when
// a parseFunc returns nil as the next parseFunc, if the lexer attempts to scan
// input that does not match the expected grammar, or if the tokens returned from
// the lexer cannot be parsed into a complete series of matchers.
type parser struct {
matchers labels.Matchers
// Tracks if the input starts with an open brace and if we should expect to
// parse a close brace at the end of the input.
hasOpenBrace bool
lexer lexer
}
func (p *parser) parse() (labels.Matchers, error) {
var (
err error
fn = p.parseOpenBrace
l = &p.lexer
)
for {
if fn, err = fn(l); err != nil {
return nil, err
} else if fn == nil {
break
}
}
return p.matchers, nil
}
func (p *parser) parseOpenBrace(l *lexer) (parseFunc, error) {
var (
hasCloseBrace bool
err error
)
// Can start with an optional open brace.
p.hasOpenBrace, err = p.accept(l, tokenOpenBrace)
if err != nil {
if errors.Is(err, errEOF) {
return p.parseEOF, nil
}
return nil, err
}
// If the next token is a close brace there are no matchers in the input.
hasCloseBrace, err = p.acceptPeek(l, tokenCloseBrace)
if err != nil {
// If there is no more input after the open brace then parse the close brace
// so the error message contains ErrNoCloseBrace.
if errors.Is(err, errEOF) {
return p.parseCloseBrace, nil
}
return nil, err
}
if hasCloseBrace {
return p.parseCloseBrace, nil
}
return p.parseMatcher, nil
}
func (p *parser) parseCloseBrace(l *lexer) (parseFunc, error) {
if p.hasOpenBrace {
// If there was an open brace there must be a matching close brace.
if _, err := p.expect(l, tokenCloseBrace); err != nil {
return nil, fmt.Errorf("0:%d: %s: %w", l.position().columnEnd, err, errNoCloseBrace)
}
} else {
// If there was no open brace there must not be a close brace either.
if _, err := p.expect(l, tokenCloseBrace); err == nil {
return nil, fmt.Errorf("0:%d: }: %w", l.position().columnEnd, errNoOpenBrace)
}
}
return p.parseEOF, nil
}
func (p *parser) parseMatcher(l *lexer) (parseFunc, error) {
var (
err error
t token
matchName, matchValue string
matchTy labels.MatchType
)
// The first token should be the label name.
if t, err = p.expect(l, tokenQuoted, tokenUnquoted); err != nil {
return nil, fmt.Errorf("%s: %w", err, errNoLabelName)
}
matchName, err = t.unquote()
if err != nil {
return nil, fmt.Errorf("%d:%d: %s: invalid input", t.columnStart, t.columnEnd, t.value)
}
// The next token should be the operator.
if t, err = p.expect(l, tokenEquals, tokenNotEquals, tokenMatches, tokenNotMatches); err != nil {
return nil, fmt.Errorf("%s: %w", err, errNoOperator)
}
switch t.kind {
case tokenEquals:
matchTy = labels.MatchEqual
case tokenNotEquals:
matchTy = labels.MatchNotEqual
case tokenMatches:
matchTy = labels.MatchRegexp
case tokenNotMatches:
matchTy = labels.MatchNotRegexp
default:
panic(fmt.Sprintf("bad operator %s", t))
}
// The next token should be the match value. Like the match name, this too
// can be either double-quoted UTF-8 or unquoted UTF-8 without reserved characters.
if t, err = p.expect(l, tokenUnquoted, tokenQuoted); err != nil {
return nil, fmt.Errorf("%s: %w", err, errNoLabelValue)
}
matchValue, err = t.unquote()
if err != nil {
return nil, fmt.Errorf("%d:%d: %s: invalid input", t.columnStart, t.columnEnd, t.value)
}
m, err := labels.NewMatcher(matchTy, matchName, matchValue)
if err != nil {
return nil, fmt.Errorf("failed to create matcher: %s", err)
}
p.matchers = append(p.matchers, m)
return p.parseEndOfMatcher, nil
}
func (p *parser) parseEndOfMatcher(l *lexer) (parseFunc, error) {
t, err := p.expectPeek(l, tokenComma, tokenCloseBrace)
if err != nil {
if errors.Is(err, errEOF) {
// If this is the end of input we still need to check if the optional
// open brace has a matching close brace
return p.parseCloseBrace, nil
}
return nil, fmt.Errorf("%s: %w", err, errExpectedCommaOrCloseBrace)
}
switch t.kind {
case tokenComma:
return p.parseComma, nil
case tokenCloseBrace:
return p.parseCloseBrace, nil
default:
panic(fmt.Sprintf("bad token %s", t))
}
}
func (p *parser) parseComma(l *lexer) (parseFunc, error) {
if _, err := p.expect(l, tokenComma); err != nil {
return nil, fmt.Errorf("%s: %w", err, errExpectedComma)
}
// The token after the comma can be another matcher, a close brace or end of input.
t, err := p.expectPeek(l, tokenCloseBrace, tokenUnquoted, tokenQuoted)
if err != nil {
if errors.Is(err, errEOF) {
// If this is the end of input we still need to check if the optional
// open brace has a matching close brace
return p.parseCloseBrace, nil
}
return nil, fmt.Errorf("%s: %w", err, errExpectedMatcherOrCloseBrace)
}
if t.kind == tokenCloseBrace {
return p.parseCloseBrace, nil
}
return p.parseMatcher, nil
}
func (p *parser) parseEOF(l *lexer) (parseFunc, error) {
t, err := l.scan()
if err != nil {
return nil, fmt.Errorf("%s: %w", err, errExpectedEOF)
}
if !t.isEOF() {
return nil, fmt.Errorf("%d:%d: %s: %w", t.columnStart, t.columnEnd, t.value, errExpectedEOF)
}
return nil, nil
}
// accept returns true if the next token is one of the specified kinds,
// otherwise false. If the token is accepted it is consumed. tokenEOF is
// not an accepted kind and instead accept returns ErrEOF if there is no
// more input.
func (p *parser) accept(l *lexer, kinds ...tokenKind) (ok bool, err error) {
ok, err = p.acceptPeek(l, kinds...)
if ok {
if _, err = l.scan(); err != nil {
panic("failed to scan peeked token")
}
}
return ok, err
}
// acceptPeek returns true if the next token is one of the specified kinds,
// otherwise false. However, unlike accept, acceptPeek does not consume accepted
// tokens. tokenEOF is not an accepted kind and instead accept returns ErrEOF
// if there is no more input.
func (p *parser) acceptPeek(l *lexer, kinds ...tokenKind) (bool, error) {
t, err := l.peek()
if err != nil {
return false, err
}
if t.isEOF() {
return false, errEOF
}
return t.isOneOf(kinds...), nil
}
// expect returns the next token if it is one of the specified kinds, otherwise
// it returns an error. If the token is expected it is consumed. tokenEOF is not
// an accepted kind and instead expect returns ErrEOF if there is no more input.
func (p *parser) expect(l *lexer, kind ...tokenKind) (token, error) {
t, err := p.expectPeek(l, kind...)
if err != nil {
return t, err
}
if _, err = l.scan(); err != nil {
panic("failed to scan peeked token")
}
return t, nil
}
// expect returns the next token if it is one of the specified kinds, otherwise
// it returns an error. However, unlike expect, expectPeek does not consume tokens.
// tokenEOF is not an accepted kind and instead expect returns ErrEOF if there is no
// more input.
func (p *parser) expectPeek(l *lexer, kind ...tokenKind) (token, error) {
t, err := l.peek()
if err != nil {
return t, err
}
if t.isEOF() {
return t, errEOF
}
if !t.isOneOf(kind...) {
return t, fmt.Errorf("%d:%d: unexpected %s", t.columnStart, t.columnEnd, t.value)
}
return t, nil
}

View File

@ -0,0 +1,349 @@
// Copyright 2023 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package parse
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/prometheus/alertmanager/pkg/labels"
)
func TestMatchers(t *testing.T) {
tests := []struct {
name string
input string
expected labels.Matchers
error string
}{{
name: "no braces",
input: "",
expected: nil,
}, {
name: "open and closing braces",
input: "{}",
expected: nil,
}, {
name: "equals",
input: "{foo=bar}",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchEqual, "foo", "bar")},
}, {
name: "equals with trailing comma",
input: "{foo=bar,}",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchEqual, "foo", "bar")},
}, {
name: "not equals",
input: "{foo!=bar}",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchNotEqual, "foo", "bar")},
}, {
name: "match regex",
input: "{foo=~[a-z]+}",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchRegexp, "foo", "[a-z]+")},
}, {
name: "doesn't match regex",
input: "{foo!~[a-z]+}",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchNotRegexp, "foo", "[a-z]+")},
}, {
name: "equals unicode emoji",
input: "{foo=🙂}",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchEqual, "foo", "🙂")},
}, {
name: "equals unicode sentence",
input: "{foo=🙂bar}",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchEqual, "foo", "🙂bar")},
}, {
name: "equals without braces",
input: "foo=bar",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchEqual, "foo", "bar")},
}, {
name: "equals without braces but with trailing comma",
input: "foo=bar,",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchEqual, "foo", "bar")},
}, {
name: "not equals without braces",
input: "foo!=bar",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchNotEqual, "foo", "bar")},
}, {
name: "match regex without braces",
input: "foo=~[a-z]+",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchRegexp, "foo", "[a-z]+")},
}, {
name: "doesn't match regex without braces",
input: "foo!~[a-z]+",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchNotRegexp, "foo", "[a-z]+")},
}, {
name: "equals in quotes",
input: "{\"foo\"=\"bar\"}",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchEqual, "foo", "bar")},
}, {
name: "equals in quotes and with trailing comma",
input: "{\"foo\"=\"bar\",}",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchEqual, "foo", "bar")},
}, {
name: "not equals in quotes",
input: "{\"foo\"!=\"bar\"}",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchNotEqual, "foo", "bar")},
}, {
name: "match regex in quotes",
input: "{\"foo\"=~\"[a-z]+\"}",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchRegexp, "foo", "[a-z]+")},
}, {
name: "doesn't match regex in quotes",
input: "{\"foo\"!~\"[a-z]+\"}",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchNotRegexp, "foo", "[a-z]+")},
}, {
name: "equals unicode emoji in quotes",
input: "{\"foo\"=\"🙂\"}",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchEqual, "foo", "🙂")},
}, {
name: "equals unicode sentence in quotes",
input: "{\"foo\"=\"🙂bar\"}",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchEqual, "foo", "🙂bar")},
}, {
name: "equals with newline in quotes",
input: "{\"foo\"=\"bar\\n\"}",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchEqual, "foo", "bar\n")},
}, {
name: "equals with tab in quotes",
input: "{\"foo\"=\"bar\\t\"}",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchEqual, "foo", "bar\t")},
}, {
name: "equals with escaped quotes in quotes",
input: "{\"foo\"=\"\\\"bar\\\"\"}",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchEqual, "foo", "\"bar\"")},
}, {
name: "equals with escaped backslash in quotes",
input: "{\"foo\"=\"bar\\\\\"}",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchEqual, "foo", "bar\\")},
}, {
name: "equals without braces in quotes",
input: "\"foo\"=\"bar\"",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchEqual, "foo", "bar")},
}, {
name: "equals without braces in quotes with trailing comma",
input: "\"foo\"=\"bar\",",
expected: labels.Matchers{mustNewMatcher(t, labels.MatchEqual, "foo", "bar")},
}, {
name: "complex",
input: "{foo=bar,bar!=baz}",
expected: labels.Matchers{
mustNewMatcher(t, labels.MatchEqual, "foo", "bar"),
mustNewMatcher(t, labels.MatchNotEqual, "bar", "baz"),
},
}, {
name: "complex in quotes",
input: "{foo=\"bar\",bar!=\"baz\"}",
expected: labels.Matchers{
mustNewMatcher(t, labels.MatchEqual, "foo", "bar"),
mustNewMatcher(t, labels.MatchNotEqual, "bar", "baz"),
},
}, {
name: "complex without braces",
input: "foo=bar,bar!=baz",
expected: labels.Matchers{
mustNewMatcher(t, labels.MatchEqual, "foo", "bar"),
mustNewMatcher(t, labels.MatchNotEqual, "bar", "baz"),
},
}, {
name: "complex without braces in quotes",
input: "foo=\"bar\",bar!=\"baz\"",
expected: labels.Matchers{
mustNewMatcher(t, labels.MatchEqual, "foo", "bar"),
mustNewMatcher(t, labels.MatchNotEqual, "bar", "baz"),
},
}, {
name: "comma",
input: ",",
error: "0:1: unexpected ,: expected label name",
}, {
name: "comma in braces",
input: "{,}",
error: "1:2: unexpected ,: expected label name",
}, {
name: "open brace",
input: "{",
error: "0:1: end of input: expected close brace",
}, {
name: "close brace",
input: "}",
error: "0:1: }: expected opening brace",
}, {
name: "no open brace",
input: "foo=bar}",
error: "0:8: }: expected opening brace",
}, {
name: "no close brace",
input: "{foo=bar",
error: "0:8: end of input: expected close brace",
}, {
name: "invalid input after operator and before quotes",
input: "{foo=:\"bar\"}",
error: "6:11: unexpected \"bar\": expected a comma or close brace",
}, {
name: "invalid escape sequence",
input: "{foo=\"bar\\w\"}",
error: "5:12: \"bar\\w\": invalid input",
}}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
matchers, err := Matchers(test.input)
if test.error != "" {
require.EqualError(t, err, test.error)
} else {
require.Nil(t, err)
require.EqualValues(t, test.expected, matchers)
}
})
}
}
func TestMatcher(t *testing.T) {
tests := []struct {
name string
input string
expected *labels.Matcher
error string
}{{
name: "equals",
input: "{foo=bar}",
expected: mustNewMatcher(t, labels.MatchEqual, "foo", "bar"),
}, {
name: "equals with trailing comma",
input: "{foo=bar,}",
expected: mustNewMatcher(t, labels.MatchEqual, "foo", "bar"),
}, {
name: "not equals",
input: "{foo!=bar}",
expected: mustNewMatcher(t, labels.MatchNotEqual, "foo", "bar"),
}, {
name: "match regex",
input: "{foo=~[a-z]+}",
expected: mustNewMatcher(t, labels.MatchRegexp, "foo", "[a-z]+"),
}, {
name: "doesn't match regex",
input: "{foo!~[a-z]+}",
expected: mustNewMatcher(t, labels.MatchNotRegexp, "foo", "[a-z]+"),
}, {
name: "equals unicode emoji",
input: "{foo=🙂}",
expected: mustNewMatcher(t, labels.MatchEqual, "foo", "🙂"),
}, {
name: "equals unicode sentence",
input: "{foo=🙂bar}",
expected: mustNewMatcher(t, labels.MatchEqual, "foo", "🙂bar"),
}, {
name: "equals without braces",
input: "foo=bar",
expected: mustNewMatcher(t, labels.MatchEqual, "foo", "bar"),
}, {
name: "equals without braces but with trailing comma",
input: "foo=bar,",
expected: mustNewMatcher(t, labels.MatchEqual, "foo", "bar"),
}, {
name: "not equals without braces",
input: "foo!=bar",
expected: mustNewMatcher(t, labels.MatchNotEqual, "foo", "bar"),
}, {
name: "match regex without braces",
input: "foo=~[a-z]+",
expected: mustNewMatcher(t, labels.MatchRegexp, "foo", "[a-z]+"),
}, {
name: "doesn't match regex without braces",
input: "foo!~[a-z]+",
expected: mustNewMatcher(t, labels.MatchNotRegexp, "foo", "[a-z]+"),
}, {
name: "equals in quotes",
input: "{\"foo\"=\"bar\"}",
expected: mustNewMatcher(t, labels.MatchEqual, "foo", "bar"),
}, {
name: "equals in quotes and with trailing comma",
input: "{\"foo\"=\"bar\",}",
expected: mustNewMatcher(t, labels.MatchEqual, "foo", "bar"),
}, {
name: "not equals in quotes",
input: "{\"foo\"!=\"bar\"}",
expected: mustNewMatcher(t, labels.MatchNotEqual, "foo", "bar"),
}, {
name: "match regex in quotes",
input: "{\"foo\"=~\"[a-z]+\"}",
expected: mustNewMatcher(t, labels.MatchRegexp, "foo", "[a-z]+"),
}, {
name: "doesn't match regex in quotes",
input: "{\"foo\"!~\"[a-z]+\"}",
expected: mustNewMatcher(t, labels.MatchNotRegexp, "foo", "[a-z]+"),
}, {
name: "equals unicode emoji in quotes",
input: "{\"foo\"=\"🙂\"}",
expected: mustNewMatcher(t, labels.MatchEqual, "foo", "🙂"),
}, {
name: "equals unicode sentence in quotes",
input: "{\"foo\"=\"🙂bar\"}",
expected: mustNewMatcher(t, labels.MatchEqual, "foo", "🙂bar"),
}, {
name: "equals with newline in quotes",
input: "{\"foo\"=\"bar\\n\"}",
expected: mustNewMatcher(t, labels.MatchEqual, "foo", "bar\n"),
}, {
name: "equals with tab in quotes",
input: "{\"foo\"=\"bar\\t\"}",
expected: mustNewMatcher(t, labels.MatchEqual, "foo", "bar\t"),
}, {
name: "equals with escaped quotes in quotes",
input: "{\"foo\"=\"\\\"bar\\\"\"}",
expected: mustNewMatcher(t, labels.MatchEqual, "foo", "\"bar\""),
}, {
name: "equals with escaped backslash in quotes",
input: "{\"foo\"=\"bar\\\\\"}",
expected: mustNewMatcher(t, labels.MatchEqual, "foo", "bar\\"),
}, {
name: "equals without braces in quotes",
input: "\"foo\"=\"bar\"",
expected: mustNewMatcher(t, labels.MatchEqual, "foo", "bar"),
}, {
name: "equals without braces in quotes with trailing comma",
input: "\"foo\"=\"bar\",",
expected: mustNewMatcher(t, labels.MatchEqual, "foo", "bar"),
}, {
name: "no input",
error: "no matchers",
}, {
name: "open and closing braces",
input: "{}",
error: "no matchers",
}, {
name: "two or more returns error",
input: "foo=bar,bar=baz",
error: "expected 1 matcher, found 2",
}}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
matcher, err := Matcher(test.input)
if test.error != "" {
require.EqualError(t, err, test.error)
} else {
require.Nil(t, err)
require.EqualValues(t, test.expected, matcher)
}
})
}
}
func mustNewMatcher(t *testing.T, op labels.MatchType, name, value string) *labels.Matcher {
m, err := labels.NewMatcher(op, name, value)
require.NoError(t, err)
return m
}

99
matchers/parse/token.go Normal file
View File

@ -0,0 +1,99 @@
// Copyright 2023 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package parse
import (
"fmt"
"strconv"
)
type tokenKind int
const (
tokenEOF tokenKind = iota
tokenOpenBrace
tokenCloseBrace
tokenComma
tokenEquals
tokenNotEquals
tokenMatches
tokenNotMatches
tokenQuoted
tokenUnquoted
)
func (k tokenKind) String() string {
switch k {
case tokenOpenBrace:
return "OpenBrace"
case tokenCloseBrace:
return "CloseBrace"
case tokenComma:
return "Comma"
case tokenEquals:
return "Equals"
case tokenNotEquals:
return "NotEquals"
case tokenMatches:
return "Matches"
case tokenNotMatches:
return "NotMatches"
case tokenQuoted:
return "Quoted"
case tokenUnquoted:
return "Unquoted"
default:
return "EOF"
}
}
type token struct {
kind tokenKind
value string
position
}
// isEOF returns true if the token is an end of file token.
func (t token) isEOF() bool {
return t.kind == tokenEOF
}
// isOneOf returns true if the token is one of the specified kinds.
func (t token) isOneOf(kinds ...tokenKind) bool {
for _, k := range kinds {
if k == t.kind {
return true
}
}
return false
}
// unquote the value in token. If unquoted returns it unmodified.
func (t token) unquote() (string, error) {
if t.kind == tokenQuoted {
return strconv.Unquote(t.value)
}
return t.value, nil
}
func (t token) String() string {
return fmt.Sprintf("(%s) '%s'", t.kind, t.value)
}
type position struct {
offsetStart int // The start position in the input.
offsetEnd int // The end position in the input.
columnStart int // The column number.
columnEnd int // The end of the column.
}