From fa1fb8e70f04a1683e4958e893b6c41dd7c91e3c Mon Sep 17 00:00:00 2001 From: Dinakar Chappa Date: Fri, 4 Oct 2024 09:36:49 -0400 Subject: [PATCH] addresses lint findings and unit test failures --- exporter/awsemfexporter/metric_translator.go | 11 +++--- .../awsemfexporter/metric_translator_test.go | 6 ++- exporter/awsemfexporter/util.go | 16 ++++++-- exporter/awsemfexporter/util_test.go | 38 +++++++++++++++---- 4 files changed, 53 insertions(+), 18 deletions(-) diff --git a/exporter/awsemfexporter/metric_translator.go b/exporter/awsemfexporter/metric_translator.go index 712d8ea7981a..917dab1ea7c5 100644 --- a/exporter/awsemfexporter/metric_translator.go +++ b/exporter/awsemfexporter/metric_translator.go @@ -48,9 +48,9 @@ const ( keyAttributeEntityResourceType = AWSEntityPrefix + "resource.type" resourceType = "ResourceType" keyAttributeEntityIdentifier = AWSEntityPrefix + "resource.identifier" - keyAttributeEntityAwsAccountId = AWSEntityPrefix + "aws.account.id" + keyAttributeEntityAwsAccountID = AWSEntityPrefix + "aws.account.id" identifier = "Identifier" - awsAccountId = "AwsAccountId" + awsAccountID = "AwsAccountId" attributeEntityCluster = AWSEntityPrefix + "k8s.cluster.name" cluster = "Cluster" attributeEntityNamespace = AWSEntityPrefix + "k8s.namespace.name" @@ -73,7 +73,7 @@ var keyAttributeEntityToShortNameMap = map[string]string{ keyAttributeEntityType: entityType, keyAttributeEntityResourceType: resourceType, keyAttributeEntityIdentifier: identifier, - keyAttributeEntityAwsAccountId: awsAccountId, + keyAttributeEntityAwsAccountID: awsAccountID, keyAttributeEntityServiceName: serviceName, keyAttributeEntityDeploymentEnvironment: deploymentEnvironment, keyAttributeEntityServiceNameSource: source, @@ -236,8 +236,9 @@ func fetchEntityFields(resourceAttributes pcommon.Map) cloudwatchlogs.Entity { keyAttributesMap := map[string]*string{} attributeMap := map[string]*string{} - processAttributes(keyAttributeEntityToShortNameMap, keyAttributesMap, resourceAttributes) - processAttributes(attributeEntityToShortNameMap, attributeMap, resourceAttributes) + processEntityAttributes(keyAttributeEntityToShortNameMap, keyAttributesMap, resourceAttributes) + processEntityAttributes(attributeEntityToShortNameMap, attributeMap, resourceAttributes) + removeEntityAttributes(resourceAttributes) return cloudwatchlogs.Entity{ KeyAttributes: keyAttributesMap, diff --git a/exporter/awsemfexporter/metric_translator_test.go b/exporter/awsemfexporter/metric_translator_test.go index 2dff3a7faeaa..90a9b163dbb8 100644 --- a/exporter/awsemfexporter/metric_translator_test.go +++ b/exporter/awsemfexporter/metric_translator_test.go @@ -2582,16 +2582,17 @@ func TestFetchEntityFields(t *testing.T) { resourceMetrics.Resource().Attributes().PutStr(keyAttributeEntityType, "Service") resourceMetrics.Resource().Attributes().PutStr(keyAttributeEntityDeploymentEnvironment, "my-environment") resourceMetrics.Resource().Attributes().PutStr(keyAttributeEntityServiceName, "my-service") - resourceMetrics.Resource().Attributes().PutStr(keyAttributeEntityAwsAccountId, "0123456789012") + resourceMetrics.Resource().Attributes().PutStr(keyAttributeEntityAwsAccountID, "0123456789012") resourceMetrics.Resource().Attributes().PutStr(attributeEntityNode, "my-node") resourceMetrics.Resource().Attributes().PutStr(attributeEntityCluster, "my-cluster") resourceMetrics.Resource().Attributes().PutStr(attributeEntityNamespace, "my-namespace") resourceMetrics.Resource().Attributes().PutStr(attributeEntityWorkload, "my-workload") + resourceMetrics.Resource().Attributes().PutStr(AWSEntityPrefix+"fake_value", "go_terps") expectedEntity := cloudwatchlogs.Entity{KeyAttributes: map[string]*string{ entityType: aws.String(service), serviceName: aws.String("my-service"), - awsAccountId: aws.String("0123456789012"), + awsAccountID: aws.String("0123456789012"), deploymentEnvironment: aws.String("my-environment"), }, Attributes: map[string]*string{ @@ -2603,6 +2604,7 @@ func TestFetchEntityFields(t *testing.T) { } entity := fetchEntityFields(resourceMetrics.Resource().Attributes()) assert.Equal(t, expectedEntity, entity) + assert.Equal(t, 0, resourceMetrics.Resource().Attributes().Len()) } func generateTestMetrics(tm testMetric) pmetric.Metrics { diff --git a/exporter/awsemfexporter/util.go b/exporter/awsemfexporter/util.go index 61e3fcfbb57d..9dddde4b0824 100644 --- a/exporter/awsemfexporter/util.go +++ b/exporter/awsemfexporter/util.go @@ -174,15 +174,23 @@ func attrMaptoStringMap(attrMap pcommon.Map) map[string]string { return strMap } -// processAttributes fetches the aws.entity fields and creates an entity to be sent at the PutLogEvent call. It also -// removes the entity attributes so that it is not tagged as a dimension, and reduces the size of the PLE payload. -func processAttributes(entityMap map[string]string, targetMap map[string]*string, mutableResourceAttributes pcommon.Map) { +// processEntityAttributes fetches the aws.entity fields and creates an entity to be sent at the PutLogEvent call. +func processEntityAttributes(entityMap map[string]string, targetMap map[string]*string, mutableResourceAttributes pcommon.Map) { for entityField, shortName := range entityMap { if val, ok := mutableResourceAttributes.Get(entityField); ok { if strVal := val.Str(); strVal != "" { targetMap[shortName] = aws.String(strVal) } - mutableResourceAttributes.Remove(entityField) } } } + +// removeEntityAttributes so that it is not tagged as a dimension, and reduces the size of the PLE payload. +func removeEntityAttributes(mutableResourceAttributes pcommon.Map) { + mutableResourceAttributes.RemoveIf(func(s string, _ pcommon.Value) bool { + if strings.HasPrefix(s, AWSEntityPrefix) { + return true + } + return false + }) +} diff --git a/exporter/awsemfexporter/util_test.go b/exporter/awsemfexporter/util_test.go index 0a788f59171c..d72c41064473 100644 --- a/exporter/awsemfexporter/util_test.go +++ b/exporter/awsemfexporter/util_test.go @@ -368,7 +368,7 @@ func TestGetLogInfo(t *testing.T) { } -func TestProcessAttributes(t *testing.T) { +func TestProcessAndRemoveEntityAttributes(t *testing.T) { testCases := []struct { name string entityMap []map[string]string @@ -382,11 +382,12 @@ func TestProcessAttributes(t *testing.T) { resourceAttributes: map[string]any{ keyAttributeEntityServiceName: "my-service", keyAttributeEntityDeploymentEnvironment: "my-environment", - keyAttributeEntityAwsAccountId: "0123456789012", + keyAttributeEntityAwsAccountID: "0123456789012", }, wantedAttributes: map[string]*string{ serviceName: aws.String("my-service"), deploymentEnvironment: aws.String("my-environment"), + awsAccountID: aws.String("0123456789012"), }, leftoverAttributes: make(map[string]any), }, @@ -413,7 +414,7 @@ func TestProcessAttributes(t *testing.T) { resourceAttributes: map[string]any{ keyAttributeEntityServiceName: "my-service", keyAttributeEntityDeploymentEnvironment: "my-environment", - keyAttributeEntityAwsAccountId: "0123456789012", + keyAttributeEntityAwsAccountID: "0123456789012", attributeEntityCluster: "my-cluster", attributeEntityNamespace: "my-namespace", attributeEntityNode: "my-node", @@ -422,7 +423,7 @@ func TestProcessAttributes(t *testing.T) { wantedAttributes: map[string]*string{ serviceName: aws.String("my-service"), deploymentEnvironment: aws.String("my-environment"), - awsAccountId: aws.String("0123456789012"), + awsAccountID: aws.String("0123456789012"), cluster: aws.String("my-cluster"), namespace: aws.String("my-namespace"), node: aws.String("my-node"), @@ -437,7 +438,7 @@ func TestProcessAttributes(t *testing.T) { "extra_attribute": "extra_value", keyAttributeEntityServiceName: "my-service", keyAttributeEntityDeploymentEnvironment: "my-environment", - keyAttributeEntityAwsAccountId: "0123456789012", + keyAttributeEntityAwsAccountID: "0123456789012", attributeEntityCluster: "my-cluster", attributeEntityNamespace: "my-namespace", attributeEntityNode: "my-node", @@ -446,7 +447,7 @@ func TestProcessAttributes(t *testing.T) { wantedAttributes: map[string]*string{ serviceName: aws.String("my-service"), deploymentEnvironment: aws.String("my-environment"), - awsAccountId: aws.String("0123456789012"), + awsAccountID: aws.String("0123456789012"), cluster: aws.String("my-cluster"), namespace: aws.String("my-namespace"), node: aws.String("my-node"), @@ -456,6 +457,28 @@ func TestProcessAttributes(t *testing.T) { "extra_attribute": "extra_value", }, }, + { + name: "key_and_non_key_attributes_plus_unsupported_entity_field", + entityMap: []map[string]string{keyAttributeEntityToShortNameMap, attributeEntityToShortNameMap}, + resourceAttributes: map[string]any{ + AWSEntityPrefix + "not.real.values": "unsupported", + keyAttributeEntityServiceName: "my-service", + keyAttributeEntityDeploymentEnvironment: "my-environment", + attributeEntityCluster: "my-cluster", + attributeEntityNamespace: "my-namespace", + attributeEntityNode: "my-node", + attributeEntityWorkload: "my-workload", + }, + wantedAttributes: map[string]*string{ + serviceName: aws.String("my-service"), + deploymentEnvironment: aws.String("my-environment"), + cluster: aws.String("my-cluster"), + namespace: aws.String("my-namespace"), + node: aws.String("my-node"), + workload: aws.String("my-workload"), + }, + leftoverAttributes: map[string]any{}, + }, } for _, tc := range testCases { @@ -465,8 +488,9 @@ func TestProcessAttributes(t *testing.T) { assert.Nil(t, err) targetMap := make(map[string]*string) for _, entityMap := range tc.entityMap { - processAttributes(entityMap, targetMap, attrs) + processEntityAttributes(entityMap, targetMap, attrs) } + removeEntityAttributes(attrs) assert.Equal(t, tc.leftoverAttributes, attrs.AsRaw()) assert.Equal(t, tc.wantedAttributes, targetMap) })