Skip to content

Commit

Permalink
addresses lint findings and unit test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
dchappa committed Oct 4, 2024
1 parent a9d34e1 commit fa1fb8e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 18 deletions.
11 changes: 6 additions & 5 deletions exporter/awsemfexporter/metric_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions exporter/awsemfexporter/metric_translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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 {
Expand Down
16 changes: 12 additions & 4 deletions exporter/awsemfexporter/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
38 changes: 31 additions & 7 deletions exporter/awsemfexporter/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
},
Expand All @@ -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",
Expand All @@ -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"),
Expand All @@ -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",
Expand All @@ -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"),
Expand All @@ -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 {
Expand All @@ -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)
})
Expand Down

0 comments on commit fa1fb8e

Please sign in to comment.