From a6c8d2d8898058f7522088eb7520562dfd7bbb11 Mon Sep 17 00:00:00 2001 From: Jeffrey Chien Date: Fri, 20 Oct 2023 13:55:26 -0400 Subject: [PATCH] Move EMF flags to config. --- exporter/awsemfexporter/config.go | 18 ++++++ exporter/awsemfexporter/config_test.go | 67 +++++++++++++++++++ exporter/awsemfexporter/emf_exporter.go | 21 +----- exporter/awsemfexporter/emf_exporter_test.go | 68 -------------------- 4 files changed, 87 insertions(+), 87 deletions(-) diff --git a/exporter/awsemfexporter/config.go b/exporter/awsemfexporter/config.go index a632721bec4e..649f996013df 100644 --- a/exporter/awsemfexporter/config.go +++ b/exporter/awsemfexporter/config.go @@ -4,6 +4,8 @@ package awsemfexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsemfexporter" import ( + "strings" + "go.opentelemetry.io/collector/component" "go.uber.org/zap" @@ -142,7 +144,23 @@ func (config *Config) Validate() error { } return cwlogs.ValidateTagsInput(config.Tags) +} + +func (config *Config) IsEnhancedContainerInsights() bool { + return false // temporarily disable, also need to rename _config to config + // return config.EnhancedContainerInsights && !config.DisableMetricExtraction +} + +func (config *Config) IsPulseApmEnabled() bool { + if config.LogGroupName == "" || config.Namespace == "" { + return false + } + + if config.Namespace == pulseMetricNamespace && strings.HasPrefix(config.LogGroupName, pulseLogGroupNamePrefix) { + return true + } + return false } func newEMFSupportedUnits() map[string]interface{} { diff --git a/exporter/awsemfexporter/config_test.go b/exporter/awsemfexporter/config_test.go index 9f66cc0aec4d..3d5056d5ab2b 100644 --- a/exporter/awsemfexporter/config_test.go +++ b/exporter/awsemfexporter/config_test.go @@ -314,3 +314,70 @@ func TestNoDimensionRollupFeatureGate(t *testing.T) { assert.Equal(t, cfg.(*Config).DimensionRollupOption, "NoDimensionRollup") _ = featuregate.GlobalRegistry().Set("awsemf.nodimrollupdefault", false) } + +func TestIsEnhancedContainerInsights(t *testing.T) { + factory := NewFactory() + cfg := factory.CreateDefaultConfig().(*Config) + cfg.EnhancedContainerInsights = true + cfg.DisableMetricExtraction = false + assert.False(t, cfg.IsEnhancedContainerInsights()) + cfg.EnhancedContainerInsights = false + assert.False(t, cfg.IsEnhancedContainerInsights()) + cfg.EnhancedContainerInsights = true + cfg.DisableMetricExtraction = true + assert.False(t, cfg.IsEnhancedContainerInsights()) +} + +func TestIsPulseApmEnabled(t *testing.T) { + tests := []struct { + name string + metricNameSpace string + logGroupName string + expectedResult bool + }{ + { + "validPulseEMF", + "AWS/APM", + "/aws/apm/eks", + true, + }, + { + "invalidPulseLogsGroup", + "AWS/APM", + "/nonaws/apm/eks", + false, + }, + { + "invalidPulseMetricNamespace", + "NonAWS/APM", + "/aws/apm/eks", + false, + }, + { + "invalidPulseEMF", + "NonAWS/APM", + "/nonaws/apm/eks", + false, + }, + { + "defaultConfig", + "", + "", + false, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + factory := NewFactory() + cfg := factory.CreateDefaultConfig().(*Config) + if len(tc.metricNameSpace) > 0 { + cfg.Namespace = tc.metricNameSpace + } + if len(tc.logGroupName) > 0 { + cfg.LogGroupName = tc.logGroupName + } + + assert.Equal(t, cfg.IsPulseApmEnabled(), tc.expectedResult) + }) + } +} diff --git a/exporter/awsemfexporter/emf_exporter.go b/exporter/awsemfexporter/emf_exporter.go index d00f5f074ee8..76d7690d6cae 100644 --- a/exporter/awsemfexporter/emf_exporter.go +++ b/exporter/awsemfexporter/emf_exporter.go @@ -66,8 +66,8 @@ func newEmfExporter(config *Config, set exporter.CreateSettings) (*emfExporter, config.LogRetention, config.Tags, session, - cwlogs.WithEnabledContainerInsights(isEnhancedContainerInsights(config)), - cwlogs.WithEnabledPulseApm(isPulseApmEnabled(config)), + cwlogs.WithEnabledContainerInsights(config.IsEnhancedContainerInsights()), + cwlogs.WithEnabledPulseApm(config.IsPulseApmEnabled()), ) collectorIdentifier, err := uuid.NewRandom() @@ -216,20 +216,3 @@ func wrapErrorIfBadRequest(err error) error { } return err } - -func isEnhancedContainerInsights(_ *Config) bool { - return false // temporarily disable, also need to rename _config to config - // return config.EnhancedContainerInsights && !config.DisableMetricExtraction -} - -func isPulseApmEnabled(config *Config) bool { - if config.LogGroupName == "" || config.Namespace == "" { - return false - } - - if config.Namespace == pulseMetricNamespace && strings.HasPrefix(config.LogGroupName, pulseLogGroupNamePrefix) { - return true - } - - return false -} diff --git a/exporter/awsemfexporter/emf_exporter_test.go b/exporter/awsemfexporter/emf_exporter_test.go index 9b061d0c8bc9..ca42c71d5741 100644 --- a/exporter/awsemfexporter/emf_exporter_test.go +++ b/exporter/awsemfexporter/emf_exporter_test.go @@ -348,71 +348,3 @@ func TestNewEmfExporterWithoutConfig(t *testing.T) { assert.Nil(t, exp) assert.Equal(t, settings.Logger, expCfg.logger) } - -func TestIsEnhancedContainerInsights(t *testing.T) { - factory := NewFactory() - cfg := factory.CreateDefaultConfig().(*Config) - cfg.EnhancedContainerInsights = true - cfg.DisableMetricExtraction = false - assert.False(t, isEnhancedContainerInsights(cfg)) - cfg.EnhancedContainerInsights = false - assert.False(t, isEnhancedContainerInsights(cfg)) - cfg.EnhancedContainerInsights = true - cfg.DisableMetricExtraction = true - assert.False(t, isEnhancedContainerInsights(cfg)) -} - -func TestIsPulseApmEnabled(t *testing.T) { - - tests := []struct { - name string - metricNameSpace string - logGroupName string - expectedResult bool - }{ - { - "validPulseEMF", - "AWS/APM", - "/aws/apm/eks", - true, - }, - { - "invalidPulseLogsGroup", - "AWS/APM", - "/nonaws/apm/eks", - false, - }, - { - "invalidPulseMetricNamespace", - "NonAWS/APM", - "/aws/apm/eks", - false, - }, - { - "invalidPulseEMF", - "NonAWS/APM", - "/nonaws/apm/eks", - false, - }, - { - "defaultConfig", - "", - "", - false, - }, - } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - factory := NewFactory() - cfg := factory.CreateDefaultConfig().(*Config) - if len(tc.metricNameSpace) > 0 { - cfg.Namespace = tc.metricNameSpace - } - if len(tc.logGroupName) > 0 { - cfg.LogGroupName = tc.logGroupName - } - - assert.Equal(t, isPulseApmEnabled(cfg), tc.expectedResult) - }) - } -}