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

Add RetrySettings validation function #9089

Merged
merged 1 commit into from
Dec 12, 2023
Merged
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
13 changes: 13 additions & 0 deletions .chloggen/addretrysetvalidation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 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: "Add RetrySettings validation function"

# One or more tracking issues or pull requests related to the change
issues: [9089]
22 changes: 22 additions & 0 deletions exporter/exporterhelper/retry_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@ type RetrySettings struct {
MaxElapsedTime time.Duration `mapstructure:"max_elapsed_time"`
}

func (cfg *RetrySettings) Validate() error {
bogdandrutu marked this conversation as resolved.
Show resolved Hide resolved
if !cfg.Enabled {
return nil
}
if cfg.InitialInterval < 0 {
return errors.New("'initial_interval' must be non-negative")
}
if cfg.RandomizationFactor < 0 || cfg.RandomizationFactor > 1 {
return errors.New("'randomization_factor' must be within [0, 1]")
}
if cfg.Multiplier <= 0 {
return errors.New("'multiplier' must be positive")
}
if cfg.MaxInterval < 0 {
return errors.New("'max_interval' must be non-negative")
}
if cfg.MaxElapsedTime < 0 {
return errors.New("'max_elapsed' time must be non-negative")
}
return nil
}

// NewDefaultRetrySettings returns the default settings for RetrySettings.
func NewDefaultRetrySettings() RetrySettings {
return RetrySettings{
Expand Down
63 changes: 63 additions & 0 deletions exporter/exporterhelper/retry_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,66 @@ func tagsMatchLabelKeys(tags []tag.Tag, keys []metricdata.LabelKey, labels []met
}
return true
}

func TestNewDefaultRetrySettings(t *testing.T) {
cfg := NewDefaultRetrySettings()
assert.NoError(t, cfg.Validate())
assert.Equal(t,
RetrySettings{
Enabled: true,
InitialInterval: 5 * time.Second,
RandomizationFactor: 0.5,
Multiplier: 1.5,
MaxInterval: 30 * time.Second,
MaxElapsedTime: 5 * time.Minute,
}, cfg)
}

func TestInvalidInitialInterval(t *testing.T) {
cfg := NewDefaultRetrySettings()
assert.NoError(t, cfg.Validate())
cfg.InitialInterval = -1
assert.Error(t, cfg.Validate())
}

func TestInvalidRandomizationFactor(t *testing.T) {
cfg := NewDefaultRetrySettings()
assert.NoError(t, cfg.Validate())
cfg.RandomizationFactor = -1
assert.Error(t, cfg.Validate())
cfg.RandomizationFactor = 2
assert.Error(t, cfg.Validate())
}

func TestInvalidMultiplier(t *testing.T) {
cfg := NewDefaultRetrySettings()
assert.NoError(t, cfg.Validate())
cfg.Multiplier = 0
assert.Error(t, cfg.Validate())
}

func TestInvalidMaxInterval(t *testing.T) {
cfg := NewDefaultRetrySettings()
assert.NoError(t, cfg.Validate())
cfg.MaxInterval = -1
assert.Error(t, cfg.Validate())
}

func TestInvalidMaxElapsedTime(t *testing.T) {
cfg := NewDefaultRetrySettings()
assert.NoError(t, cfg.Validate())
cfg.MaxElapsedTime = -1
assert.Error(t, cfg.Validate())
}

func TestDisabledWithInvalidValues(t *testing.T) {
cfg := RetrySettings{
Enabled: false,
InitialInterval: -1,
RandomizationFactor: -1,
Multiplier: 0,
MaxInterval: -1,
MaxElapsedTime: -1,
}
assert.NoError(t, cfg.Validate())
}
Loading