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

Filter out Prometheus scrape_ metrics if EnhancedContainer Insights is enabled #110

Merged
merged 11 commits into from
Oct 11, 2023
Merged
5 changes: 5 additions & 0 deletions exporter/awsemfexporter/emf_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,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 &&
Expand Down
3 changes: 3 additions & 0 deletions exporter/awsemfexporter/metric_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
85 changes: 73 additions & 12 deletions exporter/awsemfexporter/metric_translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down