Skip to content

Commit

Permalink
Filter out Prometheus scrape_ metrics if EnhancedContainer Insights i…
Browse files Browse the repository at this point in the history
  • Loading branch information
mitali-salvi authored and lisguo committed Oct 20, 2023
1 parent b2fee6a commit 126068b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 12 deletions.
5 changes: 5 additions & 0 deletions exporter/awsemfexporter/emf_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
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

0 comments on commit 126068b

Please sign in to comment.