Skip to content

Commit

Permalink
Support UTF-8 label matchers: Rename feature flags and make package p…
Browse files Browse the repository at this point in the history
…ublic (#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>
  • Loading branch information
grobinson-grafana authored Nov 17, 2023
1 parent d2b6692 commit ca5089d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
58 changes: 29 additions & 29 deletions featurecontrol/featurecontrol.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
}

Expand All @@ -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)
}
Expand All @@ -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
Expand All @@ -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 }
4 changes: 2 additions & 2 deletions featurecontrol/featurecontrol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"),
},
}
Expand Down
4 changes: 2 additions & 2 deletions matchers/compat/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ca5089d

Please sign in to comment.