Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] [exporter/loki] Use NewDefaultClientConfig instead of manually creating struct #35526

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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