Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading