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

[featuregate] Deprecate NewFlag in favor of RegisterFlags #8727

Merged
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
25 changes: 25 additions & 0 deletions .chloggen/mx-psi_featuregate-futureproof.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: deprecation

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: featuregate

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Deprecate `featuregate.NewFlag` in favor of `featuregate.Registry`'s `RegisterFlags` method

# One or more tracking issues or pull requests related to the change
issues: [8727]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
20 changes: 20 additions & 0 deletions featuregate/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,31 @@
"go.uber.org/multierr"
)

const (
featureGatesFlag = "feature-gates"
featureGatesFlagDescription = "Comma-delimited list of feature gate identifiers. Prefix with '-' to disable the feature. '+' or no prefix will enable the feature."
)

// NewFlag returns a flag.Value that directly applies feature gate statuses to a Registry.
// Deprecated: Use Registry's RegisterFlags method instead.
func NewFlag(reg *Registry) flag.Value {
return newFeatureGateValue(reg)

Check warning on line 21 in featuregate/flag.go

View check run for this annotation

Codecov / codecov/patch

featuregate/flag.go#L21

Added line #L21 was not covered by tests
}

func newFeatureGateValue(reg *Registry) flag.Value {
return &flagValue{reg: reg}
}

// RegisterFlagsOption is an option for RegisterFlags.
type RegisterFlagsOption interface {
private()
}

// RegisterFlags that directly applies feature gate statuses to a Registry.
func (r *Registry) RegisterFlags(flagSet *flag.FlagSet, _ ...RegisterFlagsOption) {
flagSet.Var(newFeatureGateValue(r), featureGatesFlag, featureGatesFlagDescription)
}

// flagValue implements the flag.Value interface and directly applies feature gate statuses to a Registry.
type flagValue struct {
reg *Registry
Expand Down
12 changes: 8 additions & 4 deletions featuregate/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package featuregate

import (
"flag"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -113,18 +114,21 @@ func TestNewFlag(t *testing.T) {
reg.MustRegister("beta", StageBeta)
reg.MustRegister("deprecated", StageDeprecated, WithRegisterToVersion("1.0.0"))
reg.MustRegister("stable", StageStable, WithRegisterToVersion("1.0.0"))
v := NewFlag(reg)
fs := flag.NewFlagSet("test", flag.ContinueOnError)
reg.RegisterFlags(fs)
registrationFlag := fs.Lookup(featureGatesFlag)
require.NotNil(t, registrationFlag)
if tt.expectedSetErr {
require.Error(t, v.Set(tt.input))
require.Error(t, registrationFlag.Value.Set(tt.input))
} else {
require.NoError(t, v.Set(tt.input))
require.NoError(t, registrationFlag.Value.Set(tt.input))
}
got := map[string]bool{}
reg.VisitAll(func(g *Gate) {
got[g.ID()] = g.IsEnabled()
})
assert.Equal(t, tt.expected, got)
assert.Equal(t, tt.expectedStr, v.String())
assert.Equal(t, tt.expectedStr, registrationFlag.Value.String())
})
}
}
7 changes: 2 additions & 5 deletions otelcol/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
)

const (
configFlag = "config"
featureGatesFlag = "feature-gates"
configFlag = "config"
)

type configFlagValue struct {
Expand Down Expand Up @@ -50,9 +49,7 @@ func flags(reg *featuregate.Registry) *flag.FlagSet {
return nil
})

flagSet.Var(featuregate.NewFlag(reg), featureGatesFlag,
"Comma-delimited list of feature gate identifiers. Prefix with '-' to disable the feature. '+' or no prefix will enable the feature.")

reg.RegisterFlags(flagSet)
return flagSet
}

Expand Down
Loading