-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add config settings for Http client (#1186)
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
- Loading branch information
1 parent
bd886e8
commit c8b812b
Showing
12 changed files
with
169 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package confighttp | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
"go.opentelemetry.io/collector/config/configtls" | ||
) | ||
|
||
type HTTPClientSettings struct { | ||
// The target URL to send data to (e.g.: http://some.url:9411/v1/trace). | ||
Endpoint string `mapstructure:"endpoint"` | ||
|
||
// TLSSetting struct exposes TLS client configuration. | ||
TLSSetting configtls.TLSClientSetting `mapstructure:",squash"` | ||
|
||
// Timeout parameter configures `http.Client.Timeout`. | ||
Timeout time.Duration `mapstructure:"timeout,omitempty"` | ||
} | ||
|
||
func (hcs *HTTPClientSettings) ToClient() (*http.Client, error) { | ||
tlsCfg, err := hcs.TLSSetting.LoadTLSConfig() | ||
if err != nil { | ||
return nil, err | ||
} | ||
transport := http.DefaultTransport.(*http.Transport).Clone() | ||
if tlsCfg != nil { | ||
transport.TLSClientConfig = tlsCfg | ||
} | ||
return &http.Client{ | ||
Transport: transport, | ||
Timeout: hcs.Timeout, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package confighttp | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"go.opentelemetry.io/collector/config/configtls" | ||
) | ||
|
||
func TestInvalidPemFile(t *testing.T) { | ||
tests := []struct { | ||
settings HTTPClientSettings | ||
err string | ||
}{ | ||
{ | ||
err: "^failed to load TLS config: failed to load CA CertPool: failed to load CA /doesnt/exist:", | ||
settings: HTTPClientSettings{ | ||
Endpoint: "", | ||
TLSSetting: configtls.TLSClientSetting{ | ||
TLSSetting: configtls.TLSSetting{ | ||
CAFile: "/doesnt/exist", | ||
}, | ||
Insecure: false, | ||
ServerName: "", | ||
}, | ||
}, | ||
}, | ||
{ | ||
err: "^failed to load TLS config: for auth via TLS, either both certificate and key must be supplied, or neither", | ||
settings: HTTPClientSettings{ | ||
Endpoint: "", | ||
TLSSetting: configtls.TLSClientSetting{ | ||
TLSSetting: configtls.TLSSetting{ | ||
CertFile: "/doesnt/exist", | ||
}, | ||
Insecure: false, | ||
ServerName: "", | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.err, func(t *testing.T) { | ||
_, err := test.settings.ToClient() | ||
assert.Regexp(t, test.err, err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.