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

[component] deprecate component.UnmarshalConfig #9141

Closed
Closed
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/deprecate_UnmarshalConfig.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: breaking

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Deprecate component.UnmarshalConfig. Prefer `conf.Unmarshal(cfg)` instead

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

# (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: []
7 changes: 2 additions & 5 deletions component/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,9 @@ var configValidatorType = reflect.TypeOf((*ConfigValidator)(nil)).Elem()
// UnmarshalConfig helper function to UnmarshalConfig a Config.
// It checks if the config implements confmap.Unmarshaler and uses that if available,
// otherwise uses Map.UnmarshalExact, erroring if a field is nonexistent.
// Deprecated: Use `conf.Unmarshal(cfg)` instead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the comment, and why this is needed

func UnmarshalConfig(conf *confmap.Conf, intoCfg Config) error {
if cu, ok := intoCfg.(confmap.Unmarshaler); ok {
return cu.Unmarshal(conf)
}

return conf.Unmarshal(intoCfg, confmap.WithErrorUnused())
return conf.Unmarshal(intoCfg)
}

// ConfigValidator defines an optional interface for configurations to implement to do validation.
Expand Down
17 changes: 13 additions & 4 deletions confmap/confmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,24 @@ type UnmarshalOption interface {
}

type unmarshalOption struct {
errorUnused bool
ignoreErrorUnused bool
}

// WithErrorUnused sets an option to error when there are existing
// keys in the original Conf that were unused in the decoding process
// (extra keys).
// (extra keys). This option is enabled by default and can be disabled with `WithIgnoreErrorUnused`.
func WithErrorUnused() UnmarshalOption {
return unmarshalOptionFunc(func(uo *unmarshalOption) {
uo.errorUnused = true
uo.ignoreErrorUnused = false
})
}

// WithIgnoreErrorUnused sets an option to ignore errors if existing
// keys in the original Conf were unused in the decoding process
// (extra keys).
func WithIgnoreErrorUnused() UnmarshalOption {
return unmarshalOptionFunc(func(uo *unmarshalOption) {
uo.ignoreErrorUnused = true
})
}

Expand All @@ -76,7 +85,7 @@ func (l *Conf) Unmarshal(result any, opts ...UnmarshalOption) error {
for _, opt := range opts {
opt.apply(&set)
}
return decodeConfig(l, result, set.errorUnused)
return decodeConfig(l, result, !set.ignoreErrorUnused)
}

type marshalOption struct{}
Expand Down
2 changes: 2 additions & 0 deletions confmap/confmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ func TestUnmarshalWithErrorUnused(t *testing.T) {
}
conf := NewFromStringMap(stringMap)
assert.Error(t, conf.Unmarshal(&TestIDConfig{}, WithErrorUnused()))
assert.Error(t, conf.Unmarshal(&TestIDConfig{}))
assert.NoError(t, conf.Unmarshal(&TestIDConfig{}, WithIgnoreErrorUnused()))
}

type TestConfig struct {
Expand Down
7 changes: 3 additions & 4 deletions exporter/debugexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/confmaptest"
Expand All @@ -19,7 +18,7 @@ import (
func TestUnmarshalDefaultConfig(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NoError(t, component.UnmarshalConfig(confmap.New(), cfg))
assert.NoError(t, confmap.New().Unmarshal(&cfg))
assert.Equal(t, factory.CreateDefaultConfig(), cfg)
}

Expand Down Expand Up @@ -49,7 +48,7 @@ func TestUnmarshalConfig(t *testing.T) {
require.NoError(t, err)
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
err = component.UnmarshalConfig(cm, cfg)
err = cm.Unmarshal(&cfg)
if tt.expectedErr != "" {
assert.EqualError(t, err, tt.expectedErr)
} else {
Expand Down Expand Up @@ -91,7 +90,7 @@ func Test_UnmarshalMarshalled(t *testing.T) {

outCfg := &Config{}

err = component.UnmarshalConfig(conf, outCfg)
err = conf.Unmarshal(&outCfg)

if tc.expectedErr == "" {
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion exporter/loggingexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (cfg *Config) Unmarshal(conf *confmap.Conf) error {
return fmt.Errorf("'loglevel' and 'verbosity' are incompatible. Use only 'verbosity' instead")
}

if err := conf.Unmarshal(cfg, confmap.WithErrorUnused()); err != nil {
if err := conf.Unmarshal(cfg); err != nil {
return err
}

Expand Down
13 changes: 6 additions & 7 deletions exporter/loggingexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/confmaptest"
Expand All @@ -20,7 +19,7 @@ import (
func TestUnmarshalDefaultConfig(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NoError(t, component.UnmarshalConfig(confmap.New(), cfg))
assert.NoError(t, confmap.New().Unmarshal(&cfg))
assert.Equal(t, factory.CreateDefaultConfig(), cfg)
}

Expand Down Expand Up @@ -61,11 +60,11 @@ func TestUnmarshalConfig(t *testing.T) {
},
{
filename: "invalid_verbosity_loglevel.yaml",
expectedErr: "'loglevel' and 'verbosity' are incompatible. Use only 'verbosity' instead",
expectedErr: "error decoding '': 'loglevel' and 'verbosity' are incompatible. Use only 'verbosity' instead",
},
{
filename: "config_loglevel_typo.yaml",
expectedErr: "1 error(s) decoding:\n\n* '' has invalid keys: logLevel",
expectedErr: "error decoding '': 1 error(s) decoding:\n\n* '' has invalid keys: logLevel",
},
}

Expand All @@ -75,7 +74,7 @@ func TestUnmarshalConfig(t *testing.T) {
require.NoError(t, err)
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
err = component.UnmarshalConfig(cm, cfg)
err = cm.Unmarshal(&cfg)
if tt.expectedErr != "" {
assert.EqualError(t, err, tt.expectedErr)
} else {
Expand Down Expand Up @@ -120,7 +119,7 @@ func Test_UnmarshalMarshalled(t *testing.T) {
LogLevel: zapcore.DebugLevel,
Verbosity: configtelemetry.LevelNormal,
},
expectedErr: "'loglevel' and 'verbosity' are incompatible. Use only 'verbosity' instead",
expectedErr: "error decoding '': 'loglevel' and 'verbosity' are incompatible. Use only 'verbosity' instead",
},
} {
t.Run(name, func(t *testing.T) {
Expand All @@ -135,7 +134,7 @@ func Test_UnmarshalMarshalled(t *testing.T) {

outCfg := &Config{}

err = component.UnmarshalConfig(conf, outCfg)
err = conf.Unmarshal(&outCfg)

if tc.expectedErr == "" {
assert.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions exporter/otlpexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
func TestUnmarshalDefaultConfig(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NoError(t, component.UnmarshalConfig(confmap.New(), cfg))
assert.NoError(t, confmap.New().Unmarshal(&cfg))
assert.Equal(t, factory.CreateDefaultConfig(), cfg)
}

Expand All @@ -34,7 +34,7 @@ func TestUnmarshalConfig(t *testing.T) {
require.NoError(t, err)
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NoError(t, component.UnmarshalConfig(cm, cfg))
assert.NoError(t, cm.Unmarshal(&cfg))
assert.Equal(t,
&Config{
TimeoutSettings: exporterhelper.TimeoutSettings{
Expand Down
4 changes: 2 additions & 2 deletions exporter/otlphttpexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
func TestUnmarshalDefaultConfig(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NoError(t, component.UnmarshalConfig(confmap.New(), cfg))
assert.NoError(t, confmap.New().Unmarshal(&cfg))
assert.Equal(t, factory.CreateDefaultConfig(), cfg)
// Default/Empty config is invalid.
assert.Error(t, component.ValidateConfig(cfg))
Expand All @@ -35,7 +35,7 @@ func TestUnmarshalConfig(t *testing.T) {
require.NoError(t, err)
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NoError(t, component.UnmarshalConfig(cm, cfg))
assert.NoError(t, cm.Unmarshal(&cfg))
assert.Equal(t,
&Config{
RetryConfig: configretry.BackOffConfig{
Expand Down
5 changes: 2 additions & 3 deletions extension/ballastextension/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/confmaptest"
)

func TestUnmarshalDefaultConfig(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NoError(t, component.UnmarshalConfig(confmap.New(), cfg))
assert.NoError(t, confmap.New().Unmarshal(&cfg))
assert.Equal(t, factory.CreateDefaultConfig(), cfg)
}

Expand All @@ -27,7 +26,7 @@ func TestUnmarshalConfig(t *testing.T) {
require.NoError(t, err)
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NoError(t, component.UnmarshalConfig(cm, cfg))
assert.NoError(t, cm.Unmarshal(&cfg))
assert.Equal(t,
&Config{
SizeMiB: 123,
Expand Down
5 changes: 2 additions & 3 deletions extension/zpagesextension/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/confmaptest"
Expand All @@ -19,7 +18,7 @@ import (
func TestUnmarshalDefaultConfig(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NoError(t, component.UnmarshalConfig(confmap.New(), cfg))
assert.NoError(t, confmap.New().Unmarshal(&cfg))
assert.Equal(t, factory.CreateDefaultConfig(), cfg)
}

Expand All @@ -28,7 +27,7 @@ func TestUnmarshalConfig(t *testing.T) {
require.NoError(t, err)
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NoError(t, component.UnmarshalConfig(cm, cfg))
assert.NoError(t, cm.Unmarshal(&cfg))
assert.Equal(t,
&Config{
TCPAddr: confignet.TCPAddr{
Expand Down
4 changes: 2 additions & 2 deletions otelcol/internal/configunmarshaler/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewConfigs[F component.Factory](factories map[component.Type]F) *Configs[F]

func (c *Configs[F]) Unmarshal(conf *confmap.Conf) error {
rawCfgs := make(map[component.ID]map[string]any)
if err := conf.Unmarshal(&rawCfgs, confmap.WithErrorUnused()); err != nil {
if err := conf.Unmarshal(&rawCfgs); err != nil {
return err
}

Expand All @@ -42,7 +42,7 @@ func (c *Configs[F]) Unmarshal(conf *confmap.Conf) error {

// Now that the default config struct is created we can Unmarshal into it,
// and it will apply user-defined config on top of the default.
if err := component.UnmarshalConfig(confmap.NewFromStringMap(value), cfg); err != nil {
if err := confmap.NewFromStringMap(value).Unmarshal(&cfg); err != nil {
return errorUnmarshalError(id, err)
}

Expand Down
2 changes: 1 addition & 1 deletion otelcol/unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@ func unmarshal(v *confmap.Conf, factories Factories) (*configSettings, error) {
},
}

return cfg, v.Unmarshal(&cfg, confmap.WithErrorUnused())
return cfg, v.Unmarshal(&cfg)
}
4 changes: 2 additions & 2 deletions otelcol/unmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func TestPipelineConfigUnmarshalError(t *testing.T) {
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
pips := new(pipelines.Config)
err := tt.conf.Unmarshal(&pips, confmap.WithErrorUnused())
err := tt.conf.Unmarshal(&pips)
require.Error(t, err)
assert.Contains(t, err.Error(), tt.expectError)
})
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestServiceUnmarshalError(t *testing.T) {

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
err := tt.conf.Unmarshal(&service.Config{}, confmap.WithErrorUnused())
err := tt.conf.Unmarshal(&service.Config{})
require.Error(t, err)
assert.Contains(t, err.Error(), tt.expectError)
})
Expand Down
5 changes: 2 additions & 3 deletions processor/batchprocessor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/confmaptest"
)

func TestUnmarshalDefaultConfig(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NoError(t, component.UnmarshalConfig(confmap.New(), cfg))
assert.NoError(t, confmap.New().Unmarshal(&cfg))
assert.Equal(t, factory.CreateDefaultConfig(), cfg)
}

Expand All @@ -28,7 +27,7 @@ func TestUnmarshalConfig(t *testing.T) {
require.NoError(t, err)
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NoError(t, component.UnmarshalConfig(cm, cfg))
assert.NoError(t, cm.Unmarshal(&cfg))
assert.Equal(t,
&Config{
SendBatchSize: uint32(10000),
Expand Down
5 changes: 2 additions & 3 deletions processor/memorylimiterprocessor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/confmaptest"
)

func TestUnmarshalDefaultConfig(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NoError(t, component.UnmarshalConfig(confmap.New(), cfg))
assert.NoError(t, confmap.New().Unmarshal(&cfg))
assert.Equal(t, factory.CreateDefaultConfig(), cfg)
}

Expand All @@ -28,7 +27,7 @@ func TestUnmarshalConfig(t *testing.T) {
require.NoError(t, err)
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NoError(t, component.UnmarshalConfig(cm, cfg))
assert.NoError(t, cm.Unmarshal(&cfg))
assert.Equal(t,
&Config{
CheckInterval: 5 * time.Second,
Expand Down
2 changes: 1 addition & 1 deletion receiver/otlpreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (cfg *Config) Validate() error {
// Unmarshal a confmap.Conf into the config struct.
func (cfg *Config) Unmarshal(conf *confmap.Conf) error {
// first load the config normally
err := conf.Unmarshal(cfg, confmap.WithErrorUnused())
err := conf.Unmarshal(cfg)
if err != nil {
return err
}
Expand Down
Loading
Loading