Skip to content

Commit ef5960e

Browse files
[exporter][batching] configuration and config validation for bytes based 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]>
1 parent b37cd33 commit ef5960e

File tree

16 files changed

+198
-1
lines changed

16 files changed

+198
-1
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: exporterhelper
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Adds the config API to support serialized bytes based batching
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [3262]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [api]

cmd/otelcorecol/go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exporter/debugexporter/go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exporter/exporterbatcher/config.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,21 @@ type Config struct {
1919
// FlushTimeout sets the time after which a batch will be sent regardless of its size.
2020
FlushTimeout time.Duration `mapstructure:"flush_timeout"`
2121

22+
SizeConfig `mapstructure:",squash"`
23+
24+
// Deprecated. Ignored if SizeConfig is set.
2225
MinSizeConfig `mapstructure:",squash"`
26+
// Deprecated. Ignored if SizeConfig is set.
2327
MaxSizeConfig `mapstructure:",squash"`
2428
}
2529

30+
type SizeConfig struct {
31+
Sizer SizerType `mapstructure:"sizer"`
32+
33+
MinSize int `mapstructure:"mix_size"`
34+
MaxSize int `mapstructure:"max_size"`
35+
}
36+
2637
// MinSizeConfig defines the configuration for the minimum number of items in a batch.
2738
// Experimental: This API is at the early stage of development and may change without backward compatibility
2839
// until https://github.com/open-telemetry/opentelemetry-collector/issues/8122 is resolved.
@@ -59,6 +70,19 @@ func (c Config) Validate() error {
5970
return nil
6071
}
6172

73+
func (c SizeConfig) Validate() error {
74+
if c.MinSize < 0 {
75+
return errors.New("min_size must be greater than or equal to zero")
76+
}
77+
if c.MaxSize < 0 {
78+
return errors.New("max_size must be greater than or equal to zero")
79+
}
80+
if c.MaxSize != 0 && c.MaxSize < c.MinSize {
81+
return errors.New("max_size must be greater than or equal to mix_size")
82+
}
83+
return nil
84+
}
85+
6286
func NewDefaultConfig() Config {
6387
return Config{
6488
Enabled: true,

exporter/exporterbatcher/config_test.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ import (
88

99
"github.com/stretchr/testify/assert"
1010
"github.com/stretchr/testify/require"
11+
"gopkg.in/yaml.v2"
12+
13+
"go.opentelemetry.io/collector/confmap"
1114
)
1215

13-
func TestConfig_Validate(t *testing.T) {
16+
func TestValidateConfig(t *testing.T) {
1417
cfg := NewDefaultConfig()
1518
require.NoError(t, cfg.Validate())
1619

@@ -29,3 +32,52 @@ func TestConfig_Validate(t *testing.T) {
2932
cfg.MinSizeItems = 20001
3033
assert.EqualError(t, cfg.Validate(), "max_size_items must be greater than or equal to min_size_items")
3134
}
35+
36+
func TestValidateSizeConfig(t *testing.T) {
37+
cfg := SizeConfig{
38+
Sizer: SizerTypeItems,
39+
MaxSize: -100,
40+
MinSize: 100,
41+
}
42+
require.EqualError(t, cfg.Validate(), "max_size must be greater than or equal to zero")
43+
44+
cfg = SizeConfig{
45+
Sizer: SizerTypeBytes,
46+
MaxSize: 100,
47+
MinSize: -100,
48+
}
49+
require.EqualError(t, cfg.Validate(), "min_size must be greater than or equal to zero")
50+
51+
cfg = SizeConfig{
52+
Sizer: SizerTypeBytes,
53+
MaxSize: 100,
54+
MinSize: 200,
55+
}
56+
require.EqualError(t, cfg.Validate(), "max_size must be greater than or equal to mix_size")
57+
}
58+
59+
func TestSizeUnmarshaler(t *testing.T) {
60+
var rawConf map[string]any
61+
cfg := NewDefaultConfig()
62+
63+
require.NoError(t, yaml.Unmarshal([]byte(`sizer: bytes`), &rawConf))
64+
require.NoError(t, confmap.NewFromStringMap(rawConf).Unmarshal(&cfg))
65+
require.NoError(t, cfg.Validate())
66+
67+
require.NoError(t, yaml.Unmarshal([]byte(`sizer: "bytes"`), &rawConf))
68+
require.NoError(t, confmap.NewFromStringMap(rawConf).Unmarshal(&cfg))
69+
require.NoError(t, cfg.Validate())
70+
71+
require.NoError(t, yaml.Unmarshal([]byte(`sizer: items`), &rawConf))
72+
require.NoError(t, confmap.NewFromStringMap(rawConf).Unmarshal(&cfg))
73+
require.NoError(t, cfg.Validate())
74+
75+
require.NoError(t, yaml.Unmarshal([]byte(`sizer: 'items'`), &rawConf))
76+
require.NoError(t, confmap.NewFromStringMap(rawConf).Unmarshal(&cfg))
77+
require.NoError(t, cfg.Validate())
78+
79+
require.NoError(t, yaml.Unmarshal([]byte(`sizer: invalid`), &rawConf))
80+
require.EqualError(t,
81+
confmap.NewFromStringMap(rawConf).Unmarshal(&cfg),
82+
"decoding failed due to the following error(s):\n\nerror decoding 'sizer': invalid sizer: \"invalid\"")
83+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package exporterbatcher // import "go.opentelemetry.io/collector/exporter/exporterbatcher"
5+
6+
import (
7+
"fmt"
8+
)
9+
10+
type SizerType string
11+
12+
const (
13+
SizerTypeItems SizerType = "items"
14+
SizerTypeBytes SizerType = "bytes"
15+
)
16+
17+
// UnmarshalText implements TextUnmarshaler interface.
18+
func (s *SizerType) UnmarshalText(text []byte) error {
19+
switch str := string(text); str {
20+
case string(SizerTypeItems):
21+
*s = SizerTypeItems
22+
case string(SizerTypeBytes):
23+
*s = SizerTypeBytes
24+
default:
25+
return fmt.Errorf("invalid sizer: %q", str)
26+
}
27+
return nil
28+
}

exporter/exporterhelper/xexporterhelper/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,5 @@ replace go.opentelemetry.io/collector/extension/extensiontest => ../../../extens
102102
replace go.opentelemetry.io/collector/featuregate => ../../../featuregate
103103

104104
replace go.opentelemetry.io/collector/extension/xextension => ../../../extension/xextension
105+
106+
replace go.opentelemetry.io/collector/confmap => ../../../confmap

exporter/exporterhelper/xexporterhelper/go.sum

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exporter/exportertest/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,5 @@ replace go.opentelemetry.io/collector/extension/extensiontest => ../../extension
9292
replace go.opentelemetry.io/collector/featuregate => ../../featuregate
9393

9494
replace go.opentelemetry.io/collector/extension/xextension => ../../extension/xextension
95+
96+
replace go.opentelemetry.io/collector/confmap => ../../confmap

exporter/exportertest/go.sum

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exporter/go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
go.opentelemetry.io/collector/component v0.120.0
99
go.opentelemetry.io/collector/component/componenttest v0.120.0
1010
go.opentelemetry.io/collector/config/configretry v1.26.0
11+
go.opentelemetry.io/collector/confmap v1.24.0
1112
go.opentelemetry.io/collector/consumer v1.26.0
1213
go.opentelemetry.io/collector/consumer/consumererror v0.120.0
1314
go.opentelemetry.io/collector/consumer/consumertest v0.120.0
@@ -26,16 +27,23 @@ require (
2627
go.uber.org/goleak v1.3.0
2728
go.uber.org/multierr v1.11.0
2829
go.uber.org/zap v1.27.0
30+
gopkg.in/yaml.v2 v2.4.0
2931
)
3032

3133
require (
3234
github.com/davecgh/go-spew v1.1.1 // indirect
3335
github.com/go-logr/logr v1.4.2 // indirect
3436
github.com/go-logr/stdr v1.2.2 // indirect
37+
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
3538
github.com/gogo/protobuf v1.3.2 // indirect
3639
github.com/google/uuid v1.6.0 // indirect
3740
github.com/hashicorp/go-version v1.7.0 // indirect
3841
github.com/json-iterator/go v1.1.12 // indirect
42+
github.com/knadh/koanf/maps v0.1.1 // indirect
43+
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
44+
github.com/knadh/koanf/v2 v2.1.2 // indirect
45+
github.com/mitchellh/copystructure v1.2.0 // indirect
46+
github.com/mitchellh/reflectwalk v1.0.2 // indirect
3947
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4048
github.com/modern-go/reflect2 v1.0.2 // indirect
4149
github.com/pmezard/go-difflib v1.0.0 // indirect
@@ -97,3 +105,5 @@ replace go.opentelemetry.io/collector/extension/extensiontest => ../extension/ex
97105
replace go.opentelemetry.io/collector/featuregate => ../featuregate
98106

99107
replace go.opentelemetry.io/collector/extension/xextension => ../extension/xextension
108+
109+
replace go.opentelemetry.io/collector/confmap => ../confmap

exporter/go.sum

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exporter/otlpexporter/go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)