Skip to content

Commit

Permalink
[chore] [exporter/prometheusremotewrite] Use NewDefaultClientConfig i…
Browse files Browse the repository at this point in the history
…nstead of manually creating struct (open-telemetry#35542)

**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 jmichalek132 committed Oct 10, 2024
1 parent 8921f03 commit b6ff1d4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 53 deletions.
37 changes: 19 additions & 18 deletions exporter/prometheusremotewriteexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ func TestLoadConfig(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)

clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = "localhost:8888"
clientConfig.TLSSetting = configtls.ClientConfig{
Config: configtls.Config{
CAFile: "/var/lib/mycert.pem", // This is subject to change, but currently I have no idea what else to put here lol
},
Insecure: false,
}
clientConfig.ReadBufferSize = 0
clientConfig.WriteBufferSize = 512 * 1024
clientConfig.Timeout = 5 * time.Second
clientConfig.Headers = map[string]configopaque.String{
"Prometheus-Remote-Write-Version": "0.1.0",
"X-Scope-OrgID": "234",
}
tests := []struct {
id component.ID
expected component.Config
Expand Down Expand Up @@ -56,24 +71,10 @@ func TestLoadConfig(t *testing.T) {
QueueSize: 2000,
NumConsumers: 10,
},
AddMetricSuffixes: false,
Namespace: "test-space",
ExternalLabels: map[string]string{"key1": "value1", "key2": "value2"},
ClientConfig: confighttp.ClientConfig{
Endpoint: "localhost:8888",
TLSSetting: configtls.ClientConfig{
Config: configtls.Config{
CAFile: "/var/lib/mycert.pem", // This is subject to change, but currently I have no idea what else to put here lol
},
Insecure: false,
},
ReadBufferSize: 0,
WriteBufferSize: 512 * 1024,
Timeout: 5 * time.Second,
Headers: map[string]configopaque.String{
"Prometheus-Remote-Write-Version": "0.1.0",
"X-Scope-OrgID": "234"},
},
AddMetricSuffixes: false,
Namespace: "test-space",
ExternalLabels: map[string]string{"key1": "value1", "key2": "value2"},
ClientConfig: clientConfig,
ResourceToTelemetrySettings: resourcetotelemetry.Settings{Enabled: true},
TargetInfo: &TargetInfo{
Enabled: true,
Expand Down
53 changes: 28 additions & 25 deletions exporter/prometheusremotewriteexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Test_NewPRWExporter(t *testing.T) {
BackOffConfig: configretry.BackOffConfig{},
Namespace: "",
ExternalLabels: map[string]string{},
ClientConfig: confighttp.ClientConfig{Endpoint: ""},
ClientConfig: confighttp.NewDefaultClientConfig(),
TargetInfo: &TargetInfo{
Enabled: true,
},
Expand Down Expand Up @@ -155,6 +155,21 @@ func Test_Start(t *testing.T) {
}
set := exportertest.NewNopSettings()
set.BuildInfo = buildInfo

clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = "https://some.url:9411/api/prom/push"
clientConfigTLS := confighttp.NewDefaultClientConfig()
clientConfigTLS.Endpoint = "https://some.url:9411/api/prom/push"
clientConfigTLS.TLSSetting = configtls.ClientConfig{
Config: configtls.Config{
CAFile: "non-existent file",
CertFile: "",
KeyFile: "",
},
Insecure: false,
ServerName: "",
}

tests := []struct {
name string
config *Config
Expand All @@ -173,7 +188,7 @@ func Test_Start(t *testing.T) {
concurrency: 5,
externalLabels: map[string]string{"Key1": "Val1"},
set: set,
clientSettings: confighttp.ClientConfig{Endpoint: "https://some.url:9411/api/prom/push"},
clientSettings: clientConfig,
},
{
name: "invalid_tls",
Expand All @@ -183,18 +198,7 @@ func Test_Start(t *testing.T) {
externalLabels: map[string]string{"Key1": "Val1"},
set: set,
returnErrorOnStartUp: true,
clientSettings: confighttp.ClientConfig{
Endpoint: "https://some.url:9411/api/prom/push",
TLSSetting: configtls.ClientConfig{
Config: configtls.Config{
CAFile: "non-existent file",
CertFile: "",
KeyFile: "",
},
Insecure: false,
ServerName: "",
},
},
clientSettings: clientConfigTLS,
},
}

Expand Down Expand Up @@ -705,14 +709,13 @@ func Test_PushMetrics(t *testing.T) {
MaxInterval: 1 * time.Second, // Shorter max interval
MaxElapsedTime: 2 * time.Second, // Shorter max elapsed time
}
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = server.URL
clientConfig.ReadBufferSize = 0
clientConfig.WriteBufferSize = 512 * 1024
cfg := &Config{
Namespace: "",
ClientConfig: confighttp.ClientConfig{
Endpoint: server.URL,
// We almost read 0 bytes, so no need to tune ReadBufferSize.
ReadBufferSize: 0,
WriteBufferSize: 512 * 1024,
},
Namespace: "",
ClientConfig: clientConfig,
MaxBatchSizeBytes: 3000000,
RemoteWriteQueue: RemoteWriteQueue{NumConsumers: 1},
TargetInfo: &TargetInfo{
Expand Down Expand Up @@ -938,11 +941,11 @@ func TestWALOnExporterRoundTrip(t *testing.T) {
// 2. Create the WAL configuration, create the
// exporter and export some time series!
tempDir := t.TempDir()
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = prweServer.URL
cfg := &Config{
Namespace: "test_ns",
ClientConfig: confighttp.ClientConfig{
Endpoint: prweServer.URL,
},
Namespace: "test_ns",
ClientConfig: clientConfig,
RemoteWriteQueue: RemoteWriteQueue{NumConsumers: 1},
WAL: &WALConfig{
Directory: tempDir,
Expand Down
16 changes: 7 additions & 9 deletions exporter/prometheusremotewriteexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,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 Down Expand Up @@ -81,6 +80,12 @@ func createMetricsExporter(ctx context.Context, set exporter.Settings,
func createDefaultConfig() component.Config {
retrySettings := configretry.NewDefaultBackOffConfig()
retrySettings.InitialInterval = 50 * time.Millisecond
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = "http://some.url:9411/api/prom/push"
// We almost read 0 bytes, so no need to tune ReadBufferSize.
clientConfig.ReadBufferSize = 0
clientConfig.WriteBufferSize = 512 * 1024
clientConfig.Timeout = exporterhelper.NewDefaultTimeoutConfig().Timeout

return &Config{
Namespace: "",
Expand All @@ -90,14 +95,7 @@ func createDefaultConfig() component.Config {
BackOffConfig: retrySettings,
AddMetricSuffixes: true,
SendMetadata: false,
ClientConfig: confighttp.ClientConfig{
Endpoint: "http://some.url:9411/api/prom/push",
// We almost read 0 bytes, so no need to tune ReadBufferSize.
ReadBufferSize: 0,
WriteBufferSize: 512 * 1024,
Timeout: exporterhelper.NewDefaultTimeoutConfig().Timeout,
Headers: map[string]configopaque.String{},
},
ClientConfig: clientConfig,
// TODO(jbd): Adjust the default queue size.
RemoteWriteQueue: RemoteWriteQueue{
Enabled: true,
Expand Down
2 changes: 1 addition & 1 deletion exporter/prometheusremotewriteexporter/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Test_createDefaultConfig(t *testing.T) {
func Test_createMetricsExporter(t *testing.T) {

invalidConfig := createDefaultConfig().(*Config)
invalidConfig.ClientConfig = confighttp.ClientConfig{}
invalidConfig.ClientConfig = confighttp.NewDefaultClientConfig()
invalidTLSConfig := createDefaultConfig().(*Config)
invalidTLSConfig.ClientConfig.TLSSetting = configtls.ClientConfig{
Config: configtls.Config{
Expand Down

0 comments on commit b6ff1d4

Please sign in to comment.