diff --git a/.chloggen/confighttp-customroundtripper.yaml b/.chloggen/confighttp-customroundtripper.yaml new file mode 100644 index 00000000000..79076c0733d --- /dev/null +++ b/.chloggen/confighttp-customroundtripper.yaml @@ -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: deprecation + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: confighttp + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Deprecate `ClientConfig.CustomRoundTripper` + +# One or more tracking issues or pull requests related to the change +issues: [8627] + +# (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: Set the `Transport` field on the `*http.Client` object returned from `(ClientConfig).ToClient` instead. + +# 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] diff --git a/config/confighttp/confighttp.go b/config/confighttp/confighttp.go index f89f8c6ea0f..c0d434aa66a 100644 --- a/config/confighttp/confighttp.go +++ b/config/confighttp/confighttp.go @@ -58,7 +58,10 @@ type ClientConfig struct { Headers map[string]configopaque.String `mapstructure:"headers"` // Custom Round Tripper to allow for individual components to intercept HTTP requests - CustomRoundTripper func(next http.RoundTripper) (http.RoundTripper, error) + // + // Deprecated: [v0.103.0] Set (*http.Client).Transport on the *http.Client returned from ToClient + // to configure this. + CustomRoundTripper func(next http.RoundTripper) (http.RoundTripper, error) `mapstructure:"-"` // Auth configuration for outgoing HTTP calls. Auth *configauth.Authentication `mapstructure:"auth"` @@ -221,13 +224,6 @@ func (hcs *ClientConfig) ToClient(ctx context.Context, host component.Host, sett clientTransport = otelhttp.NewTransport(clientTransport, otelOpts...) } - if hcs.CustomRoundTripper != nil { - clientTransport, err = hcs.CustomRoundTripper(clientTransport) - if err != nil { - return nil, err - } - } - return &http.Client{ Transport: clientTransport, Timeout: hcs.Timeout, diff --git a/config/confighttp/confighttp_test.go b/config/confighttp/confighttp_test.go index 99ac9a51201..c7c45924783 100644 --- a/config/confighttp/confighttp_test.go +++ b/config/confighttp/confighttp_test.go @@ -81,7 +81,6 @@ func TestAllHTTPClientSettings(t *testing.T) { MaxIdleConnsPerHost: &maxIdleConnsPerHost, MaxConnsPerHost: &maxConnsPerHost, IdleConnTimeout: &idleConnTimeout, - CustomRoundTripper: func(next http.RoundTripper) (http.RoundTripper, error) { return next, nil }, Compression: "", DisableKeepAlives: true, HTTP2ReadIdleTimeout: idleConnTimeout, @@ -102,7 +101,6 @@ func TestAllHTTPClientSettings(t *testing.T) { MaxIdleConnsPerHost: &maxIdleConnsPerHost, MaxConnsPerHost: &maxConnsPerHost, IdleConnTimeout: &idleConnTimeout, - CustomRoundTripper: func(next http.RoundTripper) (http.RoundTripper, error) { return next, nil }, Compression: "none", DisableKeepAlives: true, HTTP2ReadIdleTimeout: idleConnTimeout, @@ -123,7 +121,6 @@ func TestAllHTTPClientSettings(t *testing.T) { MaxIdleConnsPerHost: &maxIdleConnsPerHost, MaxConnsPerHost: &maxConnsPerHost, IdleConnTimeout: &idleConnTimeout, - CustomRoundTripper: func(next http.RoundTripper) (http.RoundTripper, error) { return next, nil }, Compression: "gzip", DisableKeepAlives: true, HTTP2ReadIdleTimeout: idleConnTimeout, @@ -144,7 +141,6 @@ func TestAllHTTPClientSettings(t *testing.T) { MaxIdleConnsPerHost: &maxIdleConnsPerHost, MaxConnsPerHost: &maxConnsPerHost, IdleConnTimeout: &idleConnTimeout, - CustomRoundTripper: func(next http.RoundTripper) (http.RoundTripper, error) { return next, nil }, Compression: "gzip", DisableKeepAlives: true, HTTP2ReadIdleTimeout: idleConnTimeout, @@ -152,19 +148,6 @@ func TestAllHTTPClientSettings(t *testing.T) { }, shouldError: false, }, - { - name: "error_round_tripper_returned", - settings: ClientConfig{ - Endpoint: "localhost:1234", - TLSSetting: configtls.ClientConfig{ - Insecure: false, - }, - ReadBufferSize: 1024, - WriteBufferSize: 512, - CustomRoundTripper: func(http.RoundTripper) (http.RoundTripper, error) { return nil, errors.New("error") }, - }, - shouldError: true, - }, } for _, test := range tests { @@ -212,9 +195,8 @@ func TestPartialHTTPClientSettings(t *testing.T) { TLSSetting: configtls.ClientConfig{ Insecure: false, }, - ReadBufferSize: 1024, - WriteBufferSize: 512, - CustomRoundTripper: func(next http.RoundTripper) (http.RoundTripper, error) { return next, nil }, + ReadBufferSize: 1024, + WriteBufferSize: 512, }, shouldError: false, }, @@ -728,15 +710,14 @@ func TestHttpReception(t *testing.T) { Endpoint: prefix + ln.Addr().String(), TLSSetting: *tt.tlsClientCreds, } + + client, errClient := hcs.ToClient(context.Background(), componenttest.NewNopHost(), component.TelemetrySettings{}) + require.NoError(t, errClient) + if tt.forceHTTP1 { expectedProto = "HTTP/1.1" - hcs.CustomRoundTripper = func(rt http.RoundTripper) (http.RoundTripper, error) { - rt.(*http.Transport).ForceAttemptHTTP2 = false - return rt, nil - } + client.Transport.(*http.Transport).ForceAttemptHTTP2 = false } - client, errClient := hcs.ToClient(context.Background(), componenttest.NewNopHost(), component.TelemetrySettings{}) - require.NoError(t, errClient) resp, errResp := client.Get(hcs.Endpoint) if tt.hasError { @@ -1479,23 +1460,23 @@ func BenchmarkHttpRequest(b *testing.B) { Endpoint: "https://" + ln.Addr().String(), TLSSetting: *tlsClientCreds, } - if bb.forceHTTP1 { - hcs.CustomRoundTripper = func(rt http.RoundTripper) (http.RoundTripper, error) { - rt.(*http.Transport).ForceAttemptHTTP2 = false - return rt, nil - } - } + b.Run(bb.name, func(b *testing.B) { var c *http.Client if !bb.clientPerThread { c, err = hcs.ToClient(context.Background(), componenttest.NewNopHost(), component.TelemetrySettings{}) require.NoError(b, err) + } b.RunParallel(func(pb *testing.PB) { if c == nil { c, err = hcs.ToClient(context.Background(), componenttest.NewNopHost(), component.TelemetrySettings{}) require.NoError(b, err) } + if bb.forceHTTP1 { + c.Transport.(*http.Transport).ForceAttemptHTTP2 = false + } + for pb.Next() { resp, errResp := c.Get(hcs.Endpoint) require.NoError(b, errResp)