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

(exporterhelper) Configuration package for timeout sender #11387

Closed
wants to merge 17 commits into from
Closed
25 changes: 25 additions & 0 deletions .chloggen/timeout_config.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: Timeout sender configuration policy for how to respect short context deadlines.
jmacd marked this conversation as resolved.
Show resolved Hide resolved

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Policy choices are to "sustain" (default), "ignore", and "abort".

# One or more tracking issues or pull requests related to the change
issues: [11183]

# (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]
1 change: 1 addition & 0 deletions config/configtimeout/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../Makefile.Common
14 changes: 14 additions & 0 deletions config/configtimeout/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module go.opentelemetry.io/collector/config/configtimeout

go 1.22.0

require github.com/stretchr/testify v1.9.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
23 changes: 23 additions & 0 deletions config/configtimeout/go.sum

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

72 changes: 72 additions & 0 deletions config/configtimeout/timeout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package configtimeout // import "go.opentelemetry.io/collector/config/configtimeout"

import (
"errors"
"fmt"
"time"
)

// DefaultTimeout is the default timeout setting used by the
// exporterhelper Timeout sender.
const DefaultTimeout = 5 * time.Second

// Policy represents a policy towards handling short timeouts, which
// are cases where an exporter component is configured for a timeout
// and the arriving context has a shorter deadline.
type Policy string

const (
PolicySustain Policy = "sustain"
PolicyIgnore Policy = "ignore"
PolicyAbort Policy = "abort"
policyUnset Policy = ""

// PolicyDefault selects the original default behavior of the
// exporterhelper component, which is to send a request with
// shorter deadline than configured.
PolicyDefault = PolicySustain
)

// TimeoutConfig for timeout. The timeout applies to individual attempts to send data to the backend.
type TimeoutConfig 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"`

// Policy indicates how the exporter will handle requests that
// arrive with a shorter deadline than the configured timeout.
// Note that because the TimeoutConfig is traditionally
// struct-embedded in the parent configuration, we use a
// relatively long descriptive tag name--"timeout" does not
// stutter as a result.
ShortTimeoutPolicy Policy `mapstructure:"short_timeout_policy"`
}

func (ts *TimeoutConfig) Validate() error {

Check warning on line 48 in config/configtimeout/timeout.go

View check run for this annotation

Codecov / codecov/patch

config/configtimeout/timeout.go#L48

Added line #L48 was not covered by tests
// Negative timeouts are not acceptable, since all sends will fail.
if ts.Timeout < 0 {
return errors.New("'timeout' must be non-negative")

Check warning on line 51 in config/configtimeout/timeout.go

View check run for this annotation

Codecov / codecov/patch

config/configtimeout/timeout.go#L50-L51

Added lines #L50 - L51 were not covered by tests
}
return nil

Check warning on line 53 in config/configtimeout/timeout.go

View check run for this annotation

Codecov / codecov/patch

config/configtimeout/timeout.go#L53

Added line #L53 was not covered by tests
}

// NewDefaultConfig returns the default config for TimeoutConfig.
func NewDefaultConfig() TimeoutConfig {
return TimeoutConfig{
Timeout: DefaultTimeout,
ShortTimeoutPolicy: policyUnset,

Check warning on line 60 in config/configtimeout/timeout.go

View check run for this annotation

Codecov / codecov/patch

config/configtimeout/timeout.go#L57-L60

Added lines #L57 - L60 were not covered by tests
}
}

func (tp *Policy) Validate() error {
switch *tp {
case PolicySustain, PolicyIgnore, PolicyAbort, policyUnset:
return nil
default:
return fmt.Errorf("unsupported timeout type %v", *tp)
}

}
46 changes: 46 additions & 0 deletions config/configtimeout/timeout_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package configtimeout

import (
"testing"

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

func TestUnmarshalTextValid(t *testing.T) {
for _, valid := range []string{
string(PolicySustain),
string(PolicyAbort),
string(PolicyIgnore),
string(policyUnset),
string(PolicyDefault),
} {
t.Run(valid, func(t *testing.T) {
pol := Policy(valid)
require.NoError(t, pol.Validate())
assert.Equal(t, Policy(valid), pol)
})
}
}

func TestUnmarshalTextInvalid(t *testing.T) {
for _, invalid := range []string{
"unknown",
"invalid",
"etc",
} {
t.Run(invalid, func(t *testing.T) {
pol := Policy(invalid)
require.Error(t, pol.Validate())
assert.Equal(t, Policy(invalid), pol)
})
}
}

func TestTimeoutDefaultPolicy(t *testing.T) {
// The default behavior matches the legacy exporterhelper code base.
require.Equal(t, PolicySustain, PolicyDefault)
}
1 change: 1 addition & 0 deletions versions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module-sets:
- go.opentelemetry.io/collector/config/configgrpc
- go.opentelemetry.io/collector/config/confighttp
- go.opentelemetry.io/collector/config/configtelemetry
- go.opentelemetry.io/collector/config/configtimeout
- go.opentelemetry.io/collector/config/internal
- go.opentelemetry.io/collector/connector
- go.opentelemetry.io/collector/connector/connectorprofiles
Expand Down
Loading