Skip to content

Commit d9e656b

Browse files
committed
Keep both config styles
1 parent 28ad218 commit d9e656b

24 files changed

Lines changed: 390 additions & 1010 deletions

.chloggen/feat_confighttp_config-optional-api.yaml

Lines changed: 0 additions & 31 deletions
This file was deleted.

.chloggen/feat_confighttp_config-optional.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ subtext: |
3232
# Include 'user' if the change is relevant to end users.
3333
# Include 'api' if there is a change to a library API.
3434
# Default: '[user]'
35-
change_logs: [user]
35+
change_logs: [user, api]

config/confighttp/client.go

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"go.opentelemetry.io/collector/config/configopaque"
2626
"go.opentelemetry.io/collector/config/configoptional"
2727
"go.opentelemetry.io/collector/config/configtls"
28-
"go.opentelemetry.io/collector/confmap"
2928
)
3029

3130
const (
@@ -98,13 +97,14 @@ type ClientConfig struct {
9897
// Keepalive configuration.
9998
Keepalive configoptional.Optional[KeepaliveClientConfig] `mapstructure:"keepalive,omitempty"`
10099

101-
// unmarshalWarnings holds deprecation notices collected during Unmarshal and
102-
// emitted as log warnings on the first call to ToClient.
103-
unmarshalWarnings []renamedField `mapstructure:"-"`
104-
}
105-
106-
func (cc *ClientConfig) Unmarshal(conf *confmap.Conf) error {
107-
return unmarshalClientDeprecated(cc, conf)
100+
// Deprecated: use Keepalive.IdleConnTimeout instead.
101+
IdleConnTimeout time.Duration `mapstructure:"idle_conn_timeout,omitempty"`
102+
// Deprecated: use Keepalive.MaxIdleConns instead.
103+
MaxIdleConns int `mapstructure:"max_idle_conns,omitempty"`
104+
// Deprecated: use Keepalive.MaxIdleConnsPerHost instead.
105+
MaxIdleConnsPerHost int `mapstructure:"max_idle_conns_per_host,omitempty"`
106+
// Deprecated: set Keepalive to None to disable keep-alives.
107+
DisableKeepAlives bool `mapstructure:"disable_keep_alives,omitempty"`
108108
}
109109

110110
// CookiesConfig defines the configuration of the HTTP client regarding cookies served by the server.
@@ -138,7 +138,7 @@ func NewDefaultClientConfig() ClientConfig {
138138
defaultTransport := http.DefaultTransport.(*http.Transport)
139139

140140
return ClientConfig{
141-
Keepalive: configoptional.Some(KeepaliveClientConfig{
141+
Keepalive: configoptional.Default(KeepaliveClientConfig{
142142
MaxIdleConns: defaultTransport.MaxIdleConns,
143143
IdleConnTimeout: defaultTransport.IdleConnTimeout,
144144
}),
@@ -152,9 +152,16 @@ func (cc *ClientConfig) Validate() error {
152152
return err
153153
}
154154
}
155+
if cc.Keepalive.HasValue() && cc.hasDeprecatedKeepaliveFields() {
156+
return errors.New("confighttp.ClientConfig: cannot use deprecated keepalive fields (idle_conn_timeout, max_idle_conns, max_idle_conns_per_host, disable_keep_alives) alongside the 'keepalive' section; migrate to the 'keepalive' section")
157+
}
155158
return nil
156159
}
157160

161+
func (cc *ClientConfig) hasDeprecatedKeepaliveFields() bool {
162+
return cc.DisableKeepAlives || cc.IdleConnTimeout != 0 || cc.MaxIdleConns != 0 || cc.MaxIdleConnsPerHost != 0
163+
}
164+
158165
// ToClientOption is an option to change the behavior of the HTTP client
159166
// returned by ClientConfig.ToClient().
160167
// There are currently no available options.
@@ -168,8 +175,17 @@ type ToClientOption interface {
168175
// the `extensions` argument should be the output of `host.GetExtensions()`.
169176
// It may also be `nil` in tests where no such extension is expected to be used.
170177
func (cc *ClientConfig) ToClient(ctx context.Context, extensions map[component.ID]component.Component, settings component.TelemetrySettings, _ ...ToClientOption) (*http.Client, error) {
171-
for _, field := range cc.unmarshalWarnings {
172-
field.Log(settings.Logger)
178+
if cc.DisableKeepAlives {
179+
settings.Logger.Warn("'disable_keep_alives' is deprecated; set keepalive to null (keepalive: null) to disable keep-alives")
180+
}
181+
if cc.IdleConnTimeout != 0 {
182+
settings.Logger.Warn("'idle_conn_timeout' is deprecated; use 'keepalive.idle_conn_timeout' instead")
183+
}
184+
if cc.MaxIdleConns != 0 {
185+
settings.Logger.Warn("'max_idle_conns' is deprecated; use 'keepalive.max_idle_conns' instead")
186+
}
187+
if cc.MaxIdleConnsPerHost != 0 {
188+
settings.Logger.Warn("'max_idle_conns_per_host' is deprecated; use 'keepalive.max_idle_conns_per_host' instead")
173189
}
174190

175191
tlsCfg, err := cc.TLS.LoadTLSConfig(ctx)
@@ -187,11 +203,27 @@ func (cc *ClientConfig) ToClient(ctx context.Context, extensions map[component.I
187203
transport.WriteBufferSize = cc.WriteBufferSize
188204
}
189205

190-
if kaCfg := cc.Keepalive.Get(); cc.Keepalive.HasValue() {
206+
// Convert deprecated fields into the canonical Keepalive optional so the
207+
// application logic below only has to deal with one struct.
208+
if cc.DisableKeepAlives {
209+
cc.Keepalive = configoptional.None[KeepaliveClientConfig]()
210+
} else if cc.hasDeprecatedKeepaliveFields() {
211+
cc.Keepalive = configoptional.Some(KeepaliveClientConfig{
212+
IdleConnTimeout: cc.IdleConnTimeout,
213+
MaxIdleConns: cc.MaxIdleConns,
214+
MaxIdleConnsPerHost: cc.MaxIdleConnsPerHost,
215+
})
216+
}
217+
218+
if cc.Keepalive.IsNone() {
219+
transport.DisableKeepAlives = true
220+
} else {
221+
kaCfg := cc.Keepalive.GetOrInsertDefault()
191222
transport.MaxIdleConns = kaCfg.MaxIdleConns
192223
transport.MaxIdleConnsPerHost = kaCfg.MaxIdleConnsPerHost
193224
transport.IdleConnTimeout = kaCfg.IdleConnTimeout
194225
}
226+
// else Default: transport.Clone() already carries the right defaults (MaxIdleConns=100, IdleConnTimeout=90s).
195227
transport.MaxConnsPerHost = cc.MaxConnsPerHost
196228
transport.ForceAttemptHTTP2 = cc.ForceAttemptHTTP2
197229
// Setting the Proxy URL
@@ -203,8 +235,6 @@ func (cc *ClientConfig) ToClient(ctx context.Context, extensions map[component.I
203235
transport.Proxy = http.ProxyURL(proxyURL)
204236
}
205237

206-
transport.DisableKeepAlives = !cc.Keepalive.HasValue()
207-
208238
if cc.HTTP2ReadIdleTimeout > 0 {
209239
transport2, transportErr := http2.ConfigureTransports(transport)
210240
if transportErr != nil {

config/confighttp/client_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,18 @@ func TestPartialHTTPClientSettings(t *testing.T) {
238238
}
239239

240240
func TestDefaultHTTPClientSettings(t *testing.T) {
241-
httpClientSettings := NewDefaultClientConfig()
242-
assert.Equal(t, 100, httpClientSettings.Keepalive.Get().MaxIdleConns)
243-
assert.Equal(t, 90*time.Second, httpClientSettings.Keepalive.Get().IdleConnTimeout)
241+
settings := componenttest.NewNopTelemetrySettings()
242+
settings.MeterProvider = nil
243+
settings.TracerProvider = nil
244+
245+
cfg := NewDefaultClientConfig()
246+
client, err := cfg.ToClient(context.Background(), nil, settings)
247+
require.NoError(t, err)
248+
transport := client.Transport.(*http.Transport)
249+
// Defaults come from http.DefaultTransport.
250+
assert.Equal(t, 100, transport.MaxIdleConns)
251+
assert.Equal(t, 90*time.Second, transport.IdleConnTimeout)
252+
assert.False(t, transport.DisableKeepAlives)
244253
}
245254

246255
func TestProxyURL(t *testing.T) {

config/confighttp/deprecated.go

Lines changed: 0 additions & 176 deletions
This file was deleted.

0 commit comments

Comments
 (0)