Skip to content

Commit

Permalink
[chore] [exporterhelper/batcher] Ignore deprecated fields if new set (#…
Browse files Browse the repository at this point in the history
…12502)

In the batcher config, ignore deprecated `min_size_items` and
`max_size_items` fields if new `min_size` and `max_size` are set.

A follow-up to
#12486.
  • Loading branch information
dmitryax authored Feb 26, 2025
1 parent 42cdb5f commit a3c772d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
4 changes: 2 additions & 2 deletions exporter/exporterbatcher/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ func (c *Config) Unmarshal(conf *confmap.Conf) error {
return err
}

if c.MinSizeItems != nil {
if c.MinSizeItems != nil && !conf.IsSet("min_size") {
c.SizeConfig.MinSize = *c.MinSizeItems
}

if c.MaxSizeItems != nil {
if c.MaxSizeItems != nil && !conf.IsSet("max_size") {
c.SizeConfig.MaxSize = *c.MaxSizeItems
}

Expand Down
71 changes: 71 additions & 0 deletions exporter/exporterbatcher/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"testing"

"github.com/stretchr/testify/require"

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

func TestValidateConfig(t *testing.T) {
Expand Down Expand Up @@ -40,3 +42,72 @@ func TestValidateSizeConfig(t *testing.T) {
}
require.EqualError(t, cfg.Validate(), "`max_size` must be greater than or equal to mix_size")
}

func TestUnmarshalDeprecatedFields(t *testing.T) {
p111 := 111
p222 := 222
tests := []struct {
name string
input map[string]any
expected Config
}{
{
name: "only_deprecated_fields_used",
input: map[string]any{
"enabled": true,
"flush_timeout": 200,
"min_size_items": 111,
"max_size_items": 222,
},
expected: Config{
Enabled: true,
FlushTimeout: 200,
SizeConfig: SizeConfig{
Sizer: SizerTypeItems,
MinSize: 111,
MaxSize: 222,
},
MinSizeConfig: MinSizeConfig{
MinSizeItems: &p111,
},
MaxSizeConfig: MaxSizeConfig{
MaxSizeItems: &p222,
},
},
},
{
name: "both_new_and_deprecated_fields_used",
input: map[string]any{
"enabled": true,
"flush_timeout": 200,
"min_size_items": 111,
"max_size_items": 222,
"min_size": 11,
"max_size": 22,
},
expected: Config{
Enabled: true,
FlushTimeout: 200,
SizeConfig: SizeConfig{
Sizer: SizerTypeItems,
MinSize: 11,
MaxSize: 22,
},
MinSizeConfig: MinSizeConfig{
MinSizeItems: &p111,
},
MaxSizeConfig: MaxSizeConfig{
MaxSizeItems: &p222,
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cfg := NewDefaultConfig()
require.NoError(t, cfg.Unmarshal(confmap.NewFromStringMap(test.input)))
require.Equal(t, test.expected, cfg)
require.NoError(t, cfg.Validate())
})
}
}

0 comments on commit a3c772d

Please sign in to comment.