Skip to content

Commit

Permalink
[chore] [exporter/loki] Use NewDefaultClientConfig instead of manuall…
Browse files Browse the repository at this point in the history
…y creating struct (open-telemetry#35526)

**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 shivanthzen committed Dec 4, 2024
1 parent 8c65233 commit 7a9c74d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 51 deletions.
40 changes: 20 additions & 20 deletions exporter/lokiexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ import (
)

func TestLoadConfigNewExporter(t *testing.T) {
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Headers = map[string]configopaque.String{
"X-Custom-Header": "loki_rocks",
}
clientConfig.Endpoint = "https://loki:3100/loki/api/v1/push"
clientConfig.TLSSetting = configtls.ClientConfig{
Config: configtls.Config{
CAFile: "/var/lib/mycert.pem",
CertFile: "certfile",
KeyFile: "keyfile",
},
Insecure: true,
}
clientConfig.ReadBufferSize = 123
clientConfig.WriteBufferSize = 345
clientConfig.Timeout = time.Second * 10
t.Parallel()

cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
Expand All @@ -36,23 +52,7 @@ func TestLoadConfigNewExporter(t *testing.T) {
{
id: component.NewIDWithName(metadata.Type, "allsettings"),
expected: &Config{
ClientConfig: confighttp.ClientConfig{
Headers: map[string]configopaque.String{
"X-Custom-Header": "loki_rocks",
},
Endpoint: "https://loki:3100/loki/api/v1/push",
TLSSetting: configtls.ClientConfig{
Config: configtls.Config{
CAFile: "/var/lib/mycert.pem",
CertFile: "certfile",
KeyFile: "keyfile",
},
Insecure: true,
},
ReadBufferSize: 123,
WriteBufferSize: 345,
Timeout: time.Second * 10,
},
ClientConfig: clientConfig,
BackOffConfig: configretry.BackOffConfig{
Enabled: true,
InitialInterval: 10 * time.Second,
Expand Down Expand Up @@ -92,6 +92,8 @@ func TestLoadConfigNewExporter(t *testing.T) {
}

func TestConfigValidate(t *testing.T) {
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = "https://loki.example.com"
testCases := []struct {
desc string
cfg *Config
Expand All @@ -110,9 +112,7 @@ func TestConfigValidate(t *testing.T) {
{
desc: "Config is valid",
cfg: &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: "https://loki.example.com",
},
ClientConfig: clientConfig,
},
err: nil,
},
Expand Down
13 changes: 7 additions & 6 deletions exporter/lokiexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ func TestPushLogData(t *testing.T) {
}))
defer ts.Close()

clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = ts.URL
cfg := &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: ts.URL,
},
ClientConfig: clientConfig,
}

f := NewFactory()
Expand Down Expand Up @@ -254,10 +254,11 @@ func TestLogsToLokiRequestWithGroupingByTenant(t *testing.T) {
}))
defer ts.Close()

clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = ts.URL

cfg := &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: ts.URL,
},
ClientConfig: clientConfig,
}

f := NewFactory()
Expand Down
13 changes: 5 additions & 8 deletions exporter/lokiexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"go.opentelemetry.io/collector/component"
"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 @@ -29,14 +28,12 @@ func NewFactory() exporter.Factory {
}

func createDefaultConfig() component.Config {
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Timeout = 30 * time.Second
// We almost read 0 bytes, so no need to tune ReadBufferSize.
clientConfig.WriteBufferSize = 512 * 1024
return &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: "",
Timeout: 30 * time.Second,
Headers: map[string]configopaque.String{},
// We almost read 0 bytes, so no need to tune ReadBufferSize.
WriteBufferSize: 512 * 1024,
},
ClientConfig: clientConfig,
BackOffConfig: configretry.NewDefaultBackOffConfig(),
QueueSettings: exporterhelper.NewDefaultQueueConfig(),
DefaultLabelsEnabled: map[string]bool{
Expand Down
38 changes: 21 additions & 17 deletions exporter/lokiexporter/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ const (
)

func TestExporter_new(t *testing.T) {
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = validEndpoint

t.Run("with valid config", func(t *testing.T) {
config := &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: validEndpoint,
},
ClientConfig: clientConfig,
}
exp, err := newExporter(config, componenttest.NewNopTelemetrySettings())
require.NoError(t, err)
Expand All @@ -31,10 +32,11 @@ func TestExporter_new(t *testing.T) {
}

func TestExporter_startReturnsNillWhenValidConfig(t *testing.T) {
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = validEndpoint

config := &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: validEndpoint,
},
ClientConfig: clientConfig,
}
exp, err := newExporter(config, componenttest.NewNopTelemetrySettings())
require.NoError(t, err)
Expand All @@ -43,27 +45,29 @@ func TestExporter_startReturnsNillWhenValidConfig(t *testing.T) {
}

func TestExporter_startReturnsErrorWhenInvalidHttpClientSettings(t *testing.T) {
config := &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: "",
TLSSetting: configtls.ClientConfig{
Config: configtls.Config{
MinVersion: "invalid",
},
},
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = ""
clientConfig.TLSSetting = configtls.ClientConfig{
Config: configtls.Config{
MinVersion: "invalid",
},
}

config := &Config{
ClientConfig: clientConfig,
}
exp, err := newExporter(config, componenttest.NewNopTelemetrySettings())
require.NoError(t, err)
require.NotNil(t, exp)
require.Error(t, exp.start(context.Background(), componenttest.NewNopHost()))
}

func TestExporter_stopAlwaysReturnsNil(t *testing.T) {
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = validEndpoint

config := &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: validEndpoint,
},
ClientConfig: clientConfig,
}
exp, err := newExporter(config, componenttest.NewNopTelemetrySettings())
require.NoError(t, err)
Expand Down

0 comments on commit 7a9c74d

Please sign in to comment.