Support UTF-8 label matchers: Rename feature flags and make package public (#3604)

* Rename feature flags and make package public

This commit renames the feature flags "classic-matchers-parsing"
and "utf8-matchers-parsing" to "classic-matchers" and "utf8-matchers".
This better represents their use, for example, when validating
both alerts and silences created via the API.

It also makes the feature flags package public for two reasons:

1. AllowedFlags is a public variable
2. We would rather use these consts in Mimir then have to hardcode
   the strings ourselves, and be subject to breaking changes in future

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-11-17 20:07:54 +00:00 committed by GitHub
parent d2b6692f2c
commit ca5089d33e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 33 deletions

View File

@ -23,40 +23,40 @@ import (
)
const (
fcReceiverNameInMetrics = "receiver-name-in-metrics"
fcClassicMatchersParsing = "classic-matchers-parsing"
fcUTF8MatchersParsing = "utf8-matchers-parsing"
FeatureReceiverNameInMetrics = "receiver-name-in-metrics"
FeatureClassicMode = "classic-mode"
FeatureUTF8Mode = "utf8-mode"
)
var AllowedFlags = []string{
fcReceiverNameInMetrics,
fcClassicMatchersParsing,
fcUTF8MatchersParsing,
FeatureReceiverNameInMetrics,
FeatureClassicMode,
FeatureUTF8Mode,
}
type Flagger interface {
EnableReceiverNamesInMetrics() bool
ClassicMatchersParsing() bool
UTF8MatchersParsing() bool
ClassicMode() bool
UTF8Mode() bool
}
type Flags struct {
logger log.Logger
enableReceiverNamesInMetrics bool
classicMatchersParsing bool
utf8MatchersParsing bool
classicMode bool
utf8Mode bool
}
func (f *Flags) EnableReceiverNamesInMetrics() bool {
return f.enableReceiverNamesInMetrics
}
func (f *Flags) ClassicMatchersParsing() bool {
return f.classicMatchersParsing
func (f *Flags) ClassicMode() bool {
return f.classicMode
}
func (f *Flags) UTF8MatchersParsing() bool {
return f.utf8MatchersParsing
func (f *Flags) UTF8Mode() bool {
return f.utf8Mode
}
type flagOption func(flags *Flags)
@ -67,15 +67,15 @@ func enableReceiverNameInMetrics() flagOption {
}
}
func enableClassicMatchersParsing() flagOption {
func enableClassicMode() flagOption {
return func(configs *Flags) {
configs.classicMatchersParsing = true
configs.classicMode = true
}
}
func enableUTF8MatchersParsing() flagOption {
func enableUTF8Mode() flagOption {
return func(configs *Flags) {
configs.utf8MatchersParsing = true
configs.utf8Mode = true
}
}
@ -89,15 +89,15 @@ func NewFlags(logger log.Logger, features string) (Flagger, error) {
for _, feature := range strings.Split(features, ",") {
switch feature {
case fcReceiverNameInMetrics:
case FeatureReceiverNameInMetrics:
opts = append(opts, enableReceiverNameInMetrics())
level.Warn(logger).Log("msg", "Experimental receiver name in metrics enabled")
case fcClassicMatchersParsing:
opts = append(opts, enableClassicMatchersParsing())
level.Warn(logger).Log("msg", "Classic matchers parsing enabled")
case fcUTF8MatchersParsing:
opts = append(opts, enableUTF8MatchersParsing())
level.Warn(logger).Log("msg", "UTF-8 matchers parsing enabled")
case FeatureClassicMode:
opts = append(opts, enableClassicMode())
level.Warn(logger).Log("msg", "Classic mode enabled")
case FeatureUTF8Mode:
opts = append(opts, enableUTF8Mode())
level.Warn(logger).Log("msg", "UTF-8 mode enabled")
default:
return nil, fmt.Errorf("Unknown option '%s' for --enable-feature", feature)
}
@ -107,8 +107,8 @@ func NewFlags(logger log.Logger, features string) (Flagger, error) {
opt(fc)
}
if fc.classicMatchersParsing && fc.utf8MatchersParsing {
return nil, errors.New("Both classic and UTF-8 matchers parsing is enabled, please choose one or remove the flag for both")
if fc.classicMode && fc.utf8Mode {
return nil, errors.New("cannot have both classic and UTF-8 modes enabled")
}
return fc, nil
@ -118,6 +118,6 @@ type NoopFlags struct{}
func (n NoopFlags) EnableReceiverNamesInMetrics() bool { return false }
func (n NoopFlags) ClassicMatchersParsing() bool { return false }
func (n NoopFlags) ClassicMode() bool { return false }
func (n NoopFlags) UTF8MatchersParsing() bool { return false }
func (n NoopFlags) UTF8Mode() bool { return false }

View File

@ -30,7 +30,7 @@ func TestFlags(t *testing.T) {
}{
{
name: "with only valid feature flags",
featureFlags: fcReceiverNameInMetrics,
featureFlags: FeatureReceiverNameInMetrics,
},
{
name: "with only invalid feature flags",
@ -39,7 +39,7 @@ func TestFlags(t *testing.T) {
},
{
name: "with both, valid and invalid feature flags",
featureFlags: strings.Join([]string{fcReceiverNameInMetrics, "somethingbad"}, ","),
featureFlags: strings.Join([]string{FeatureReceiverNameInMetrics, "somethingbad"}, ","),
err: errors.New("Unknown option 'somethingbad' for --enable-feature"),
},
}

View File

@ -48,10 +48,10 @@ func Matchers(s string) (labels.Matchers, error) {
// InitFromFlags initializes the compat package from the flagger.
func InitFromFlags(l log.Logger, f featurecontrol.Flagger) {
if f.ClassicMatchersParsing() {
if f.ClassicMode() {
parseMatcher = classicMatcherParser(l)
parseMatchers = classicMatchersParser(l)
} else if f.UTF8MatchersParsing() {
} else if f.UTF8Mode() {
parseMatcher = utf8MatcherParser(l)
parseMatchers = utf8MatchersParser(l)
} else {