Skip to content

[signalfxexporter] Deprecate config translation rules in favor of metrics transform processor #18218

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

Merged
merged 1 commit into from
Feb 1, 2023
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
11 changes: 11 additions & 0 deletions .chloggen/deprecaterules.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: deprecation

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: signalfxexporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Deprecate config `translation_rules` in favor of metrics transform processor

# One or more tracking issues related to the change
issues: [18218]
63 changes: 40 additions & 23 deletions exporter/signalfxexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.uber.org/zap"
"gopkg.in/yaml.v3"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/correlation"
Expand All @@ -34,9 +35,26 @@ import (

const (
translationRulesConfigKey = "translation_rules"
excludeMetricsConfigKey = "exclude_metrics"
)

var defaultTranslationRules = func() []translation.Rule {
cfg, err := loadConfig([]byte(translation.DefaultTranslationRulesYaml))
// It is safe to panic since this is deterministic, and will not fail anywhere else if it doesn't fail all the time.
if err != nil {
panic(err)
}
return cfg.TranslationRules
}()

var defaultExcludeMetrics = func() []dpfilters.MetricFilter {
cfg, err := loadConfig([]byte(translation.DefaultExcludeMetricsYaml))
// It is safe to panic since this is deterministic, and will not fail anywhere else if it doesn't fail all the time.
if err != nil {
panic(err)
}
return cfg.ExcludeMetrics
}()

var _ confmap.Unmarshaler = (*Config)(nil)

// Config defines configuration for SignalFx exporter.
Expand Down Expand Up @@ -79,8 +97,11 @@ type Config struct {

// TranslationRules defines a set of rules how to translate metrics to a SignalFx compatible format
// Rules defined in translation/constants.go are used by default.
// Deprecated: Use metricstransform processor to do metrics transformations.
TranslationRules []translation.Rule `mapstructure:"translation_rules"`

DisableDefaultTranslationRules bool `mapstructure:"disable_default_translation_rules"`

// DeltaTranslationTTL specifies in seconds the max duration to keep the most recent datapoint for any
// `delta_metric` specified in TranslationRules. Default is 3600s.
DeltaTranslationTTL int64 `mapstructure:"delta_translation_ttl"`
Expand Down Expand Up @@ -117,8 +138,23 @@ type Config struct {
MaxConnections int `mapstructure:"max_connections"`
}

func (cfg *Config) getMetricTranslator() (*translation.MetricTranslator, error) {
metricTranslator, err := translation.NewMetricTranslator(cfg.TranslationRules, cfg.DeltaTranslationTTL)
func (cfg *Config) getMetricTranslator(logger *zap.Logger) (*translation.MetricTranslator, error) {
rules := defaultTranslationRules
if cfg.TranslationRules != nil {
// Previous way to disable default translation rules.
if len(cfg.TranslationRules) == 0 {
logger.Warn("You are using the deprecated `translation_rules` option that will be removed soon; Use `disable_default_translation_rules` to disable the default rules in a gateway mode.")
rules = []translation.Rule{}
} else {
logger.Warn("You are using the deprecated `translation_rules` option that will be removed soon; Use metricstransform processor instead.")
rules = cfg.TranslationRules
}
}
// The new way to disable default translation rules. This override any setting of the default TranslationRules.
if cfg.DisableDefaultTranslationRules {
rules = []translation.Rule{}
}
metricTranslator, err := translation.NewMetricTranslator(rules, cfg.DeltaTranslationTTL)
if err != nil {
return nil, fmt.Errorf("invalid \"%s\": %w", translationRulesConfigKey, err)
}
Expand Down Expand Up @@ -162,15 +198,6 @@ func (cfg *Config) Unmarshal(componentParser *confmap.Conf) error {
return err
}

// If translations_config is not set in the config, set it to the default.
if !componentParser.IsSet(translationRulesConfigKey) {
var err error
cfg.TranslationRules, err = loadDefaultTranslationRules()
if err != nil {
return err
}
}

return setDefaultExcludes(cfg)
}

Expand All @@ -197,23 +224,13 @@ func (cfg *Config) Validate() error {
}

func setDefaultExcludes(cfg *Config) error {
exCfg, err := loadConfig([]byte(translation.DefaultExcludeMetricsYaml))
if err != nil {
return err
}

// If ExcludeMetrics is not set to empty, append defaults.
if cfg.ExcludeMetrics == nil || len(cfg.ExcludeMetrics) > 0 {
cfg.ExcludeMetrics = append(cfg.ExcludeMetrics, exCfg.ExcludeMetrics...)
cfg.ExcludeMetrics = append(cfg.ExcludeMetrics, defaultExcludeMetrics...)
}
return nil
}

func loadDefaultTranslationRules() ([]translation.Rule, error) {
cfg, err := loadConfig([]byte(translation.DefaultTranslationRulesYaml))
return cfg.TranslationRules, err
}

func loadConfig(bytes []byte) (Config, error) {
var cfg Config
var data map[string]interface{}
Expand Down
55 changes: 45 additions & 10 deletions exporter/signalfxexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/correlation"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/signalfxexporter/internal/translation"
Expand All @@ -46,9 +47,7 @@ func TestLoadConfig(t *testing.T) {
defaultCfg := createDefaultConfig().(*Config)
defaultCfg.AccessToken = "testToken"
defaultCfg.Realm = "ap0"
defaultTranslationRules, err := loadDefaultTranslationRules()
require.NoError(t, err)
defaultCfg.TranslationRules = defaultTranslationRules

seventy := 70

Expand Down Expand Up @@ -203,23 +202,59 @@ func TestLoadConfig(t *testing.T) {
}

func TestConfigGetMetricTranslator(t *testing.T) {
emptyTranslator := func() *translation.MetricTranslator {
translator, err := translation.NewMetricTranslator(nil, 3600)
require.NoError(t, err)
return translator
}
tests := []struct {
name string
cfg *Config
want *translation.MetricTranslator
wantErr bool
}{
{
name: "Test empty translator",
name: "Test empty config",
cfg: &Config{
DeltaTranslationTTL: 3600,
},
want: func() *translation.MetricTranslator {
translator, err := translation.NewMetricTranslator(defaultTranslationRules, 3600)
require.NoError(t, err)
return translator
}(),
},
{
name: "Test empty rules",
cfg: &Config{
TranslationRules: []translation.Rule{},
DeltaTranslationTTL: 3600,
},
want: emptyTranslator(),
want: func() *translation.MetricTranslator {
translator, err := translation.NewMetricTranslator([]translation.Rule{}, 3600)
require.NoError(t, err)
return translator
}(),
},
{
name: "Test disable rules",
cfg: &Config{
DisableDefaultTranslationRules: true,
DeltaTranslationTTL: 3600,
},
want: func() *translation.MetricTranslator {
translator, err := translation.NewMetricTranslator([]translation.Rule{}, 3600)
require.NoError(t, err)
return translator
}(),
},
{
name: "Test disable rules overrides rules",
cfg: &Config{
TranslationRules: []translation.Rule{{Action: translation.ActionDropDimensions}},
DisableDefaultTranslationRules: true,
DeltaTranslationTTL: 3600,
},
want: func() *translation.MetricTranslator {
translator, err := translation.NewMetricTranslator([]translation.Rule{}, 3600)
require.NoError(t, err)
return translator
}(),
},
{
name: "Test invalid translation rules",
Expand All @@ -238,7 +273,7 @@ func TestConfigGetMetricTranslator(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.cfg.getMetricTranslator()
got, err := tt.cfg.getMetricTranslator(zap.NewNop())
if tt.wantErr {
assert.Error(t, err)
return
Expand Down
2 changes: 1 addition & 1 deletion exporter/signalfxexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func newSignalFxExporter(
return nil, errors.New("nil config")
}

metricTranslator, err := config.getMetricTranslator()
metricTranslator, err := config.getMetricTranslator(createSettings.TelemetrySettings.Logger)
if err != nil {
return nil, err
}
Expand Down
9 changes: 3 additions & 6 deletions exporter/signalfxexporter/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ func TestCreateMetricsExporter_CustomConfig(t *testing.T) {
}

func TestDefaultTranslationRules(t *testing.T) {
rules, err := loadDefaultTranslationRules()
require.NoError(t, err)
rules := defaultTranslationRules
require.NotNil(t, rules, "rules are nil")
tr, err := translation.NewMetricTranslator(rules, 1)
require.NoError(t, err)
Expand Down Expand Up @@ -448,8 +447,7 @@ func TestDefaultDiskTranslations(t *testing.T) {
}

func testGetTranslator(t *testing.T) *translation.MetricTranslator {
rules, err := loadDefaultTranslationRules()
require.NoError(t, err)
rules := defaultTranslationRules
require.NotNil(t, rules, "rules are nil")
tr, err := translation.NewMetricTranslator(rules, 3600)
require.NoError(t, err)
Expand Down Expand Up @@ -589,8 +587,7 @@ func TestDefaultExcludes_not_translated(t *testing.T) {

// Benchmark test for default translation rules on an example hostmetrics dataset.
func BenchmarkMetricConversion(b *testing.B) {
rules, err := loadDefaultTranslationRules()
require.NoError(b, err)
rules := defaultTranslationRules
require.NotNil(b, rules, "rules are nil")
tr, err := translation.NewMetricTranslator(rules, 1)
require.NoError(b, err)
Expand Down