diff --git a/exporter/awsemfexporter/emf_exporter.go b/exporter/awsemfexporter/emf_exporter.go index 2eb7ebbc1673..104051a7144f 100644 --- a/exporter/awsemfexporter/emf_exporter.go +++ b/exporter/awsemfexporter/emf_exporter.go @@ -103,7 +103,11 @@ func newEmfExporter(config *Config, set exporter.Settings) (*emfExporter, error) } func (emf *emfExporter) pushMetricsData(_ context.Context, md pmetric.Metrics) error { - rms := md.ResourceMetrics() + // the original resourceAttributes map is immutable, so we need to create a mutable copy of the resource metrics + // to remove the entity fields from the attributes + mutableMetrics := pmetric.NewMetrics() + md.CopyTo(mutableMetrics) + rms := mutableMetrics.ResourceMetrics() labels := map[string]string{} for i := 0; i < rms.Len(); i++ { rm := rms.At(i) @@ -127,6 +131,7 @@ func (emf *emfExporter) pushMetricsData(_ context.Context, md pmetric.Metrics) e if err != nil { return err } + rms.At(i).Resource().Attributes().RemoveIf(filterEntityAttributes()) } for _, groupedMetric := range groupedMetrics { @@ -176,6 +181,14 @@ func (emf *emfExporter) pushMetricsData(_ context.Context, md pmetric.Metrics) e return nil } +func filterEntityAttributes() func(s string, _ pcommon.Value) bool { + return func(s string, _ pcommon.Value) bool { + _, containsKeyAttribute := keyAttributeEntityToShortNameMap[s] + _, containsAttribute := keyAttributeEntityToShortNameMap[s] + return containsKeyAttribute || containsAttribute + } +} + func (emf *emfExporter) getPusher(key cwlogs.StreamKey) cwlogs.Pusher { var ok bool hash := key.Hash() @@ -192,11 +205,7 @@ func (emf *emfExporter) listPushers() []cwlogs.Pusher { emf.pusherMapLock.Lock() defer emf.pusherMapLock.Unlock() - var pushers []cwlogs.Pusher - for _, pusher := range emf.boundedPusherMap.ListAllPushers() { - pushers = append(pushers, pusher) - } - return pushers + return emf.boundedPusherMap.ListAllPushers() } func (emf *emfExporter) start(_ context.Context, host component.Host) error { diff --git a/exporter/awsemfexporter/emf_exporter_test.go b/exporter/awsemfexporter/emf_exporter_test.go index f6a66b4d67f5..b890dabafd97 100644 --- a/exporter/awsemfexporter/emf_exporter_test.go +++ b/exporter/awsemfexporter/emf_exporter_test.go @@ -75,6 +75,28 @@ func TestConsumeMetrics(t *testing.T) { require.NoError(t, exp.shutdown(ctx)) } +func TestFilterEntities(t *testing.T) { + md := generateTestMetrics(testMetric{ + metricNames: []string{"metric_1", "metric_2"}, + metricValues: [][]float64{{100}, {4}}, + resourceAttributeMap: map[string]any{ + "aws.ecs.cluster.name": "test-cluster-name", + "aws.ecs.task.id": "test-task-id", + keyAttributeEntityType: service, + keyAttributeEntityServiceName: "myService", + keyAttributeEntityDeploymentEnvironment: "myEnvironment", + }, + }) + + rms := md.ResourceMetrics() + + for i := 0; i < rms.Len(); i++ { + assert.Equal(t, 5, rms.At(i).Resource().Attributes().Len()) + rms.At(i).Resource().Attributes().RemoveIf(filterEntityAttributes()) + assert.Equal(t, 2, rms.At(i).Resource().Attributes().Len()) + } +} + func TestConsumeMetricsWithNaNValues(t *testing.T) { tests := []struct { testName string diff --git a/exporter/awsemfexporter/metric_translator.go b/exporter/awsemfexporter/metric_translator.go index 54614570dc5b..2733effbaaca 100644 --- a/exporter/awsemfexporter/metric_translator.go +++ b/exporter/awsemfexporter/metric_translator.go @@ -179,12 +179,7 @@ func (mt metricTranslator) translateOTelToGroupedMetric(rm pmetric.ResourceMetri metricReceiver = containerInsightsReceiver } } - // the original resourceAttributes map is immutable, so we need to create a mutable copy of the resource metrics - // to remove the entity fields from the attributes - newResourceMetrics := pmetric.NewResourceMetrics() - rm.CopyTo(newResourceMetrics) - entity := fetchEntityFields(newResourceMetrics.Resource().Attributes()) - rm = newResourceMetrics + entity := fetchEntityFields(rm.Resource().Attributes()) for j := 0; j < ilms.Len(); j++ { ilm := ilms.At(j) diff --git a/exporter/awsemfexporter/metric_translator_test.go b/exporter/awsemfexporter/metric_translator_test.go index 246b552b0c6f..54848f80780f 100644 --- a/exporter/awsemfexporter/metric_translator_test.go +++ b/exporter/awsemfexporter/metric_translator_test.go @@ -2599,10 +2599,8 @@ func TestFetchEntityFields(t *testing.T) { workload: aws.String("my-workload"), }, } - assert.Equal(t, 7, resourceMetrics.Resource().Attributes().Len()) entity := fetchEntityFields(resourceMetrics.Resource().Attributes()) assert.Equal(t, expectedEntity, entity) - assert.Equal(t, 0, resourceMetrics.Resource().Attributes().Len()) } diff --git a/exporter/awsemfexporter/util.go b/exporter/awsemfexporter/util.go index 61e3fcfbb57d..12a6d46aeb38 100644 --- a/exporter/awsemfexporter/util.go +++ b/exporter/awsemfexporter/util.go @@ -182,7 +182,7 @@ func processAttributes(entityMap map[string]string, targetMap map[string]*string if strVal := val.Str(); strVal != "" { targetMap[shortName] = aws.String(strVal) } - mutableResourceAttributes.Remove(entityField) + //mutableResourceAttributes.Remove(entityField) } } } diff --git a/exporter/awsemfexporter/util_test.go b/exporter/awsemfexporter/util_test.go index f52542e594d7..73e0ab9b6bae 100644 --- a/exporter/awsemfexporter/util_test.go +++ b/exporter/awsemfexporter/util_test.go @@ -374,7 +374,6 @@ func TestProcessAttributes(t *testing.T) { entityMap []map[string]string resourceAttributes map[string]any wantedAttributes map[string]*string - leftoverAttributes map[string]any }{ { name: "key_attributes", @@ -387,7 +386,6 @@ func TestProcessAttributes(t *testing.T) { serviceName: aws.String("my-service"), deploymentEnvironment: aws.String("my-environment"), }, - leftoverAttributes: make(map[string]any), }, { name: "non-key_attributes", @@ -404,7 +402,6 @@ func TestProcessAttributes(t *testing.T) { node: aws.String("my-node"), workload: aws.String("my-workload"), }, - leftoverAttributes: make(map[string]any), }, { name: "key_and_non_key_attributes", @@ -425,7 +422,6 @@ func TestProcessAttributes(t *testing.T) { node: aws.String("my-node"), workload: aws.String("my-workload"), }, - leftoverAttributes: make(map[string]any), }, { name: "key_and_non_key_attributes_plus_extras", @@ -447,9 +443,6 @@ func TestProcessAttributes(t *testing.T) { node: aws.String("my-node"), workload: aws.String("my-workload"), }, - leftoverAttributes: map[string]any{ - "extra_attribute": "extra_value", - }, }, } @@ -462,7 +455,6 @@ func TestProcessAttributes(t *testing.T) { for _, entityMap := range tc.entityMap { processAttributes(entityMap, targetMap, attrs) } - assert.Equal(t, tc.leftoverAttributes, attrs.AsRaw()) assert.Equal(t, tc.wantedAttributes, targetMap) }) }