diff --git a/.chloggen/config-for-bytes-based-batchin.yaml b/.chloggen/config-for-bytes-based-batchin.yaml new file mode 100644 index 00000000000..7e58e7ed6f5 --- /dev/null +++ b/.chloggen/config-for-bytes-based-batchin.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: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: exporterhelper + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Adds the config API to support serialized bytes based batching + +# One or more tracking issues or pull requests related to the change +issues: [3262] + +# (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/exporter/exporterbatcher/config.go b/exporter/exporterbatcher/config.go index 239dc2dd4fe..8d839c2d26a 100644 --- a/exporter/exporterbatcher/config.go +++ b/exporter/exporterbatcher/config.go @@ -31,6 +31,7 @@ type MinSizeConfig struct { // sent regardless of the timeout. There is no guarantee that the batch size always greater than this value. // This option requires the Request to implement RequestItemsCounter interface. Otherwise, it will be ignored. MinSizeItems int `mapstructure:"min_size_items"` + MinSizeBytes int `mapstructure:"min_size_bytes"` } // MaxSizeConfig defines the configuration for the maximum number of items in a batch. @@ -41,18 +42,34 @@ type MaxSizeConfig struct { // If the batch size exceeds this value, it will be broken up into smaller batches if possible. // Setting this value to zero disables the maximum size limit. MaxSizeItems int `mapstructure:"max_size_items"` + MaxSizeBytes int `mapstructure:"max_size_bytes"` } func (c Config) Validate() error { + if c.MinSizeBytes != 0 && c.MinSizeItems != 0 || + c.MinSizeBytes != 0 && c.MaxSizeItems != 0 || + c.MinSizeItems != 0 && c.MaxSizeBytes != 0 || + c.MaxSizeBytes != 0 && c.MaxSizeItems != 0 { + return errors.New("byte size limit and item limit cannot be specified at the same time") + } if c.MinSizeItems < 0 { return errors.New("min_size_items must be greater than or equal to zero") } + if c.MinSizeBytes < 0 { + return errors.New("min_size_bytes must be greater than or equal to zero") + } if c.MaxSizeItems < 0 { return errors.New("max_size_items must be greater than or equal to zero") } + if c.MaxSizeBytes < 0 { + return errors.New("max_size_bytes must be greater than or equal to zero") + } if c.MaxSizeItems != 0 && c.MaxSizeItems < c.MinSizeItems { return errors.New("max_size_items must be greater than or equal to min_size_items") } + if c.MaxSizeBytes != 0 && c.MaxSizeBytes < c.MinSizeBytes { + return errors.New("max_size_bytes must be greater than or equal to min_size_bytes") + } if c.FlushTimeout <= 0 { return errors.New("timeout must be greater than zero") } diff --git a/exporter/exporterbatcher/config_test.go b/exporter/exporterbatcher/config_test.go index 6dfc1101f95..e9eec5436c0 100644 --- a/exporter/exporterbatcher/config_test.go +++ b/exporter/exporterbatcher/config_test.go @@ -5,12 +5,13 @@ package exporterbatcher import ( "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) -func TestConfig_Validate(t *testing.T) { +func TestConfig_ValidateItemBasedBatching(t *testing.T) { cfg := NewDefaultConfig() require.NoError(t, cfg.Validate()) @@ -29,3 +30,101 @@ func TestConfig_Validate(t *testing.T) { cfg.MinSizeItems = 20001 assert.EqualError(t, cfg.Validate(), "max_size_items must be greater than or equal to min_size_items") } + +func TestConfig_ValidateBytesBasedBatching(t *testing.T) { + cfg := Config{ + Enabled: true, + FlushTimeout: 200 * time.Millisecond, + MinSizeConfig: MinSizeConfig{ + MinSizeBytes: 20000, + }, + MaxSizeConfig: MaxSizeConfig{ + MaxSizeBytes: 20001, + }, + } + require.NoError(t, cfg.Validate()) + + cfg = Config{ + Enabled: true, + FlushTimeout: 200 * time.Millisecond, + MinSizeConfig: MinSizeConfig{ + MinSizeBytes: -1, + }, + MaxSizeConfig: MaxSizeConfig{ + MaxSizeBytes: 20001, + }, + } + require.EqualError(t, cfg.Validate(), "min_size_bytes must be greater than or equal to zero") + + cfg = Config{ + Enabled: true, + FlushTimeout: 200 * time.Millisecond, + MinSizeConfig: MinSizeConfig{ + MinSizeBytes: 20000, + }, + MaxSizeConfig: MaxSizeConfig{ + MaxSizeBytes: -1, + }, + } + require.EqualError(t, cfg.Validate(), "max_size_bytes must be greater than or equal to zero") + + cfg = Config{ + Enabled: true, + FlushTimeout: 200 * time.Millisecond, + MinSizeConfig: MinSizeConfig{ + MinSizeBytes: 20001, + }, + MaxSizeConfig: MaxSizeConfig{ + MaxSizeBytes: 20000, + }, + } + assert.EqualError(t, cfg.Validate(), "max_size_bytes must be greater than or equal to min_size_bytes") +} + +func TestConfig_ItemSizeLimitAndByteSizeLimitShouldNotBeBothSet(t *testing.T) { + cfg := Config{ + Enabled: true, + FlushTimeout: 200 * time.Millisecond, + MinSizeConfig: MinSizeConfig{ + MinSizeItems: 20000, + }, + MaxSizeConfig: MaxSizeConfig{ + MaxSizeBytes: 20001, + }, + } + require.EqualError(t, cfg.Validate(), "byte size limit and item limit cannot be specified at the same time") + + cfg = Config{ + Enabled: true, + FlushTimeout: 200 * time.Millisecond, + MinSizeConfig: MinSizeConfig{ + MinSizeBytes: 20000, + }, + MaxSizeConfig: MaxSizeConfig{ + MaxSizeItems: 20001, + }, + } + require.EqualError(t, cfg.Validate(), "byte size limit and item limit cannot be specified at the same time") + + cfg = Config{ + Enabled: true, + FlushTimeout: 200 * time.Millisecond, + MinSizeConfig: MinSizeConfig{}, + MaxSizeConfig: MaxSizeConfig{ + MaxSizeItems: 20001, + MaxSizeBytes: 20001, + }, + } + require.EqualError(t, cfg.Validate(), "byte size limit and item limit cannot be specified at the same time") + + cfg = Config{ + Enabled: true, + FlushTimeout: 200 * time.Millisecond, + MinSizeConfig: MinSizeConfig{ + MinSizeItems: 20001, + MinSizeBytes: 20001, + }, + MaxSizeConfig: MaxSizeConfig{}, + } + require.EqualError(t, cfg.Validate(), "byte size limit and item limit cannot be specified at the same time") +}