Skip to content

Commit

Permalink
wip: different way of handling combinations of options
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeWinikates committed Oct 17, 2023
1 parent 4a32f84 commit aa67ed8
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 29 deletions.
33 changes: 20 additions & 13 deletions senders/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ type configuration struct {

// interval (in seconds) at which to flush data to Wavefront. defaults to 1 Second.
// together with batch size controls the max theoretical throughput of the sender.
FlushInterval time.Duration
SDKMetricsTags map[string]string
Path string
Authentication interface{}
HTTPClient *http.Client
FlushInterval time.Duration
SDKMetricsTags map[string]string
Path string
Authentication interface{}
httpClientConfiguration *httpClientConfiguration
HTTPClient *http.Client
}

func (c *configuration) Direct() bool {
Expand All @@ -60,13 +61,14 @@ func (c *configuration) Direct() bool {

func createConfig(wfURL string, setters ...Option) (*configuration, error) {
cfg := &configuration{
MetricsPort: defaultMetricsPort,
TracesPort: defaultTracesPort,
BatchSize: defaultBatchSize,
MaxBufferSize: defaultBufferSize,
FlushInterval: defaultFlushInterval,
SendInternalMetrics: true,
SDKMetricsTags: map[string]string{},
MetricsPort: defaultMetricsPort,
TracesPort: defaultTracesPort,
BatchSize: defaultBatchSize,
MaxBufferSize: defaultBufferSize,
FlushInterval: defaultFlushInterval,
SendInternalMetrics: true,
SDKMetricsTags: map[string]string{},
httpClientConfiguration: &httpClientConfiguration{Timeout: defaultTimeout},
}

u, err := url.Parse(wfURL)
Expand Down Expand Up @@ -116,7 +118,12 @@ func createConfig(wfURL string, setters ...Option) (*configuration, error) {
cfg.Server = u.String()

if cfg.HTTPClient == nil {
cfg.HTTPClient = &http.Client{Timeout: defaultTimeout}
cfg.HTTPClient = &http.Client{
Timeout: cfg.httpClientConfiguration.Timeout,
Transport: &http.Transport{
TLSClientConfig: cfg.httpClientConfiguration.TLSClientConfig,
},
}
}

return cfg, nil
Expand Down
8 changes: 8 additions & 0 deletions senders/example_newsender_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package senders_test

import (
"crypto/tls"
"net/http"
"time"

wavefront "github.com/wavefronthq/wavefront-sdk-go/senders"
Expand All @@ -18,6 +19,13 @@ func ExampleNewSender_options() {
wavefront.Timeout(15), // Set an HTTP timeout in seconds (default is 10s)
wavefront.SendInternalMetrics(false), // Don't send internal ~sdk.go.* metrics
wavefront.TLSConfigOptions(&tls.Config{}), // Set TLS config options.
wavefront.HTTPClient(&http.Client{
Timeout: 15 * time.Second,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{},
IdleConnTimeout: 4 * time.Second,
},
}), // Provide a fully configured http.Client
)

if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion senders/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package senders

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -39,7 +40,7 @@ func TestTLSEndToEnd(t *testing.T) {
testServer.httpServer.Client()
tlsConfig := testServer.TLSConfig()

sender, err := NewSender(testServer.URL, TLSConfigOptions(tlsConfig))
sender, err := NewSender(testServer.URL, TLSConfigOptions(tlsConfig), Timeout(10*time.Second))
require.NoError(t, err)
require.NoError(t, sender.SendMetric("my metric", 20, 0, "localhost", nil))
require.NoError(t, sender.Flush())
Expand Down
2 changes: 1 addition & 1 deletion senders/new_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestDefaults(t *testing.T) {
assert.Equal(t, 2878, cfg.MetricsPort)
assert.Equal(t, 30001, cfg.TracesPort)
assert.Equal(t, 10*time.Second, cfg.HTTPClient.Timeout)
assert.Nil(t, cfg.HTTPClient.Transport)
assert.Nil(t, cfg.HTTPClient.Transport.(*http.Transport).TLSClientConfig)
}

func TestBatchSize(t *testing.T) {
Expand Down
25 changes: 11 additions & 14 deletions senders/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
// Option Wavefront client configuration options
type Option func(*configuration)

func mutuallyExclusiveOptionWarning(optionName string) string {
return optionName + ` will overwrite the previously set HTTPClient option (HTTPClient, TLSConfigOptions, or Timeout)`
type httpClientConfiguration struct {
Timeout time.Duration
TLSClientConfig *tls.Config
}

// APIToken configures the sender to use a Wavefront API Token for authentication
Expand Down Expand Up @@ -121,22 +122,21 @@ func TracesPort(port int) Option {
}
}

// Timeout sets the HTTP timeout (in seconds). Defaults to 10 seconds.
// Timeout sets the HTTP timeout. Defaults to 10 seconds.
func Timeout(timeout time.Duration) Option {
return func(cfg *configuration) {
if cfg.HTTPClient != nil {
log.Println(mutuallyExclusiveOptionWarning("Timeout"))
log.Println("using Timeout after setting the HTTPClient is not supported." +
"If you are using the HTTPClient Option, set Timeout on the HTTPClient directly")
}
cfg.HTTPClient = &http.Client{Timeout: timeout}
cfg.httpClientConfiguration.Timeout = timeout
}
}

// HTTPClient sets the http.Client used to send data to Wavefront.
// Overrides TLSConfigOptions and Timeout.
func HTTPClient(client *http.Client) Option {
return func(cfg *configuration) {
if cfg.HTTPClient != nil {
log.Println(mutuallyExclusiveOptionWarning("HTTPClient"))
}
cfg.HTTPClient = client
}
}
Expand All @@ -146,13 +146,10 @@ func TLSConfigOptions(tlsCfg *tls.Config) Option {
tlsCfgCopy := tlsCfg.Clone()
return func(cfg *configuration) {
if cfg.HTTPClient != nil {
log.Println(mutuallyExclusiveOptionWarning("TLSConfigOptions"))
}
cfg.HTTPClient = &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsCfgCopy,
},
log.Println("using TLSConfigOptions after setting the HTTPClient is not supported." +
"If you are using the HTTPClient Option, set TLSClientConfig on the HTTPClient directly")
}
cfg.httpClientConfiguration.TLSClientConfig = tlsCfgCopy
}
}

Expand Down

0 comments on commit aa67ed8

Please sign in to comment.