Skip to content

Commit

Permalink
[chore] [exporter/sumologic] Use NewDefaultClientConfig instead of ma…
Browse files Browse the repository at this point in the history
…nually creating struct (open-telemetry#35547)

**Description:**
This PR makes usage of `NewDefaultClientConfig` instead of manually
creating the confighttp.ClientConfig struct.

**Link to tracking Issue:** open-telemetry#35457
  • Loading branch information
mackjmr authored and AkhigbeEromo committed Oct 9, 2024
1 parent c068b61 commit c64a75e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 47 deletions.
12 changes: 6 additions & 6 deletions exporter/sumologicexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ type Config struct {

// createDefaultClientConfig returns default http client settings
func createDefaultClientConfig() confighttp.ClientConfig {
return confighttp.ClientConfig{
Timeout: defaultTimeout,
Compression: DefaultCompressEncoding,
Auth: &configauth.Authentication{
AuthenticatorID: component.NewID(sumologicextension.NewFactory().Type()),
},
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Timeout = defaultTimeout
clientConfig.Compression = DefaultCompressEncoding
clientConfig.Auth = &configauth.Authentication{
AuthenticatorID: component.NewID(sumologicextension.NewFactory().Type()),
}
return clientConfig
}

func (cfg *Config) Validate() error {
Expand Down
45 changes: 20 additions & 25 deletions exporter/sumologicexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ import (
)

func TestInitExporterInvalidConfiguration(t *testing.T) {
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = "test_endpoint"
clientConfig.Timeout = defaultTimeout

clientConfigGzip := confighttp.NewDefaultClientConfig()
clientConfigGzip.Endpoint = "test_endpoint"
clientConfigGzip.Timeout = defaultTimeout
clientConfigGzip.Compression = "gzip"

testcases := []struct {
name string
cfg *Config
Expand All @@ -25,10 +34,7 @@ func TestInitExporterInvalidConfiguration(t *testing.T) {
cfg: &Config{
LogFormat: "test_format",
MetricFormat: "otlp",
ClientConfig: confighttp.ClientConfig{
Timeout: defaultTimeout,
Endpoint: "test_endpoint",
},
ClientConfig: clientConfig,
},
},
{
Expand All @@ -37,11 +43,7 @@ func TestInitExporterInvalidConfiguration(t *testing.T) {
cfg: &Config{
LogFormat: "json",
MetricFormat: "test_format",
ClientConfig: confighttp.ClientConfig{
Timeout: defaultTimeout,
Endpoint: "test_endpoint",
Compression: "gzip",
},
ClientConfig: clientConfigGzip,
},
},
{
Expand All @@ -50,11 +52,7 @@ func TestInitExporterInvalidConfiguration(t *testing.T) {
cfg: &Config{
LogFormat: "json",
MetricFormat: "carbon2",
ClientConfig: confighttp.ClientConfig{
Timeout: defaultTimeout,
Endpoint: "test_endpoint",
Compression: "gzip",
},
ClientConfig: clientConfigGzip,
},
},
{
Expand All @@ -63,11 +61,7 @@ func TestInitExporterInvalidConfiguration(t *testing.T) {
cfg: &Config{
LogFormat: "json",
MetricFormat: "graphite",
ClientConfig: confighttp.ClientConfig{
Timeout: defaultTimeout,
Endpoint: "test_endpoint",
Compression: "gzip",
},
ClientConfig: clientConfigGzip,
},
},
}
Expand All @@ -87,6 +81,11 @@ func TestInitExporterInvalidConfiguration(t *testing.T) {
}

func TestConfigInvalidTimeout(t *testing.T) {
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Timeout = 56 * time.Second

clientConfigZeroTimeout := confighttp.NewDefaultClientConfig()
clientConfigZeroTimeout.Timeout = 0 * time.Second
testcases := []struct {
name string
expectedError error
Expand All @@ -96,18 +95,14 @@ func TestConfigInvalidTimeout(t *testing.T) {
name: "over the limit timeout",
expectedError: errors.New("timeout must be between 1 and 55 seconds, got 56s"),
cfg: &Config{
ClientConfig: confighttp.ClientConfig{
Timeout: 56 * time.Second,
},
ClientConfig: clientConfig,
},
},
{
name: "less than 1 timeout",
expectedError: errors.New("timeout must be between 1 and 55 seconds, got 0s"),
cfg: &Config{
ClientConfig: confighttp.ClientConfig{
Timeout: 0 * time.Second,
},
ClientConfig: clientConfigZeroTimeout,
},
},
}
Expand Down
16 changes: 8 additions & 8 deletions exporter/sumologicexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,15 @@ func TestPartiallyFailed(t *testing.T) {
}

func TestInvalidHTTPCLient(t *testing.T) {
exp, err := initExporter(&Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: "test_endpoint",
TLSSetting: configtls.ClientConfig{
Config: configtls.Config{
MinVersion: "invalid",
},
},
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = "test_endpoint"
clientConfig.TLSSetting = configtls.ClientConfig{
Config: configtls.Config{
MinVersion: "invalid",
},
}
exp, err := initExporter(&Config{
ClientConfig: clientConfig,
}, exportertest.NewNopSettings())
require.NoError(t, err)

Expand Down
15 changes: 7 additions & 8 deletions exporter/sumologicexporter/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,19 @@ func TestCreateDefaultConfig(t *testing.T) {
cfg := factory.CreateDefaultConfig()
qs := exporterhelper.NewDefaultQueueConfig()
qs.Enabled = false

clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Timeout = 30 * time.Second
clientConfig.Compression = "gzip"
clientConfig.Auth = &configauth.Authentication{
AuthenticatorID: component.NewID(metadata.Type),
}
assert.Equal(t, &Config{
MaxRequestBodySize: 1_048_576,
LogFormat: "otlp",
MetricFormat: "otlp",
Client: "otelcol",

ClientConfig: confighttp.ClientConfig{
Timeout: 30 * time.Second,
Compression: "gzip",
Auth: &configauth.Authentication{
AuthenticatorID: component.NewID(metadata.Type),
},
},
ClientConfig: clientConfig,
BackOffConfig: configretry.NewDefaultBackOffConfig(),
QueueSettings: qs,
}, cfg)
Expand Down

0 comments on commit c64a75e

Please sign in to comment.