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

New Relic exporter updates. #4278

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
62 changes: 31 additions & 31 deletions exporter/newrelicexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
package newrelicexporter

import (
"time"

"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configparser"
"go.opentelemetry.io/collector/exporter/exporterhelper"
)

// EndpointConfig defines configuration for a single endpoint in the New Relic exporter.
Expand All @@ -31,10 +31,14 @@ type EndpointConfig struct {
// HostOverride overrides the endpoint.
HostOverride string `mapstructure:"host_override"`

// Timeout is the total amount of time spent attempting a request,
// including retries, before abandoning and dropping data. Default is 15
// TimeoutSettings is the total amount of time spent attempting a request,
// including retries, before abandoning and dropping data. Default is 5
// seconds.
Timeout time.Duration `mapstructure:"timeout"`
TimeoutSettings exporterhelper.TimeoutSettings `mapstructure:",squash"`

// RetrySettings defines configuration for retrying batches in case of export failure.
// The current supported strategy is exponential backoff.
RetrySettings exporterhelper.RetrySettings `mapstructure:"retry"`

// Insecure disables TLS on the endpoint.
insecure bool
Expand All @@ -57,36 +61,32 @@ type Config struct {
LogsConfig EndpointConfig `mapstructure:"logs"`
}

// GetTracesConfig merges the common configuration section with the traces specific section.
func (c Config) GetTracesConfig() EndpointConfig {
return mergeConfig(c.CommonConfig, c.TracesConfig)
}

// GetMetricsConfig merges the common configuration section with the metrics specific section.
func (c Config) GetMetricsConfig() EndpointConfig {
return mergeConfig(c.CommonConfig, c.MetricsConfig)
}

// GetLogsConfig merges the common configuration section with the logs specific section.
func (c Config) GetLogsConfig() EndpointConfig {
return mergeConfig(c.CommonConfig, c.LogsConfig)
}

func mergeConfig(baseConfig EndpointConfig, config EndpointConfig) EndpointConfig {
if config.APIKey == "" {
config.APIKey = baseConfig.APIKey
func (c *Config) Unmarshal(componentSection *configparser.Parser) error {
if err := componentSection.UnmarshalExact(c); err != nil {
return err
}

if config.APIKeyHeader == "" {
config.APIKeyHeader = baseConfig.APIKeyHeader
baseEndpointConfig := c.CommonConfig
endpointConfigs := map[string]*EndpointConfig{
"metrics": &c.MetricsConfig,
"traces": &c.TracesConfig,
"logs": &c.LogsConfig,
}

if config.HostOverride == "" {
config.HostOverride = baseConfig.HostOverride
}
for section, sectionCfg := range endpointConfigs {

// Default to whatever the common config is
*sectionCfg = baseEndpointConfig

p, err := componentSection.Sub(section)
if err != nil {
return err
}

if config.Timeout == 0 {
config.Timeout = baseConfig.Timeout
// Overlay with the section specific configuration
if err := p.UnmarshalExact(sectionCfg); err != nil {
return err
}
}
return config
return nil
}
142 changes: 22 additions & 120 deletions exporter/newrelicexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configtest"
"go.opentelemetry.io/collector/exporter/exporterhelper"
)

func TestLoadConfig(t *testing.T) {
Expand All @@ -41,141 +42,42 @@ func TestLoadConfig(t *testing.T) {

r0 := cfg.Exporters[config.NewID(typeStr)]
defaultConfig := factory.CreateDefaultConfig().(*Config)

// The marshaller should set the endpoint specific configuration to the common config by default
defaultConfig.TracesConfig = defaultConfig.CommonConfig
defaultConfig.LogsConfig = defaultConfig.CommonConfig
defaultConfig.MetricsConfig = defaultConfig.CommonConfig
assert.Equal(t, r0, defaultConfig)

r1 := cfg.Exporters[config.NewIDWithName(typeStr, "alt")].(*Config)
assert.Equal(t, r1, &Config{
ExporterSettings: config.NewExporterSettings(config.NewIDWithName(typeStr, "alt")),
CommonConfig: EndpointConfig{
APIKey: "a1b2c3d4",
Timeout: time.Second * 30,
APIKey: "a1b2c3d4",
TimeoutSettings: exporterhelper.TimeoutSettings{
Timeout: 30 * time.Second,
},
},
MetricsConfig: EndpointConfig{
APIKey: "a1b2c3d4",
TimeoutSettings: exporterhelper.TimeoutSettings{
Timeout: 30 * time.Second,
},
HostOverride: "alt.metrics.newrelic.com",
insecure: false,
},
TracesConfig: EndpointConfig{
APIKey: "a1b2c3d4",
TimeoutSettings: exporterhelper.TimeoutSettings{
Timeout: 30 * time.Second,
},
HostOverride: "alt.spans.newrelic.com",
insecure: false,
},
LogsConfig: EndpointConfig{
APIKey: "a1b2c3d4",
TimeoutSettings: exporterhelper.TimeoutSettings{
Timeout: 30 * time.Second,
},
HostOverride: "alt.logs.newrelic.com",
insecure: false,
},
})
}

func TestEndpointSpecificConfigTakesPrecedence(t *testing.T) {
cfg := Config{
CommonConfig: EndpointConfig{
APIKey: "commonapikey",
APIKeyHeader: "commonapikeyheader",
HostOverride: "commonhost",
Timeout: time.Second * 10,
},
TracesConfig: EndpointConfig{
APIKey: "tracesapikey",
APIKeyHeader: "tracesapikeyheader",
HostOverride: "traceshost",
Timeout: time.Second * 20,
},
MetricsConfig: EndpointConfig{
APIKey: "metricsapikey",
APIKeyHeader: "metricsapikeyheader",
HostOverride: "metricshost",
Timeout: time.Second * 30,
},
LogsConfig: EndpointConfig{
APIKey: "logsapikey",
APIKeyHeader: "logsapikeyheader",
HostOverride: "logshost",
Timeout: time.Second * 40,
},
}

assert.Equal(t, cfg.TracesConfig, cfg.GetTracesConfig())
assert.Equal(t, cfg.MetricsConfig, cfg.GetMetricsConfig())
assert.Equal(t, cfg.LogsConfig, cfg.GetLogsConfig())
}

func TestEndpointSpecificConfigUsedWhenDefined(t *testing.T) {
cfg := Config{
CommonConfig: EndpointConfig{
APIKey: "commonapikey",
APIKeyHeader: "commonapikeyheader",
HostOverride: "commonhost",
Timeout: time.Second * 10,
},
TracesConfig: EndpointConfig{
APIKey: "tracesapikey",
HostOverride: "traceshost",
Timeout: time.Second * 20,
},
MetricsConfig: EndpointConfig{
APIKeyHeader: "metricsapikeyheader",
HostOverride: "metricshost",
Timeout: time.Second * 30,
},
LogsConfig: EndpointConfig{
APIKey: "logsapikey",
APIKeyHeader: "logsapikeyheader",
HostOverride: "logshost",
},
}

expectedTraceConfig := EndpointConfig{
APIKey: "tracesapikey",
APIKeyHeader: "commonapikeyheader",
HostOverride: "traceshost",
Timeout: time.Second * 20,
}
expectedMetricConfig := EndpointConfig{
APIKey: "commonapikey",
APIKeyHeader: "metricsapikeyheader",
HostOverride: "metricshost",
Timeout: time.Second * 30,
}
expectedLogConfig := EndpointConfig{
APIKey: "logsapikey",
APIKeyHeader: "logsapikeyheader",
HostOverride: "logshost",
Timeout: time.Second * 10,
}

assert.Equal(t, expectedTraceConfig, cfg.GetTracesConfig())
assert.Equal(t, expectedMetricConfig, cfg.GetMetricsConfig())
assert.Equal(t, expectedLogConfig, cfg.GetLogsConfig())
}

func TestCommonConfigValuesUsed(t *testing.T) {
cfg := Config{
CommonConfig: EndpointConfig{
APIKey: "commonapikey",
APIKeyHeader: "commonapikeyheader",
HostOverride: "commonhost",
Timeout: time.Second * 10,
},
TracesConfig: EndpointConfig{
APIKey: "",
APIKeyHeader: "",
HostOverride: "",
Timeout: 0,
},
MetricsConfig: EndpointConfig{
APIKey: "",
APIKeyHeader: "",
HostOverride: "",
Timeout: 0,
},
LogsConfig: EndpointConfig{
APIKey: "",
APIKeyHeader: "",
HostOverride: "",
Timeout: 0,
},
}

assert.Equal(t, cfg.CommonConfig, cfg.GetTracesConfig())
assert.Equal(t, cfg.CommonConfig, cfg.GetMetricsConfig())
assert.Equal(t, cfg.CommonConfig, cfg.GetLogsConfig())
}
Loading