diff --git a/.chloggen/mx-psi_featuregate-futureproof.yaml b/.chloggen/mx-psi_featuregate-futureproof.yaml new file mode 100755 index 00000000000..da1cbef2074 --- /dev/null +++ b/.chloggen/mx-psi_featuregate-futureproof.yaml @@ -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] diff --git a/featuregate/flag.go b/featuregate/flag.go index 3ff105d3e69..1464e4808f7 100644 --- a/featuregate/flag.go +++ b/featuregate/flag.go @@ -10,11 +10,31 @@ import ( "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) +} + +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 diff --git a/featuregate/flag_test.go b/featuregate/flag_test.go index b7ee5fc433c..0eb60330794 100644 --- a/featuregate/flag_test.go +++ b/featuregate/flag_test.go @@ -4,6 +4,7 @@ package featuregate import ( + "flag" "testing" "github.com/stretchr/testify/assert" @@ -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()) }) } } diff --git a/otelcol/flags.go b/otelcol/flags.go index a2d0885f53e..06f9989adb3 100644 --- a/otelcol/flags.go +++ b/otelcol/flags.go @@ -12,8 +12,7 @@ import ( ) const ( - configFlag = "config" - featureGatesFlag = "feature-gates" + configFlag = "config" ) type configFlagValue struct { @@ -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 }