Skip to content

Commit

Permalink
Add config Validate for exporterhelper.TimeoutSettings (open-telemetr…
Browse files Browse the repository at this point in the history
…y#9104)

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu authored and sokoide committed Dec 18, 2023
1 parent 8e607de commit 13022bf
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
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())
}

0 comments on commit 13022bf

Please sign in to comment.