From 126068be247aaea1f16f1e48ba4681762d726475 Mon Sep 17 00:00:00 2001 From: Mitali Salvi <44349099+mitali-salvi@users.noreply.github.com> Date: Wed, 11 Oct 2023 15:16:31 -0400 Subject: [PATCH] Filter out Prometheus scrape_ metrics if EnhancedContainer Insights is enabled (#110) --- exporter/awsemfexporter/emf_exporter.go | 5 ++ exporter/awsemfexporter/metric_translator.go | 3 + .../awsemfexporter/metric_translator_test.go | 85 ++++++++++++++++--- 3 files changed, 81 insertions(+), 12 deletions(-) diff --git a/exporter/awsemfexporter/emf_exporter.go b/exporter/awsemfexporter/emf_exporter.go index 981cf03e8645..d00f5f074ee8 100644 --- a/exporter/awsemfexporter/emf_exporter.go +++ b/exporter/awsemfexporter/emf_exporter.go @@ -123,6 +123,11 @@ func (emf *emfExporter) pushMetricsData(_ context.Context, md pmetric.Metrics) e if err != nil { 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 02fb101c5d34..a3f4e4e12f76 100644 --- a/exporter/awsemfexporter/metric_translator.go +++ b/exporter/awsemfexporter/metric_translator.go @@ -431,6 +431,9 @@ func translateCWMetricToEMF(cWMetric *cWMetrics, config *Config) (*cwlogs.Event, */ fieldMap["CloudWatchMetrics"] = cWMetric.measurements } + } else if len(cWMetric.measurements) < 1 && config.EnhancedContainerInsights { + // Return nil if requests does not contain metrics when EnhancedContainerInsights is enabled + return nil, nil } pleMsg, err := json.Marshal(fieldMap) diff --git a/exporter/awsemfexporter/metric_translator_test.go b/exporter/awsemfexporter/metric_translator_test.go index a72c725aa6fa..36b39c87bf31 100644 --- a/exporter/awsemfexporter/metric_translator_test.go +++ b/exporter/awsemfexporter/metric_translator_test.go @@ -481,6 +481,67 @@ func TestTranslateCWMetricToEMF(t *testing.T) { } +func TestTranslateCWMetricToEMFForEnhancedContainerInsights(t *testing.T) { + testCases := map[string]struct { + EnhancedContainerInsights bool + fields map[string]interface{} + measurements []cWMeasurement + expectedEMFLogEvent interface{} + }{ + "EnhancedContainerInsightsEnabled": { + EnhancedContainerInsights: true, + fields: map[string]interface{}{ + oTellibDimensionKey: "cloudwatch-otel", + "scrape_samples_post_metric_relabeling": "12", + "scrape_samples_scraped": "34", + "scrape_series_added": "56", + "service.instance.id": "1.2.3.4:443", + "Sources": "[\"apiserver\"]", + }, + measurements: nil, + expectedEMFLogEvent: nil, + }, + "EnhancedContainerInsightsDisabled": { + EnhancedContainerInsights: false, + fields: map[string]interface{}{ + oTellibDimensionKey: "cloudwatch-otel", + "scrape_samples_post_metric_relabeling": "12", + "scrape_samples_scraped": "34", + "scrape_series_added": "56", + "service.instance.id": "1.2.3.4:443", + "Sources": "[\"apiserver\"]", + }, + measurements: nil, + expectedEMFLogEvent: "{\"OTelLib\":\"cloudwatch-otel\",\"Sources\":[\"apiserver\"],\"scrape_samples_post_metric_relabeling\":\"12\",\"scrape_samples_scraped\":\"34\",\"scrape_series_added\":\"56\",\"service.instance.id\":\"1.2.3.4:443\"}", + }, + } + + for name, tc := range testCases { + t.Run(name, func(_ *testing.T) { + config := &Config{ + // include valid json string, a non-existing key, and keys whose value are not json/string + ParseJSONEncodedAttributeValues: []string{"Sources"}, + EnhancedContainerInsights: tc.EnhancedContainerInsights, + logger: zap.NewNop(), + } + + cloudwatchMetric := &cWMetrics{ + timestampMs: int64(1596151098037), + fields: tc.fields, + measurements: tc.measurements, + } + + emfLogEvent, err := translateCWMetricToEMF(cloudwatchMetric, config) + require.NoError(t, err) + + if tc.expectedEMFLogEvent != nil { + assert.Equal(t, tc.expectedEMFLogEvent, *emfLogEvent.InputLogEvent.Message) + } + }) + } + +} + func TestTranslateGroupedMetricToCWMetric(t *testing.T) { timestamp := int64(1596151098037) namespace := "Namespace" @@ -1395,22 +1456,22 @@ func TestGroupedMetricToCWMeasurementsWithFilters(t *testing.T) { MetricNameSelectors: []string{"metric(1|3)"}, }, }, []cWMeasurement{ - { - Namespace: namespace, - Dimensions: [][]string{{}}, - Metrics: []map[string]string{ - { - "Name": "metric1", - "Unit": "Count", - }, - { - "Name": "metric3", - "Unit": "Seconds", - }, + { + Namespace: namespace, + Dimensions: [][]string{{}}, + Metrics: []map[string]string{ + { + "Name": "metric1", + "Unit": "Count", + }, + { + "Name": "metric3", + "Unit": "Seconds", }, }, }, }, + }, { "label matchers", []*MetricDeclaration{