bump dependencies
Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
This commit is contained in:
+2
-2
@@ -163,9 +163,9 @@ var Assets = func() http.FileSystem {
|
||||
"/templates/default.tmpl": &vfsgen۰CompressedFileInfo{
|
||||
name: "default.tmpl",
|
||||
modTime: time.Date(1970, 1, 1, 0, 0, 1, 0, time.UTC),
|
||||
uncompressedSize: 5875,
|
||||
uncompressedSize: 5951,
|
||||
|
||||
compressedContent: []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\xff\xec\x58\x41\x6f\xa3\x3a\x10\xbe\xf3\x2b\xac\xf4\xd2\x1c\x42\xdf\xb9\x52\xf5\x54\x3d\xbd\xdd\x4b\xb5\x5a\xa5\xca\x5e\x56\x2b\xe4\xc0\x84\xba\x31\x36\xb5\x87\xb4\x11\xe1\xbf\xaf\x0c\x94\x40\x0c\xa9\x49\xb3\xa7\xcd\xad\xb8\x33\xdf\x8c\xbf\xf9\x98\x19\x92\xe7\x24\x82\x15\x13\x40\x26\x41\x40\x39\x28\x4c\xa8\xa0\x31\xa8\x09\x29\x8a\xfb\xd6\x73\x9e\x13\x10\x11\x29\x0a\x6f\xd0\x65\x31\x7f\x30\x5e\x79\x4e\xfc\xff\xdf\x10\x94\xa0\x7c\x31\x7f\x20\x45\x71\x73\x75\x53\xda\xe9\x7f\x15\x84\xc0\x36\xa0\xee\x8c\xd1\xbc\x7e\x20\x3b\x92\x29\xfe\x92\x81\xda\x56\xee\x75\xa0\x6e\x24\x9d\x2d\x9f\x21\x44\x13\xe1\xa7\xf1\x7e\x44\x8a\x99\x26\x3b\x82\x72\x91\xa6\xa0\x2a\x57\xb6\x22\xf0\xd2\xfc\x73\xb2\x62\x8a\x89\xd8\xf8\xdc\x1a\x9f\xf2\x42\xda\xff\x52\x9e\x92\x1d\xe1\x20\xda\x11\x7f\x11\x63\xf4\x55\xc9\x2c\x7d\xa0\x4b\xe0\xda\x7f\x94\x0a\x21\xfa\x4e\x99\xd2\xfe\x0f\xca\x33\x30\x01\x9f\x25\x13\x64\x42\x0c\x2a\xa9\x42\xc6\x48\xae\x0d\x96\xff\x9f\x4c\x12\x29\x2a\xe7\x69\x7d\xd6\xc2\x9b\x92\xa2\xb8\xce\x73\xf2\xca\xf0\xa9\x6b\xec\xcf\x21\x91\x1b\xe8\x46\xff\x46\x13\xd0\x35\xa3\x7d\xd1\x9b\xc4\xa7\xcd\x5f\x03\x65\x8a\x40\x87\x8a\xa5\xc8\xa4\x98\x1c\xe1\x18\xe1\x0d\xab\x92\x06\x9c\x69\xac\x4d\x15\x15\x31\x10\x9f\x14\x45\x95\xd7\xad\xb7\x3f\xb4\x79\x32\xac\xcc\x4a\x22\x4d\xfa\xe6\xe9\x8e\x34\x17\xa8\x13\xab\x82\xdf\x0b\x21\x91\x9a\x9c\x3a\x90\xad\xe3\xd3\x70\x1f\x65\xa6\x42\xb8\xad\x8a\x09\x02\x14\x45\xa9\x2a\x25\x7a\x3d\x44\x1d\xa5\x20\x48\xa8\x5a\x47\xf2\x55\x58\x5c\x78\xae\x64\x38\x66\xed\x8d\xa7\xc3\x15\xd9\x89\x10\xaf\x9f\x11\xcd\x69\xb8\xf6\x23\x58\xd1\x8c\xa3\x8f\x0c\x39\xd4\x54\x20\x24\x29\xa7\xd8\x7d\x39\xfd\x21\x0d\x76\x71\x32\x6d\xda\x43\xd2\x07\xd5\x6d\x42\x8e\x78\x2b\xca\xf9\x92\x86\x6b\x0b\xaf\x37\x7d\x03\x4a\x76\xe4\x23\x43\xce\xc4\xda\x39\x83\xb0\xce\x80\x45\x13\x37\x87\x54\x81\xd1\x9a\xa3\x75\x2b\xa1\xa3\x8c\x95\x3d\xd8\x31\x65\x16\x4a\x01\x89\x7c\x66\x13\x77\xfb\x4c\x71\xd7\x8c\xdd\x2f\xb7\x92\x12\xab\x89\xd3\xd2\x60\xdb\x3c\x35\x57\x8b\x32\xdc\x36\x2e\x76\x43\x1b\x27\x47\x1b\x31\xe4\x0c\x04\x9e\x2e\xc8\x21\xc4\xfd\x54\x3c\xad\x66\x36\x2e\x13\x1a\xa9\x08\x41\xf7\xe0\x5a\x1d\xdc\x1f\x66\x55\xa6\x3a\x06\xc1\xa0\x01\x4e\x40\x6b\x1a\x9f\xf6\x7e\x5b\x60\x76\x85\xea\x81\x37\xd0\xd0\x7a\x27\x9c\x77\x30\x5f\x3b\x03\x7c\x4a\xfe\x21\x33\xd3\x38\xcb\x43\x52\x1d\x96\xad\xf3\x38\x23\xdd\x2d\xa0\x0c\x32\x6b\xdd\xa8\x27\xde\x1c\xb4\xe4\x1b\x88\x0e\x22\xbe\x1f\xbb\xc7\x7c\xf7\xb0\xa2\xce\x5c\x28\xd5\x65\x1f\x1f\xaf\xa6\x4e\xd5\x5f\x21\x7c\xa2\x38\xb6\xe6\xde\xa5\x7e\x47\xea\xd7\x5e\x94\x17\x8a\x5b\x78\xbd\xf5\x19\xa8\xfa\x41\x7d\x50\x06\x66\x58\x0e\x76\x52\xdb\x3c\xa5\x0a\xb7\x23\xec\x91\xc6\xae\xd6\x34\x06\x81\xc1\xe1\x88\xeb\xea\x6b\xc3\x42\x94\x4a\xa6\x7a\x2f\x5b\xa4\x08\x41\x57\x68\x17\x2d\x8d\xeb\x05\x36\xab\x20\x90\xe1\x36\x88\x98\x4e\x39\xdd\x06\x03\xdb\xd4\xc7\x8d\xdb\x46\x4e\xa4\x60\x28\x0d\x21\x01\x4a\xc9\x47\x8e\xc4\xce\xec\xca\xf4\x93\xdc\x80\x3a\xc3\xfe\x68\x41\xfd\x79\x3d\x9d\x47\x4e\xee\x6a\x3a\x9f\x98\xec\x95\xfe\x18\x93\xfb\x9d\x6e\xcc\x4c\x69\x6f\x73\xa2\xf5\xb2\xef\x3f\xd3\xc7\x7f\x23\xb4\x70\x2e\xe5\x1d\x53\xde\x36\x8b\x08\x1c\x62\x45\x93\x3e\x2a\xff\x5a\x52\x22\xa6\x43\xa9\xa2\x33\x34\xa2\x43\xa4\x0b\xbb\x66\x4d\x58\xc2\xdb\xe5\xd5\xfd\x34\x8f\x89\x46\xa0\x89\x3e\x83\x4a\x2d\xa4\xfa\x6b\xdc\x85\xda\x2b\x32\x8a\xdc\xd6\x4f\x64\x9f\x66\xb9\x09\xed\xca\x73\x4f\xf0\x8f\x08\xff\x1d\x00\x00\xff\xff\xc6\x76\xea\x54\xf3\x16\x00\x00"),
|
||||
compressedContent: []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\xff\xec\x58\x4f\x6f\xbb\x46\x10\xbd\xf3\x29\x56\xce\x25\x3e\x98\xf4\x1c\x29\xaa\xa2\xaa\xed\x25\xaa\x2a\x47\xee\xa5\xaa\xd0\x1a\xc6\x64\xe3\xfd\x43\x76\x07\x27\x16\xe6\xbb\x57\x0b\xc4\x06\x2f\x38\x8b\xe3\xdf\xe9\xe7\x5b\xd8\xcc\xbc\x99\x7d\x6f\x98\x19\x5c\x14\x24\x81\x15\x93\x40\x26\x51\x44\x39\x68\x14\x54\xd2\x14\xf4\x84\x94\xe5\x63\xeb\xb9\x28\x08\xc8\x84\x94\x65\x30\xe8\xb2\x98\x3f\x59\xaf\xa2\x20\xe1\xef\x1f\x08\x5a\x52\xbe\x98\x3f\x91\xb2\xbc\xbb\xb9\xab\xec\xcc\xaf\x1a\x62\x60\x1b\xd0\x0f\xd6\x68\xde\x3c\x90\x1d\xc9\x35\x7f\xcb\x41\x6f\x6b\xf7\x26\x50\x37\x92\xc9\x97\xaf\x10\xa3\x8d\xf0\xaf\xf5\x7e\x46\x8a\xb9\x21\x3b\x82\x6a\x91\x65\xa0\x6b\x57\xb6\x22\xf0\xb6\xff\xe7\x64\xc5\x34\x93\xa9\xf5\xb9\xb7\x3e\xd5\x85\x4c\xf8\x47\x75\x4a\x76\x84\x83\x6c\x47\xfc\x8f\x58\xa3\x3f\xb5\xca\xb3\x27\xba\x04\x6e\xc2\x67\xa5\x11\x92\xbf\x29\xd3\x26\xfc\x87\xf2\x1c\x6c\xc0\x57\xc5\x24\x99\x10\x8b\x4a\xea\x90\x29\x92\x5b\x8b\x15\xfe\xa6\x84\x50\xb2\x76\x9e\x36\x67\x2d\xbc\x29\x29\xcb\xdb\xa2\x20\xef\x0c\x5f\xba\xc6\xe1\x1c\x84\xda\x40\x37\xfa\x5f\x54\x80\x69\x18\xed\x8b\xbe\x4f\x7c\xba\xff\x6b\x40\xa6\x04\x4c\xac\x59\x86\x4c\xc9\xc9\x09\x8e\x11\x3e\xb0\x96\x34\xe2\xcc\x60\x63\xaa\xa9\x4c\x81\x84\xa4\x2c\xeb\xbc\xee\x83\xc3\xa1\xcb\x93\x65\x65\x56\x11\x69\xd3\xb7\x4f\x0f\x64\x7f\x81\x26\xb1\x3a\xf8\xa3\x94\x0a\xa9\xcd\xa9\x03\xd9\x3a\x3e\x0f\xf7\x59\xe5\x3a\x86\xfb\x5a\x4c\x90\xa0\x29\x2a\x5d\x57\x62\xd0\x43\xd4\x49\x0a\x22\x41\xf5\x3a\x51\xef\xd2\xe1\x22\xf0\x25\xc3\x33\xeb\x60\x3c\x1d\xbe\xc8\x5e\x84\x04\xfd\x8c\x18\x4e\xe3\x75\x98\xc0\x8a\xe6\x1c\x43\x64\xc8\xa1\xa1\x02\x41\x64\x9c\x62\xf7\xe5\x0c\x87\x6a\xb0\x8b\x93\x1b\xdb\x1e\x44\x1f\x54\xb7\x09\x79\xe2\xad\x28\xe7\x4b\x1a\xaf\x1d\xbc\xde\xf4\x2d\x28\xd9\x91\xaf\x0c\x39\x93\x6b\xef\x0c\xe2\x26\x03\x96\x4c\xfc\x1c\x32\x0d\xb6\xd6\x3c\xad\x5b\x09\x9d\x64\xac\xea\xc1\x9e\x29\xb3\x58\x49\x10\xea\x95\x4d\xfc\xed\x73\xcd\x7d\x33\xf6\xbf\xdc\x4a\x29\xac\x27\x4e\xab\x06\xdb\xe6\x99\xbd\x5a\x92\xe3\x76\xef\xe2\x36\xb4\x71\xe5\xe8\x22\xc6\x9c\x81\xc4\xf3\x0b\x72\x08\xf1\x30\x15\xcf\xd3\xcc\xc5\x65\xd2\x20\x95\x31\x98\x1e\x5c\xa7\x83\x87\xc3\xac\xaa\xcc\xa4\x20\x19\xec\x81\x05\x18\x43\xd3\xf3\xde\x6f\x07\xcc\x55\xa8\x19\x78\x03\x0d\xad\x77\xc2\x05\x47\xf3\xb5\x33\xc0\xa7\xe4\x17\x32\xb3\x8d\xb3\x3a\x24\xf5\x61\xd5\x3a\x4f\x33\xd2\xdd\x02\xaa\x20\xb3\xd6\x8d\x7a\xe2\xcd\xc1\x28\xbe\x81\xe4\x28\xe2\xe7\xb1\x7f\xcc\x4f\x0f\x27\xea\xcc\x87\x52\x53\xf5\xf1\xf1\xd5\xd4\x51\xfd\x1d\xe2\x17\x8a\x63\x35\x0f\xae\xfa\x9d\xd0\xaf\xbd\x28\x2f\x34\x77\xf0\x7a\xf5\x19\x50\xfd\x48\x1f\x54\x91\x1d\x96\x83\x9d\xd4\x35\xcf\xa8\xc6\xed\x08\x7b\xa4\xa9\xaf\x35\x4d\x41\x62\x74\x3c\xe2\xba\xf5\xb5\x61\x31\x2a\xad\x32\x73\x28\x5b\xa4\x08\x51\xb7\xd0\xae\xb5\x34\xae\x17\xb8\xac\x82\x44\x86\xdb\x28\x61\x26\xe3\x74\x1b\x0d\x6c\x53\x5f\x37\x6e\x17\x59\x28\xc9\x50\x59\x42\x22\x54\x8a\x8f\x1c\x89\x9d\xd9\x95\x9b\x17\xb5\x01\x7d\x81\xfd\xd1\x81\xfa\xf1\xf5\x74\x99\x72\xf2\xaf\xa6\xcb\x15\x93\xbb\xd2\x9f\x62\xf2\xb0\xd3\x8d\x99\x29\xed\x6d\x4e\xb6\x5e\xf6\xc3\x67\xfa\xf8\x6f\x84\x16\xce\x55\xde\x31\xf2\xb6\x59\x44\xe0\x90\x6a\x2a\xfa\xa8\xfc\x69\x49\x49\x98\x89\x95\x4e\x2e\xd0\x88\x8e\x91\xae\xec\xda\x35\x61\x09\x1f\xd7\x57\xf7\xdb\x3c\x0a\x83\x40\x45\xbb\x99\x0a\x41\xf5\xf6\xac\x3a\x3d\xc6\x3a\xbf\xe2\x1d\xa4\xe6\xcb\xde\x47\xa6\x1b\x32\x4a\xa8\xd6\xcf\x6d\xdf\x56\x6c\x1f\xda\x57\xb3\x9e\xe0\x5f\x89\xf7\x7f\x00\x00\x00\xff\xff\xf6\x0e\x88\xb1\x3f\x17\x00\x00"),
|
||||
},
|
||||
"/templates/email.tmpl": &vfsgen۰CompressedFileInfo{
|
||||
name: "email.tmpl",
|
||||
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
// Copyright 2023 Prometheus Team
|
||||
// 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 featurecontrol
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/go-kit/log"
|
||||
"github.com/go-kit/log/level"
|
||||
)
|
||||
|
||||
const (
|
||||
FeatureReceiverNameInMetrics = "receiver-name-in-metrics"
|
||||
FeatureClassicMode = "classic-mode"
|
||||
FeatureUTF8StrictMode = "utf8-strict-mode"
|
||||
)
|
||||
|
||||
var AllowedFlags = []string{
|
||||
FeatureReceiverNameInMetrics,
|
||||
FeatureClassicMode,
|
||||
FeatureUTF8StrictMode,
|
||||
}
|
||||
|
||||
type Flagger interface {
|
||||
EnableReceiverNamesInMetrics() bool
|
||||
ClassicMode() bool
|
||||
UTF8StrictMode() bool
|
||||
}
|
||||
|
||||
type Flags struct {
|
||||
logger log.Logger
|
||||
enableReceiverNamesInMetrics bool
|
||||
classicMode bool
|
||||
utf8StrictMode bool
|
||||
}
|
||||
|
||||
func (f *Flags) EnableReceiverNamesInMetrics() bool {
|
||||
return f.enableReceiverNamesInMetrics
|
||||
}
|
||||
|
||||
func (f *Flags) ClassicMode() bool {
|
||||
return f.classicMode
|
||||
}
|
||||
|
||||
func (f *Flags) UTF8StrictMode() bool {
|
||||
return f.utf8StrictMode
|
||||
}
|
||||
|
||||
type flagOption func(flags *Flags)
|
||||
|
||||
func enableReceiverNameInMetrics() flagOption {
|
||||
return func(configs *Flags) {
|
||||
configs.enableReceiverNamesInMetrics = true
|
||||
}
|
||||
}
|
||||
|
||||
func enableClassicMode() flagOption {
|
||||
return func(configs *Flags) {
|
||||
configs.classicMode = true
|
||||
}
|
||||
}
|
||||
|
||||
func enableUTF8StrictMode() flagOption {
|
||||
return func(configs *Flags) {
|
||||
configs.utf8StrictMode = true
|
||||
}
|
||||
}
|
||||
|
||||
func NewFlags(logger log.Logger, features string) (Flagger, error) {
|
||||
fc := &Flags{logger: logger}
|
||||
opts := []flagOption{}
|
||||
|
||||
if len(features) == 0 {
|
||||
return NoopFlags{}, nil
|
||||
}
|
||||
|
||||
for _, feature := range strings.Split(features, ",") {
|
||||
switch feature {
|
||||
case FeatureReceiverNameInMetrics:
|
||||
opts = append(opts, enableReceiverNameInMetrics())
|
||||
level.Warn(logger).Log("msg", "Experimental receiver name in metrics enabled")
|
||||
case FeatureClassicMode:
|
||||
opts = append(opts, enableClassicMode())
|
||||
level.Warn(logger).Log("msg", "Classic mode enabled")
|
||||
case FeatureUTF8StrictMode:
|
||||
opts = append(opts, enableUTF8StrictMode())
|
||||
level.Warn(logger).Log("msg", "UTF-8 strict mode enabled")
|
||||
default:
|
||||
return nil, fmt.Errorf("Unknown option '%s' for --enable-feature", feature)
|
||||
}
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(fc)
|
||||
}
|
||||
|
||||
if fc.classicMode && fc.utf8StrictMode {
|
||||
return nil, errors.New("cannot have both classic and UTF-8 modes enabled")
|
||||
}
|
||||
|
||||
return fc, nil
|
||||
}
|
||||
|
||||
type NoopFlags struct{}
|
||||
|
||||
func (n NoopFlags) EnableReceiverNamesInMetrics() bool { return false }
|
||||
|
||||
func (n NoopFlags) ClassicMode() bool { return false }
|
||||
|
||||
func (n NoopFlags) UTF8StrictMode() bool { return false }
|
||||
+205
@@ -0,0 +1,205 @@
|
||||
// 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 compat
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/go-kit/log"
|
||||
"github.com/go-kit/log/level"
|
||||
"github.com/prometheus/common/model"
|
||||
|
||||
"github.com/prometheus/alertmanager/featurecontrol"
|
||||
"github.com/prometheus/alertmanager/matchers/parse"
|
||||
"github.com/prometheus/alertmanager/pkg/labels"
|
||||
)
|
||||
|
||||
var (
|
||||
isValidLabelName = isValidClassicLabelName(log.NewNopLogger())
|
||||
parseMatcher = ClassicMatcherParser(log.NewNopLogger())
|
||||
parseMatchers = ClassicMatchersParser(log.NewNopLogger())
|
||||
)
|
||||
|
||||
// IsValidLabelName returns true if the string is a valid label name.
|
||||
func IsValidLabelName(name model.LabelName) bool {
|
||||
return isValidLabelName(name)
|
||||
}
|
||||
|
||||
type ParseMatcher func(input, origin string) (*labels.Matcher, error)
|
||||
|
||||
type ParseMatchers func(input, origin string) (labels.Matchers, error)
|
||||
|
||||
// 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, origin string) (*labels.Matcher, error) {
|
||||
return parseMatcher(input, origin)
|
||||
}
|
||||
|
||||
// Matchers parses one or more matchers in the input string. It returns
|
||||
// an error if the input is invalid.
|
||||
func Matchers(input, origin string) (labels.Matchers, error) {
|
||||
return parseMatchers(input, origin)
|
||||
}
|
||||
|
||||
// InitFromFlags initializes the compat package from the flagger.
|
||||
func InitFromFlags(l log.Logger, f featurecontrol.Flagger) {
|
||||
if f.ClassicMode() {
|
||||
isValidLabelName = isValidClassicLabelName(l)
|
||||
parseMatcher = ClassicMatcherParser(l)
|
||||
parseMatchers = ClassicMatchersParser(l)
|
||||
} else if f.UTF8StrictMode() {
|
||||
isValidLabelName = isValidUTF8LabelName(l)
|
||||
parseMatcher = UTF8MatcherParser(l)
|
||||
parseMatchers = UTF8MatchersParser(l)
|
||||
} else {
|
||||
isValidLabelName = isValidUTF8LabelName(l)
|
||||
parseMatcher = FallbackMatcherParser(l)
|
||||
parseMatchers = FallbackMatchersParser(l)
|
||||
}
|
||||
}
|
||||
|
||||
// ClassicMatcherParser uses the pkg/labels parser to parse the matcher in
|
||||
// the input string.
|
||||
func ClassicMatcherParser(l log.Logger) ParseMatcher {
|
||||
return func(input, origin string) (matcher *labels.Matcher, err error) {
|
||||
level.Debug(l).Log("msg", "Parsing with classic matchers parser", "input", input, "origin", origin)
|
||||
return labels.ParseMatcher(input)
|
||||
}
|
||||
}
|
||||
|
||||
// ClassicMatchersParser uses the pkg/labels parser to parse zero or more
|
||||
// matchers in the input string. It returns an error if the input is invalid.
|
||||
func ClassicMatchersParser(l log.Logger) ParseMatchers {
|
||||
return func(input, origin string) (matchers labels.Matchers, err error) {
|
||||
level.Debug(l).Log("msg", "Parsing with classic matchers parser", "input", input, "origin", origin)
|
||||
return labels.ParseMatchers(input)
|
||||
}
|
||||
}
|
||||
|
||||
// UTF8MatcherParser uses the new matchers/parse parser to parse the matcher
|
||||
// in the input string. If this fails it does not revert to the pkg/labels parser.
|
||||
func UTF8MatcherParser(l log.Logger) ParseMatcher {
|
||||
return func(input, origin string) (matcher *labels.Matcher, err error) {
|
||||
level.Debug(l).Log("msg", "Parsing with UTF-8 matchers parser", "input", input, "origin", origin)
|
||||
if strings.HasPrefix(input, "{") || strings.HasSuffix(input, "}") {
|
||||
return nil, fmt.Errorf("unexpected open or close brace: %s", input)
|
||||
}
|
||||
return parse.Matcher(input)
|
||||
}
|
||||
}
|
||||
|
||||
// UTF8MatchersParser uses the new matchers/parse parser to parse zero or more
|
||||
// matchers in the input string. If this fails it does not revert to the
|
||||
// pkg/labels parser.
|
||||
func UTF8MatchersParser(l log.Logger) ParseMatchers {
|
||||
return func(input, origin string) (matchers labels.Matchers, err error) {
|
||||
level.Debug(l).Log("msg", "Parsing with UTF-8 matchers parser", "input", input, "origin", origin)
|
||||
return parse.Matchers(input)
|
||||
}
|
||||
}
|
||||
|
||||
// FallbackMatcherParser uses the new matchers/parse parser to parse zero or more
|
||||
// matchers in the string. If this fails it reverts to the pkg/labels parser and
|
||||
// emits a warning log line.
|
||||
func FallbackMatcherParser(l log.Logger) ParseMatcher {
|
||||
return func(input, origin string) (matcher *labels.Matcher, err error) {
|
||||
level.Debug(l).Log("msg", "Parsing with UTF-8 matchers parser, with fallback to classic matchers parser", "input", input, "origin", origin)
|
||||
if strings.HasPrefix(input, "{") || strings.HasSuffix(input, "}") {
|
||||
return nil, fmt.Errorf("unexpected open or close brace: %s", input)
|
||||
}
|
||||
// Parse the input in both parsers to look for disagreement and incompatible
|
||||
// inputs.
|
||||
nMatcher, nErr := parse.Matcher(input)
|
||||
cMatcher, cErr := labels.ParseMatcher(input)
|
||||
if nErr != nil {
|
||||
// If the input is invalid in both parsers, return the error.
|
||||
if cErr != nil {
|
||||
return nil, cErr
|
||||
}
|
||||
// The input is valid in the pkg/labels parser, but not the matchers/parse
|
||||
// parser. This means the input is not forwards compatible.
|
||||
suggestion := cMatcher.String()
|
||||
level.Warn(l).Log("msg", "Alertmanager is moving to a new parser for labels and matchers, and this input is incompatible. Alertmanager has instead parsed the input using the classic matchers parser as a fallback. To make this input compatible with the UTF-8 matchers parser please make sure all regular expressions and values are double-quoted. If you are still seeing this message please open an issue.", "input", input, "origin", origin, "err", nErr, "suggestion", suggestion)
|
||||
return cMatcher, nil
|
||||
}
|
||||
// If the input is valid in both parsers, but produces different results,
|
||||
// then there is disagreement.
|
||||
if nErr == nil && cErr == nil && !reflect.DeepEqual(nMatcher, cMatcher) {
|
||||
level.Warn(l).Log("msg", "Matchers input has disagreement", "input", input, "origin", origin)
|
||||
return cMatcher, nil
|
||||
}
|
||||
return nMatcher, nil
|
||||
}
|
||||
}
|
||||
|
||||
// FallbackMatchersParser uses the new matchers/parse parser to parse the
|
||||
// matcher in the input string. If this fails it falls back to the pkg/labels
|
||||
// parser and emits a warning log line.
|
||||
func FallbackMatchersParser(l log.Logger) ParseMatchers {
|
||||
return func(input, origin string) (matchers labels.Matchers, err error) {
|
||||
level.Debug(l).Log("msg", "Parsing with UTF-8 matchers parser, with fallback to classic matchers parser", "input", input, "origin", origin)
|
||||
// Parse the input in both parsers to look for disagreement and incompatible
|
||||
// inputs.
|
||||
nMatchers, nErr := parse.Matchers(input)
|
||||
cMatchers, cErr := labels.ParseMatchers(input)
|
||||
if nErr != nil {
|
||||
// If the input is invalid in both parsers, return the error.
|
||||
if cErr != nil {
|
||||
return nil, cErr
|
||||
}
|
||||
// The input is valid in the pkg/labels parser, but not the matchers/parse
|
||||
// parser. This means the input is not forwards compatible.
|
||||
var sb strings.Builder
|
||||
for i, n := range cMatchers {
|
||||
sb.WriteString(n.String())
|
||||
if i < len(cMatchers)-1 {
|
||||
sb.WriteRune(',')
|
||||
}
|
||||
}
|
||||
suggestion := sb.String()
|
||||
// The input is valid in the pkg/labels parser, but not the
|
||||
// new matchers/parse parser.
|
||||
level.Warn(l).Log("msg", "Alertmanager is moving to a new parser for labels and matchers, and this input is incompatible. Alertmanager has instead parsed the input using the classic matchers parser as a fallback. To make this input compatible with the UTF-8 matchers parser please make sure all regular expressions and values are double-quoted. If you are still seeing this message please open an issue.", "input", input, "origin", origin, "err", nErr, "suggestion", suggestion)
|
||||
return cMatchers, nil
|
||||
}
|
||||
// If the input is valid in both parsers, but produces different results,
|
||||
// then there is disagreement. We need to compare to labels.Matchers(cMatchers)
|
||||
// as cMatchers is a []*labels.Matcher not labels.Matchers.
|
||||
if nErr == nil && cErr == nil && !reflect.DeepEqual(nMatchers, labels.Matchers(cMatchers)) {
|
||||
level.Warn(l).Log("msg", "Matchers input has disagreement", "input", input, "origin", origin)
|
||||
return cMatchers, nil
|
||||
}
|
||||
return nMatchers, nil
|
||||
}
|
||||
}
|
||||
|
||||
// isValidClassicLabelName returns true if the string is a valid classic label name.
|
||||
func isValidClassicLabelName(_ log.Logger) func(model.LabelName) bool {
|
||||
return func(name model.LabelName) bool {
|
||||
return name.IsValid()
|
||||
}
|
||||
}
|
||||
|
||||
// isValidUTF8LabelName returns true if the string is a valid UTF-8 label name.
|
||||
func isValidUTF8LabelName(_ log.Logger) func(model.LabelName) bool {
|
||||
return func(name model.LabelName) bool {
|
||||
if len(name) == 0 {
|
||||
return false
|
||||
}
|
||||
return utf8.ValidString(string(name))
|
||||
}
|
||||
}
|
||||
+309
@@ -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
|
||||
}
|
||||
+304
@@ -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: %w: %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("%w: %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("%w: %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("%w: %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: %w", 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("%w: %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("%w: %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("%w: %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("%w: %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
|
||||
}
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
// 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"
|
||||
"strconv"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
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 {
|
||||
unquoted, err := strconv.Unquote(t.value)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !utf8.ValidString(unquoted) {
|
||||
return "", errors.New("quoted string contains invalid UTF-8 code points")
|
||||
}
|
||||
return unquoted, nil
|
||||
}
|
||||
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.
|
||||
}
|
||||
+15
@@ -18,7 +18,9 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/prometheus/common/model"
|
||||
)
|
||||
@@ -74,6 +76,9 @@ func NewMatcher(t MatchType, n, v string) (*Matcher, error) {
|
||||
}
|
||||
|
||||
func (m *Matcher) String() string {
|
||||
if strings.ContainsFunc(m.Name, isReserved) {
|
||||
return fmt.Sprintf(`%s%s%s`, strconv.Quote(m.Name), m.Type, strconv.Quote(m.Value))
|
||||
}
|
||||
return fmt.Sprintf(`%s%s"%s"`, m.Name, m.Type, openMetricsEscape(m.Value))
|
||||
}
|
||||
|
||||
@@ -199,3 +204,13 @@ func (ms Matchers) String() string {
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// This is copied from matchers/parse/lexer.go. It will be removed when
|
||||
// the transition window from classic matchers to UTF-8 matchers is complete,
|
||||
// as then we can use double quotes when printing the label name for all
|
||||
// matchers. Until then, the classic parser does not understand double quotes
|
||||
// around the label name, so we use this function as a heuristic to tell if
|
||||
// the matcher was parsed with the UTF-8 parser or the classic parser.
|
||||
func isReserved(r rune) bool {
|
||||
return unicode.IsSpace(r) || strings.ContainsRune("{}!=~,\\\"'`", r)
|
||||
}
|
||||
|
||||
+5
-6
@@ -14,11 +14,10 @@
|
||||
package labels
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -118,7 +117,7 @@ func ParseMatchers(s string) ([]*Matcher, error) {
|
||||
func ParseMatcher(s string) (_ *Matcher, err error) {
|
||||
ms := re.FindStringSubmatch(s)
|
||||
if len(ms) == 0 {
|
||||
return nil, errors.Errorf("bad matcher format: %s", s)
|
||||
return nil, fmt.Errorf("bad matcher format: %s", s)
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -134,7 +133,7 @@ func ParseMatcher(s string) (_ *Matcher, err error) {
|
||||
}
|
||||
|
||||
if !utf8.ValidString(rawValue) {
|
||||
return nil, errors.Errorf("matcher value not valid UTF-8: %s", ms[3])
|
||||
return nil, fmt.Errorf("matcher value not valid UTF-8: %s", ms[3])
|
||||
}
|
||||
|
||||
// Unescape the rawValue:
|
||||
@@ -163,7 +162,7 @@ func ParseMatcher(s string) (_ *Matcher, err error) {
|
||||
value.WriteByte('\\')
|
||||
case '"':
|
||||
if !expectTrailingQuote || i < len(rawValue)-1 {
|
||||
return nil, errors.Errorf("matcher value contains unescaped double quote: %s", ms[3])
|
||||
return nil, fmt.Errorf("matcher value contains unescaped double quote: %s", ms[3])
|
||||
}
|
||||
expectTrailingQuote = false
|
||||
default:
|
||||
@@ -172,7 +171,7 @@ func ParseMatcher(s string) (_ *Matcher, err error) {
|
||||
}
|
||||
|
||||
if expectTrailingQuote {
|
||||
return nil, errors.Errorf("matcher value contains unescaped double quote: %s", ms[3])
|
||||
return nil, fmt.Errorf("matcher value contains unescaped double quote: %s", ms[3])
|
||||
}
|
||||
|
||||
return NewMatcher(typeMap[ms[2]], ms[1], value.String())
|
||||
|
||||
+1
@@ -146,6 +146,7 @@ Alerts Resolved:
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
{{ define "msteams.default.summary" }}{{ template "__subject" . }}{{ end }}
|
||||
{{ define "msteams.default.title" }}{{ template "__subject" . }}{{ end }}
|
||||
{{ define "msteams.default.text" }}
|
||||
{{ if gt (len .Alerts.Firing) 0 }}
|
||||
|
||||
+40
@@ -14,6 +14,7 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -21,6 +22,7 @@ import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/common/model"
|
||||
|
||||
"github.com/prometheus/alertmanager/matchers/compat"
|
||||
"github.com/prometheus/alertmanager/pkg/labels"
|
||||
)
|
||||
|
||||
@@ -302,6 +304,39 @@ type Alert struct {
|
||||
Timeout bool
|
||||
}
|
||||
|
||||
func validateLs(ls model.LabelSet) error {
|
||||
for ln, lv := range ls {
|
||||
if !compat.IsValidLabelName(ln) {
|
||||
return fmt.Errorf("invalid name %q", ln)
|
||||
}
|
||||
if !lv.IsValid() {
|
||||
return fmt.Errorf("invalid value %q", lv)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Validate overrides the same method in model.Alert to allow UTF-8 labels.
|
||||
// This can be removed once prometheus/common has support for UTF-8.
|
||||
func (a *Alert) Validate() error {
|
||||
if a.StartsAt.IsZero() {
|
||||
return fmt.Errorf("start time missing")
|
||||
}
|
||||
if !a.EndsAt.IsZero() && a.EndsAt.Before(a.StartsAt) {
|
||||
return fmt.Errorf("start time must be before end time")
|
||||
}
|
||||
if len(a.Labels) == 0 {
|
||||
return fmt.Errorf("at least one label pair required")
|
||||
}
|
||||
if err := validateLs(a.Labels); err != nil {
|
||||
return fmt.Errorf("invalid label set: %w", err)
|
||||
}
|
||||
if err := validateLs(a.Annotations); err != nil {
|
||||
return fmt.Errorf("invalid annotations: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AlertSlice is a sortable slice of Alerts.
|
||||
type AlertSlice []*Alert
|
||||
|
||||
@@ -381,6 +416,11 @@ type Muter interface {
|
||||
Mutes(model.LabelSet) bool
|
||||
}
|
||||
|
||||
// TimeMuter determines if alerts should be muted based on the specified current time and active time interval on the route.
|
||||
type TimeMuter interface {
|
||||
Mutes(timeIntervalName []string, now time.Time) (bool, error)
|
||||
}
|
||||
|
||||
// A MuteFunc is a function that implements the Muter interface.
|
||||
type MuteFunc func(model.LabelSet) bool
|
||||
|
||||
|
||||
Reference in New Issue
Block a user