Skip to content

Commit

Permalink
[chore] [exporter/logzio] Use NewDefaultClientConfig instead of manua…
Browse files Browse the repository at this point in the history
…lly creating struct (#35524)

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

**Link to tracking Issue:** #35457
  • Loading branch information
mackjmr authored Oct 9, 2024
1 parent 256e465 commit 003780f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 75 deletions.
59 changes: 22 additions & 37 deletions exporter/logzioexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configcompression"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configopaque"
"go.opentelemetry.io/collector/config/configretry"
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.opentelemetry.io/collector/exporter/exporterhelper"
Expand All @@ -40,15 +39,11 @@ func TestLoadConfig(t *testing.T) {
expected.BackOffConfig.MaxInterval = 5 * time.Second
expected.QueueSettings = exporterhelper.NewDefaultQueueConfig()
expected.QueueSettings.Enabled = false
expected.ClientConfig = confighttp.ClientConfig{
Endpoint: "",
Timeout: 30 * time.Second,
Headers: map[string]configopaque.String{},
// Default to gzip compression
Compression: configcompression.TypeGzip,
// We almost read 0 bytes, so no need to tune ReadBufferSize.
WriteBufferSize: 512 * 1024,
}
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Timeout = 30 * time.Second
clientConfig.Compression = configcompression.TypeGzip
clientConfig.WriteBufferSize = 512 * 1024
expected.ClientConfig = clientConfig
assert.Equal(t, expected, cfg)
}

Expand All @@ -67,19 +62,19 @@ func TestDefaultLoadConfig(t *testing.T) {
}
expected.BackOffConfig = configretry.NewDefaultBackOffConfig()
expected.QueueSettings = exporterhelper.NewDefaultQueueConfig()
expected.ClientConfig = confighttp.ClientConfig{
Endpoint: "",
Timeout: 30 * time.Second,
Headers: map[string]configopaque.String{},
// Default to gzip compression
Compression: configcompression.TypeGzip,
// We almost read 0 bytes, so no need to tune ReadBufferSize.
WriteBufferSize: 512 * 1024,
}
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Timeout = 30 * time.Second
clientConfig.Compression = configcompression.TypeGzip
clientConfig.WriteBufferSize = 512 * 1024
expected.ClientConfig = clientConfig
assert.Equal(t, expected, cfg)
}

func TestCheckAndWarnDeprecatedOptions(t *testing.T) {
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Timeout = 10 * time.Second
clientConfig.Compression = configcompression.TypeGzip
clientConfig.WriteBufferSize = 512 * 1024
// Config with legacy options
actualCfg := &Config{
QueueSettings: exporterhelper.NewDefaultQueueConfig(),
Expand All @@ -88,15 +83,7 @@ func TestCheckAndWarnDeprecatedOptions(t *testing.T) {
CustomEndpoint: "https://api.example.com",
QueueMaxLength: 10,
DrainInterval: 10,
ClientConfig: confighttp.ClientConfig{
Endpoint: "",
Timeout: 10 * time.Second,
Headers: map[string]configopaque.String{},
// Default to gzip compression
Compression: configcompression.TypeGzip,
// We almost read 0 bytes, so no need to tune ReadBufferSize.
WriteBufferSize: 512 * 1024,
},
ClientConfig: clientConfig,
}
params := exportertest.NewNopSettings()
logger := hclog2ZapLogger{
Expand All @@ -105,22 +92,20 @@ func TestCheckAndWarnDeprecatedOptions(t *testing.T) {
}
actualCfg.checkAndWarnDeprecatedOptions(&logger)

clientConfigEndpoint := confighttp.NewDefaultClientConfig()
clientConfigEndpoint.Timeout = 10 * time.Second
clientConfigEndpoint.Compression = configcompression.TypeGzip
clientConfigEndpoint.WriteBufferSize = 512 * 1024
clientConfigEndpoint.Endpoint = "https://api.example.com"

expected := &Config{
Token: "logzioTESTtoken",
CustomEndpoint: "https://api.example.com",
QueueMaxLength: 10,
DrainInterval: 10,
BackOffConfig: configretry.NewDefaultBackOffConfig(),
QueueSettings: exporterhelper.NewDefaultQueueConfig(),
ClientConfig: confighttp.ClientConfig{
Endpoint: "https://api.example.com",
Timeout: 10 * time.Second,
Headers: map[string]configopaque.String{},
// Default to gzip compression
Compression: configcompression.TypeGzip,
// We almost read 0 bytes, so no need to tune ReadBufferSize.
WriteBufferSize: 512 * 1024,
},
ClientConfig: clientConfigEndpoint,
}
expected.QueueSettings.QueueSize = 10
assert.Equal(t, expected, actualCfg)
Expand Down
35 changes: 18 additions & 17 deletions exporter/logzioexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,13 @@ func TestExportErrors(tester *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
rw.WriteHeader(test.status)
}))
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = server.URL

cfg := &Config{
Region: "",
Token: "token",
ClientConfig: confighttp.ClientConfig{
Endpoint: server.URL,
},
Region: "",
Token: "token",
ClientConfig: clientConfig,
}
td := newTestTracesWithAttributes()
ld := testdata.GenerateLogs(10)
Expand Down Expand Up @@ -240,13 +241,13 @@ func TestPushTraceData(tester *testing.T) {
recordedRequests, _ = io.ReadAll(req.Body)
rw.WriteHeader(http.StatusOK)
}))
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = server.URL
clientConfig.Compression = configcompression.TypeGzip
cfg := Config{
Token: "token",
Region: "",
ClientConfig: confighttp.ClientConfig{
Endpoint: server.URL,
Compression: configcompression.TypeGzip,
},
Token: "token",
Region: "",
ClientConfig: clientConfig,
}
defer server.Close()
td := newTestTraces()
Expand All @@ -273,13 +274,13 @@ func TestPushLogsData(tester *testing.T) {
recordedRequests, _ = io.ReadAll(req.Body)
rw.WriteHeader(http.StatusOK)
}))
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = server.URL
clientConfig.Compression = configcompression.TypeGzip
cfg := Config{
Token: "token",
Region: "",
ClientConfig: confighttp.ClientConfig{
Endpoint: server.URL,
Compression: configcompression.TypeGzip,
},
Token: "token",
Region: "",
ClientConfig: clientConfig,
}
defer server.Close()
ld := generateLogsOneEmptyTimestamp()
Expand Down
17 changes: 7 additions & 10 deletions exporter/logzioexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configcompression"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configopaque"
"go.opentelemetry.io/collector/config/configretry"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/exporter/exporterhelper"
Expand All @@ -34,20 +33,18 @@ func NewFactory() exporter.Factory {
}

func createDefaultConfig() component.Config {
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Timeout = 30 * time.Second
// Default to gzip compression
clientConfig.Compression = configcompression.TypeGzip
// We almost read 0 bytes, so no need to tune ReadBufferSize.
clientConfig.WriteBufferSize = 512 * 1024
return &Config{
Region: "",
Token: "",
BackOffConfig: configretry.NewDefaultBackOffConfig(),
QueueSettings: exporterhelper.NewDefaultQueueConfig(),
ClientConfig: confighttp.ClientConfig{
Endpoint: "",
Timeout: 30 * time.Second,
Headers: map[string]configopaque.String{},
// Default to gzip compression
Compression: configcompression.TypeGzip,
// We almost read 0 bytes, so no need to tune ReadBufferSize.
WriteBufferSize: 512 * 1024,
},
ClientConfig: clientConfig,
}
}

Expand Down
10 changes: 5 additions & 5 deletions exporter/logzioexporter/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ func TestGenerateUrl(t *testing.T) {
{"", "EU", "https://listener-eu.logz.io:8071/?token=token"},
}
for _, test := range generateURLTests {
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = test.endpoint
cfg := &Config{
Region: test.region,
Token: "token",
ClientConfig: confighttp.ClientConfig{
Endpoint: test.endpoint,
},
Region: test.region,
Token: "token",
ClientConfig: clientConfig,
}
output, _ := generateEndpoint(cfg)
require.Equal(t, test.expected, output)
Expand Down
12 changes: 6 additions & 6 deletions exporter/logzioexporter/jsonlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ func TestSetTimeStamp(t *testing.T) {
}))
defer func() { server.Close() }()
ld := generateLogsOneEmptyTimestamp()
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = server.URL
clientConfig.Compression = configcompression.TypeGzip
cfg := &Config{
Region: "us",
Token: "token",
ClientConfig: confighttp.ClientConfig{
Endpoint: server.URL,
Compression: configcompression.TypeGzip,
},
Region: "us",
Token: "token",
ClientConfig: clientConfig,
}
var err error
params := exportertest.NewNopSettings()
Expand Down

0 comments on commit 003780f

Please sign in to comment.