From a03b896a145aa50e0c88e84aea67ee54e047ab9b Mon Sep 17 00:00:00 2001 From: Lisa Guo Date: Wed, 20 Dec 2023 13:40:38 -0500 Subject: [PATCH] Return error for enhanced container insights for missing metrics in emfexporter --- exporter/awsemfexporter/emf_exporter.go | 12 +++---- exporter/awsemfexporter/metric_translator.go | 8 ++++- .../awsemfexporter/metric_translator_test.go | 32 +++++++++++++++++++ 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/exporter/awsemfexporter/emf_exporter.go b/exporter/awsemfexporter/emf_exporter.go index f43f74817cde..2ecc3f32d8ce 100644 --- a/exporter/awsemfexporter/emf_exporter.go +++ b/exporter/awsemfexporter/emf_exporter.go @@ -10,7 +10,6 @@ import ( "strings" "sync" - "github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/google/uuid" "go.opentelemetry.io/collector/component" @@ -20,6 +19,8 @@ import ( "go.opentelemetry.io/collector/pdata/pmetric" "go.uber.org/zap" + "github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware" + "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/awsutil" "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/cwlogs" ) @@ -122,13 +123,12 @@ func (emf *emfExporter) pushMetricsData(_ context.Context, md pmetric.Metrics) e for _, groupedMetric := range groupedMetrics { putLogEvent, err := translateGroupedMetricToEmf(groupedMetric, emf.config, defaultLogStream) if err != nil { + if errors.Is(err, errMissingMetricsForEnhancedContainerInsights) { + continue + } return err } - // Drop a nil putLogEvent for EnhancedContainerInsights - if emf.config.EnhancedContainerInsights && putLogEvent == nil { - emf.config.logger.Debug("Dropping empty putLogEvents for EnhancedContainerInsights") - continue - } + // Currently we only support two options for "OutputDestination". if strings.EqualFold(outputDestination, outputDestinationStdout) { if putLogEvent != nil && diff --git a/exporter/awsemfexporter/metric_translator.go b/exporter/awsemfexporter/metric_translator.go index f0e5324f09e5..5ec06fe13fa6 100644 --- a/exporter/awsemfexporter/metric_translator.go +++ b/exporter/awsemfexporter/metric_translator.go @@ -5,6 +5,7 @@ package awsemfexporter // import "github.com/open-telemetry/opentelemetry-collec import ( "encoding/json" + "errors" "fmt" "reflect" "strings" @@ -33,6 +34,8 @@ const ( fieldPrometheusMetricType = "prom_metric_type" ) +var errMissingMetricsForEnhancedContainerInsights = errors.New("Dropping empty putLogEvents for EnhancedContainerInsights") + var fieldPrometheusTypes = map[pmetric.MetricType]string{ pmetric.MetricTypeEmpty: "", pmetric.MetricTypeGauge: "gauge", @@ -490,10 +493,13 @@ func translateCWMetricToEMF(cWMetric *cWMetrics, config *Config) (*cwlogs.Event, func translateGroupedMetricToEmf(groupedMetric *groupedMetric, config *Config, defaultLogStream string) (*cwlogs.Event, error) { cWMetric := translateGroupedMetricToCWMetric(groupedMetric, config) event, err := translateCWMetricToEMF(cWMetric, config) - if err != nil { return nil, err } + // Drop a nil putLogEvent for EnhancedContainerInsights + if config.EnhancedContainerInsights && event == nil { + return nil, errMissingMetricsForEnhancedContainerInsights + } logGroup := groupedMetric.metadata.logGroup logStream := groupedMetric.metadata.logStream diff --git a/exporter/awsemfexporter/metric_translator_test.go b/exporter/awsemfexporter/metric_translator_test.go index 00eb94aa149b..7ed82ac2b21d 100644 --- a/exporter/awsemfexporter/metric_translator_test.go +++ b/exporter/awsemfexporter/metric_translator_test.go @@ -4,6 +4,7 @@ package awsemfexporter import ( + "errors" "fmt" "math" "reflect" @@ -20,6 +21,7 @@ import ( "go.uber.org/zap/zapcore" "go.uber.org/zap/zaptest/observer" + "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/cwlogs" "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/occonventions" ) @@ -578,6 +580,36 @@ func TestTranslateCWMetricToEMFForEnhancedContainerInsights(t *testing.T) { } +func TestTranslateGroupedMetricToEmfForEnhancedContainerInsights(t *testing.T) { + tests := map[string]struct { + groupedMetric *groupedMetric + config *Config + defaultLogStream string + want *cwlogs.Event + expectedErr error + }{ + "EnhancedContainerInsightsEnabledNoMetrics": { + groupedMetric: &groupedMetric{}, + config: &Config{EnhancedContainerInsights: true, DisableMetricExtraction: true}, + defaultLogStream: "", + want: nil, + expectedErr: errMissingMetricsForEnhancedContainerInsights, + }, + } + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + got, err := translateGroupedMetricToEmf(tt.groupedMetric, tt.config, tt.defaultLogStream) + if (err != nil) && !errors.Is(err, tt.expectedErr) { + t.Errorf("translateGroupedMetricToEmf() error = %v, expectedErr %v", err, tt.expectedErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("translateGroupedMetricToEmf() got = %v, want %v", got, tt.want) + } + }) + } +} + func TestTranslateGroupedMetricToCWMetric(t *testing.T) { timestamp := int64(1596151098037) namespace := "Namespace"