Skip to content

Commit

Permalink
Add customroundtripper to httpclientconfig (open-telemetry#2085)
Browse files Browse the repository at this point in the history
* added optional custom round tripper

* fixed comment

* returned error in roundtripper function
  • Loading branch information
Aman Brar committed Nov 9, 2020
1 parent 5f60c72 commit a0497ee
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
10 changes: 10 additions & 0 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ type HTTPClientSettings struct {
// Additional headers attached to each HTTP request sent by the client.
// Existing header values are overwritten if collision happens.
Headers map[string]string `mapstructure:"headers,omitempty"`

// Custom Round Tripper to allow for individual components to intercept HTTP requests
CustomRoundTripper func(next http.RoundTripper) (http.RoundTripper, error)
}

func (hcs *HTTPClientSettings) ToClient() (*http.Client, error) {
Expand All @@ -71,6 +74,13 @@ func (hcs *HTTPClientSettings) ToClient() (*http.Client, error) {
}
}

if hcs.CustomRoundTripper != nil {
clientTransport, err = hcs.CustomRoundTripper(clientTransport)
if err != nil {
return nil, err
}
}

return &http.Client{
Transport: clientTransport,
Timeout: hcs.Timeout,
Expand Down
56 changes: 45 additions & 11 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package confighttp

import (
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -31,19 +32,52 @@ import (
)

func TestAllHTTPClientSettings(t *testing.T) {
hcs := &HTTPClientSettings{
Endpoint: "localhost:1234",
TLSSetting: configtls.TLSClientSetting{
Insecure: false,
tests := []struct {
name string
settings HTTPClientSettings
shouldError bool
}{
{
name: "all_valid_settings",
settings: HTTPClientSettings{
Endpoint: "localhost:1234",
TLSSetting: configtls.TLSClientSetting{
Insecure: false,
},
ReadBufferSize: 1024,
WriteBufferSize: 512,
CustomRoundTripper: func(next http.RoundTripper) (http.RoundTripper, error) { return next, nil },
},
shouldError: false,
},
{
name: "error_round_tripper_returned",
settings: HTTPClientSettings{
Endpoint: "localhost:1234",
TLSSetting: configtls.TLSClientSetting{
Insecure: false,
},
ReadBufferSize: 1024,
WriteBufferSize: 512,
CustomRoundTripper: func(next http.RoundTripper) (http.RoundTripper, error) { return nil, errors.New("error") },
},
shouldError: true,
},
ReadBufferSize: 1024,
WriteBufferSize: 512,
}
client, err := hcs.ToClient()
assert.NoError(t, err)
transport := client.Transport.(*http.Transport)
assert.EqualValues(t, 1024, transport.ReadBufferSize)
assert.EqualValues(t, 512, transport.WriteBufferSize)

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
client, err := test.settings.ToClient()
if test.shouldError {
assert.Error(t, err)
return
}
assert.NoError(t, err)
transport := client.Transport.(*http.Transport)
assert.EqualValues(t, 1024, transport.ReadBufferSize)
assert.EqualValues(t, 512, transport.WriteBufferSize)
})
}
}

func TestHTTPClientSettingsError(t *testing.T) {
Expand Down

0 comments on commit a0497ee

Please sign in to comment.