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 config Validate for exporterhelper.TimeoutSettings #9104

Merged
merged 1 commit into from
Dec 14, 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/validatetimeout.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 config Validate for TimeoutSettings"

# One or more tracking issues or pull requests related to the change
issues: [9104]
23 changes: 17 additions & 6 deletions exporter/exporterhelper/timeout_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@ package exporterhelper // import "go.opentelemetry.io/collector/exporter/exporte

import (
"context"
"errors"
"time"
)

// TimeoutSettings for timeout. The timeout applies to individual attempts to send data to the backend.
type TimeoutSettings struct {
// Timeout is the timeout for every attempt to send data to the backend.
// A zero timeout means no timeout.
Timeout time.Duration `mapstructure:"timeout"`
}

func (ts *TimeoutSettings) Validate() error {
// Negative timeouts are not acceptable, since all sends will fail.
if ts.Timeout < 0 {
return errors.New("'timeout' must be non-negative")
}
return nil
}

// NewDefaultTimeoutSettings returns the default settings for TimeoutSettings.
func NewDefaultTimeoutSettings() TimeoutSettings {
return TimeoutSettings{
Expand All @@ -28,12 +38,13 @@ type timeoutSender struct {
}

func (ts *timeoutSender) send(ctx context.Context, req Request) error {
// TODO: Remove this by avoiding to create the timeout sender if timeout is 0.
if ts.cfg.Timeout == 0 {
return req.Export(ctx)
}
// Intentionally don't overwrite the context inside the request, because in case of retries deadline will not be
// updated because this deadline most likely is before the next one.
if ts.cfg.Timeout > 0 {
var cancelFunc func()
ctx, cancelFunc = context.WithTimeout(ctx, ts.cfg.Timeout)
defer cancelFunc()
}
return req.Export(ctx)
tCtx, cancelFunc := context.WithTimeout(ctx, ts.cfg.Timeout)
defer cancelFunc()
return req.Export(tCtx)
}
24 changes: 24 additions & 0 deletions exporter/exporterhelper/timeout_sender_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package exporterhelper

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestNewDefaultTimeoutSettings(t *testing.T) {
cfg := NewDefaultTimeoutSettings()
assert.NoError(t, cfg.Validate())
assert.Equal(t, TimeoutSettings{Timeout: 5 * time.Second}, cfg)
}

func TestInvalidTimeout(t *testing.T) {
cfg := NewDefaultTimeoutSettings()
assert.NoError(t, cfg.Validate())
cfg.Timeout = -1
assert.Error(t, cfg.Validate())
}
Loading