@@ -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
3130const (
@@ -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.
170177func (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 {
0 commit comments