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): Add exporterhelper timeout policy support #11385

Closed
wants to merge 7 commits into from
Closed
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
25 changes: 25 additions & 0 deletions .chloggen/exporter_timeout_policy.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 has a policy for respecting context deadlines.

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Policy choices are "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: [user]
1 change: 1 addition & 0 deletions cmd/otelcorecol/builder-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ replaces:
- go.opentelemetry.io/collector/config/configopaque => ../../config/configopaque
- go.opentelemetry.io/collector/config/configretry => ../../config/configretry
- go.opentelemetry.io/collector/config/configtelemetry => ../../config/configtelemetry
- go.opentelemetry.io/collector/config/configtimeout => ../../config/configtimeout
- go.opentelemetry.io/collector/config/configtls => ../../config/configtls
- go.opentelemetry.io/collector/config/internal => ../../config/internal
- go.opentelemetry.io/collector/confmap => ../../confmap
Expand Down
3 changes: 3 additions & 0 deletions cmd/otelcorecol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ require (
go.opentelemetry.io/collector/config/configopaque v1.17.0 // indirect
go.opentelemetry.io/collector/config/configretry v1.17.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.111.0 // indirect
go.opentelemetry.io/collector/config/configtimeout v0.111.0 // indirect
go.opentelemetry.io/collector/config/configtls v1.17.0 // indirect
go.opentelemetry.io/collector/config/internal v0.111.0 // indirect
go.opentelemetry.io/collector/connector/connectorprofiles v0.111.0 // indirect
Expand Down Expand Up @@ -179,6 +180,8 @@ replace go.opentelemetry.io/collector/config/configretry => ../../config/configr

replace go.opentelemetry.io/collector/config/configtelemetry => ../../config/configtelemetry

replace go.opentelemetry.io/collector/config/configtimeout => ../../config/configtimeout

replace go.opentelemetry.io/collector/config/configtls => ../../config/configtls

replace go.opentelemetry.io/collector/config/internal => ../../config/internal
Expand Down
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
35 changes: 35 additions & 0 deletions config/configtimeout/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module go.opentelemetry.io/collector/config/configtimeout

go 1.22.0

require (
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/collector/component v0.111.0
go.opentelemetry.io/collector/confmap v1.17.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-viper/mapstructure/v2 v2.1.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
github.com/knadh/koanf/v2 v2.1.1 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.111.0 // indirect
go.opentelemetry.io/collector/pdata v1.17.0 // indirect
go.opentelemetry.io/otel v1.30.0 // indirect
go.opentelemetry.io/otel/metric v1.30.0 // indirect
go.opentelemetry.io/otel/trace v1.30.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.17.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
google.golang.org/grpc v1.67.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
98 changes: 98 additions & 0 deletions config/configtimeout/go.sum

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

2 changes: 2 additions & 0 deletions config/configtimeout/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
timeout: 17s
short_timeout_policy: abort
Empty file.
1 change: 1 addition & 0 deletions config/configtimeout/testdata/invalid_policy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
short_timeout_policy: hope
1 change: 1 addition & 0 deletions config/configtimeout/testdata/invalid_timeout.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
timeout: -1s
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 {
// Negative timeouts are not acceptable, since all sends will fail.
if ts.Timeout < 0 {
return errors.New("'timeout' must be non-negative")
}
return nil
}

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

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

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

package configtimeout

import (
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap/confmaptest"
)

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 TestDefaultConfig(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "default.yaml"))
require.NoError(t, err)
cfg := NewDefaultConfig()
require.NoError(t, cm.Unmarshal(&cfg))
assert.Equal(t,
TimeoutConfig{
Timeout: 5 * time.Second,
ShortTimeoutPolicy: "",
}, cfg)
}

func TestDefaulPolicy(t *testing.T) {
// The legacy behavior of the exporterhelper is to sustain a short timeout.
assert.Equal(t, PolicySustain, PolicyDefault)
}

func TestUnmarshalConfig(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)
cfg := NewDefaultConfig()
require.NoError(t, cm.Unmarshal(&cfg))
assert.Equal(t,
TimeoutConfig{
Timeout: 17 * time.Second,
ShortTimeoutPolicy: "abort",
}, cfg)
}

func TestUnmarshalInvalidConfig(t *testing.T) {
type test struct {
file string
expect string
}
for _, tt := range []test{
{
file: "invalid_timeout.yaml",
expect: "'timeout' must be non-negative",
},
{
file: "invalid_policy.yaml",
expect: "unsupported 'short_timeout_policy' hope",
},
} {
t.Run(tt.file, func(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", tt.file))
require.NoError(t, err)
cfg := NewDefaultConfig()
assert.NoError(t, cm.Unmarshal(&cfg))
assert.ErrorContains(t, component.ValidateConfig(cfg), tt.expect)
})
}
}
3 changes: 3 additions & 0 deletions exporter/debugexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/collector/config/configretry v1.17.0 // indirect
go.opentelemetry.io/collector/config/configtimeout v0.111.0 // indirect
go.opentelemetry.io/collector/consumer/consumerprofiles v0.111.0 // indirect
go.opentelemetry.io/collector/consumer/consumertest v0.111.0 // indirect
go.opentelemetry.io/collector/exporter/exporterprofiles v0.111.0 // indirect
Expand Down Expand Up @@ -93,3 +94,5 @@ replace go.opentelemetry.io/collector/exporter/exporterprofiles => ../exporterpr
replace go.opentelemetry.io/collector/pipeline => ../../pipeline

replace go.opentelemetry.io/collector/internal/globalsignal => ../../internal/globalsignal

replace go.opentelemetry.io/collector/config/configtimeout => ../../config/configtimeout
Loading
Loading