Skip to content

Commit

Permalink
[exporter][batching] configuration and config validation for bytes ba…
Browse files Browse the repository at this point in the history
…sed batching (#12154)

<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description

This PR adds config API that will be used for serialized bytes based
batching.

We will deprecate `MinSizeConfig` and `MaxSizeConfig` in favor of:

```
type SizeConfig struct {
	Sizer string `mapstructure:"sizer"`
	MinSize int `mapstructure:"mix_size"`
	MaxSize int `mapstructure:"max_size"`
}
```

<!-- Issue number if applicable -->
#### Link to tracking issue
#3262
#12303

<!--Describe what testing was performed and which tests were added.-->
#### Testing

<!--Describe the documentation added.-->
#### Documentation

<!--Please delete paragraphs that you did not use before submitting.-->

---------

Co-authored-by: Dmitrii Anoshin <[email protected]>
  • Loading branch information
sfc-gh-sili and dmitryax authored Feb 21, 2025
1 parent b37cd33 commit ef5960e
Show file tree
Hide file tree
Showing 16 changed files with 198 additions and 1 deletion.
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]
2 changes: 2 additions & 0 deletions cmd/otelcorecol/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions exporter/debugexporter/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions exporter/exporterbatcher/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,21 @@ type Config struct {
// FlushTimeout sets the time after which a batch will be sent regardless of its size.
FlushTimeout time.Duration `mapstructure:"flush_timeout"`

SizeConfig `mapstructure:",squash"`

// Deprecated. Ignored if SizeConfig is set.
MinSizeConfig `mapstructure:",squash"`
// Deprecated. Ignored if SizeConfig is set.
MaxSizeConfig `mapstructure:",squash"`
}

type SizeConfig struct {
Sizer SizerType `mapstructure:"sizer"`

MinSize int `mapstructure:"mix_size"`
MaxSize int `mapstructure:"max_size"`
}

// MinSizeConfig defines the configuration for the minimum number of items in a batch.
// Experimental: This API is at the early stage of development and may change without backward compatibility
// until https://github.com/open-telemetry/opentelemetry-collector/issues/8122 is resolved.
Expand Down Expand Up @@ -59,6 +70,19 @@ func (c Config) Validate() error {
return nil
}

func (c SizeConfig) Validate() error {
if c.MinSize < 0 {
return errors.New("min_size must be greater than or equal to zero")
}
if c.MaxSize < 0 {
return errors.New("max_size must be greater than or equal to zero")
}
if c.MaxSize != 0 && c.MaxSize < c.MinSize {
return errors.New("max_size must be greater than or equal to mix_size")
}
return nil
}

func NewDefaultConfig() Config {
return Config{
Enabled: true,
Expand Down
54 changes: 53 additions & 1 deletion exporter/exporterbatcher/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"

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

func TestConfig_Validate(t *testing.T) {
func TestValidateConfig(t *testing.T) {
cfg := NewDefaultConfig()
require.NoError(t, cfg.Validate())

Expand All @@ -29,3 +32,52 @@ 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 TestValidateSizeConfig(t *testing.T) {
cfg := SizeConfig{
Sizer: SizerTypeItems,
MaxSize: -100,
MinSize: 100,
}
require.EqualError(t, cfg.Validate(), "max_size must be greater than or equal to zero")

cfg = SizeConfig{
Sizer: SizerTypeBytes,
MaxSize: 100,
MinSize: -100,
}
require.EqualError(t, cfg.Validate(), "min_size must be greater than or equal to zero")

cfg = SizeConfig{
Sizer: SizerTypeBytes,
MaxSize: 100,
MinSize: 200,
}
require.EqualError(t, cfg.Validate(), "max_size must be greater than or equal to mix_size")
}

func TestSizeUnmarshaler(t *testing.T) {
var rawConf map[string]any
cfg := NewDefaultConfig()

require.NoError(t, yaml.Unmarshal([]byte(`sizer: bytes`), &rawConf))
require.NoError(t, confmap.NewFromStringMap(rawConf).Unmarshal(&cfg))
require.NoError(t, cfg.Validate())

require.NoError(t, yaml.Unmarshal([]byte(`sizer: "bytes"`), &rawConf))
require.NoError(t, confmap.NewFromStringMap(rawConf).Unmarshal(&cfg))
require.NoError(t, cfg.Validate())

require.NoError(t, yaml.Unmarshal([]byte(`sizer: items`), &rawConf))
require.NoError(t, confmap.NewFromStringMap(rawConf).Unmarshal(&cfg))
require.NoError(t, cfg.Validate())

require.NoError(t, yaml.Unmarshal([]byte(`sizer: 'items'`), &rawConf))
require.NoError(t, confmap.NewFromStringMap(rawConf).Unmarshal(&cfg))
require.NoError(t, cfg.Validate())

require.NoError(t, yaml.Unmarshal([]byte(`sizer: invalid`), &rawConf))
require.EqualError(t,
confmap.NewFromStringMap(rawConf).Unmarshal(&cfg),
"decoding failed due to the following error(s):\n\nerror decoding 'sizer': invalid sizer: \"invalid\"")
}
28 changes: 28 additions & 0 deletions exporter/exporterbatcher/sizer_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package exporterbatcher // import "go.opentelemetry.io/collector/exporter/exporterbatcher"

import (
"fmt"
)

type SizerType string

const (
SizerTypeItems SizerType = "items"
SizerTypeBytes SizerType = "bytes"
)

// UnmarshalText implements TextUnmarshaler interface.
func (s *SizerType) UnmarshalText(text []byte) error {
switch str := string(text); str {
case string(SizerTypeItems):
*s = SizerTypeItems
case string(SizerTypeBytes):
*s = SizerTypeBytes
default:
return fmt.Errorf("invalid sizer: %q", str)
}
return nil
}
2 changes: 2 additions & 0 deletions exporter/exporterhelper/xexporterhelper/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,5 @@ replace go.opentelemetry.io/collector/extension/extensiontest => ../../../extens
replace go.opentelemetry.io/collector/featuregate => ../../../featuregate

replace go.opentelemetry.io/collector/extension/xextension => ../../../extension/xextension

replace go.opentelemetry.io/collector/confmap => ../../../confmap
14 changes: 14 additions & 0 deletions exporter/exporterhelper/xexporterhelper/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions exporter/exportertest/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,5 @@ replace go.opentelemetry.io/collector/extension/extensiontest => ../../extension
replace go.opentelemetry.io/collector/featuregate => ../../featuregate

replace go.opentelemetry.io/collector/extension/xextension => ../../extension/xextension

replace go.opentelemetry.io/collector/confmap => ../../confmap
14 changes: 14 additions & 0 deletions exporter/exportertest/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions exporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
go.opentelemetry.io/collector/component v0.120.0
go.opentelemetry.io/collector/component/componenttest v0.120.0
go.opentelemetry.io/collector/config/configretry v1.26.0
go.opentelemetry.io/collector/confmap v1.24.0
go.opentelemetry.io/collector/consumer v1.26.0
go.opentelemetry.io/collector/consumer/consumererror v0.120.0
go.opentelemetry.io/collector/consumer/consumertest v0.120.0
Expand All @@ -26,16 +27,23 @@ require (
go.uber.org/goleak v1.3.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.27.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
github.com/knadh/koanf/v2 v2.1.2 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down Expand Up @@ -97,3 +105,5 @@ replace go.opentelemetry.io/collector/extension/extensiontest => ../extension/ex
replace go.opentelemetry.io/collector/featuregate => ../featuregate

replace go.opentelemetry.io/collector/extension/xextension => ../extension/xextension

replace go.opentelemetry.io/collector/confmap => ../confmap
14 changes: 14 additions & 0 deletions exporter/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions exporter/otlpexporter/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ef5960e

Please sign in to comment.