Skip to content

Commit

Permalink
Return error for enhanced container insights for missing metrics in e…
Browse files Browse the repository at this point in the history
…mfexporter
  • Loading branch information
lisguo committed Dec 20, 2023
1 parent 7a53f89 commit a03b896
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
12 changes: 6 additions & 6 deletions exporter/awsemfexporter/emf_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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 &&
Expand Down
8 changes: 7 additions & 1 deletion exporter/awsemfexporter/metric_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package awsemfexporter // import "github.com/open-telemetry/opentelemetry-collec

import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions exporter/awsemfexporter/metric_translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package awsemfexporter

import (
"errors"
"fmt"
"math"
"reflect"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit a03b896

Please sign in to comment.