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

[exporter][batching] configuration and config validation for bytes based batching #12154

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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/config-for-bytes-based-batchin.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: 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]
17 changes: 17 additions & 0 deletions exporter/exporterbatcher/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Comment on lines 33 to +34
Copy link
Member

Choose a reason for hiding this comment

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

What if we have a "Sizer"("SizerType") as enum with 3 values (request, items, bytes) and the "size" value instead?

Copy link
Contributor Author

@sfc-gh-sili sfc-gh-sili Jan 22, 2025

Choose a reason for hiding this comment

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

Good idea. How about something like this:

type BatcherConfig struct {
	...
	minSize SizeConfig `mapstructure:"min_size"`
	maxSize SizeConfig `mapstructure:"max_size"`
}
type SizeConfig struct {
	sizer string `mapstructure:"sizer"`
	size int `mapstructure:"size"`
}
func (c SizeConfig) validate() {
	...
}

Does the option request mean that the request will implement its own sizing method?

Copy link
Member

Choose a reason for hiding this comment

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

Not sure if we need "sizer" in both min/max, since I don't think we need to accept different sizer for min and max.

Copy link
Contributor Author

@sfc-gh-sili sfc-gh-sili Jan 22, 2025

Choose a reason for hiding this comment

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

I don't think we need to accept different sizer for min and max.

Good point. How about

type BatcherConfig struct {
	...
	sizer SizeType `mapstructure:",squash"`
	minSize int `mapstructure:"max_size"`
	maxSize int `mapstructure:"max_size"`
}
type SizerType struct {
	sizer string `mapstructure:"sizer"`
}
func (c SizerType) validate() {
}

}

// MaxSizeConfig defines the configuration for the maximum number of items in a batch.
Expand All @@ -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")
}
Expand Down
101 changes: 100 additions & 1 deletion exporter/exporterbatcher/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand All @@ -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")
}
Loading