From 83290c6c3a9f13c8ac004b8f1910a042eeea7b99 Mon Sep 17 00:00:00 2001 From: Pooja Reddy Nathala Date: Wed, 30 Oct 2024 12:50:50 -0400 Subject: [PATCH] Drop entity if account ID is not present in cloudwatch exporter --- plugins/outputs/cloudwatch/convert_otel.go | 4 ++- .../outputs/cloudwatch/convert_otel_test.go | 33 +++++++++++++++++-- plugins/outputs/cloudwatchlogs/pusher.go | 9 ++--- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/plugins/outputs/cloudwatch/convert_otel.go b/plugins/outputs/cloudwatch/convert_otel.go index d91ce92c61..a318eaa265 100644 --- a/plugins/outputs/cloudwatch/convert_otel.go +++ b/plugins/outputs/cloudwatch/convert_otel.go @@ -195,7 +195,9 @@ func fetchEntityFields(resourceAttributes pcommon.Map) cloudwatch.Entity { processEntityAttributes(entityattributes.GetKeyAttributeEntityShortNameMap(), keyAttributesMap, resourceAttributes) processEntityAttributes(entityattributes.GetAttributeEntityShortNameMap(platformType), attributeMap, resourceAttributes) removeEntityFields(resourceAttributes) - + if _, ok := keyAttributesMap[entityattributes.AwsAccountId]; !ok { + return cloudwatch.Entity{} + } return cloudwatch.Entity{ KeyAttributes: keyAttributesMap, Attributes: attributeMap, diff --git a/plugins/outputs/cloudwatch/convert_otel_test.go b/plugins/outputs/cloudwatch/convert_otel_test.go index ae4a825f5b..458d72de3a 100644 --- a/plugins/outputs/cloudwatch/convert_otel_test.go +++ b/plugins/outputs/cloudwatch/convert_otel_test.go @@ -378,7 +378,7 @@ func TestProcessAndRemoveEntityAttributes(t *testing.T) { } } -func TestFetchEntityFields(t *testing.T) { +func TestFetchEntityFields_WithoutAccountID(t *testing.T) { resourceMetrics := pmetric.NewResourceMetrics() resourceMetrics.Resource().Attributes().PutStr(entityattributes.AttributeEntityType, "Service") resourceMetrics.Resource().Attributes().PutStr(entityattributes.AttributeEntityDeploymentEnvironment, "my-environment") @@ -390,11 +390,34 @@ func TestFetchEntityFields(t *testing.T) { resourceMetrics.Resource().Attributes().PutStr(entityattributes.AttributeEntityPlatformType, "AWS::EKS") assert.Equal(t, 8, resourceMetrics.Resource().Attributes().Len()) + expectedEntity := cloudwatch.Entity{ + KeyAttributes: nil, + Attributes: nil, + } + entity := fetchEntityFields(resourceMetrics.Resource().Attributes()) + assert.Equal(t, 0, resourceMetrics.Resource().Attributes().Len()) + assert.Equal(t, expectedEntity, entity) +} + +func TestFetchEntityFields_WithAccountID(t *testing.T) { + resourceMetrics := pmetric.NewResourceMetrics() + resourceMetrics.Resource().Attributes().PutStr(entityattributes.AttributeEntityType, "Service") + resourceMetrics.Resource().Attributes().PutStr(entityattributes.AttributeEntityDeploymentEnvironment, "my-environment") + resourceMetrics.Resource().Attributes().PutStr(entityattributes.AttributeEntityServiceName, "my-service") + resourceMetrics.Resource().Attributes().PutStr(entityattributes.AttributeEntityNode, "my-node") + resourceMetrics.Resource().Attributes().PutStr(entityattributes.AttributeEntityCluster, "my-cluster") + resourceMetrics.Resource().Attributes().PutStr(entityattributes.AttributeEntityNamespace, "my-namespace") + resourceMetrics.Resource().Attributes().PutStr(entityattributes.AttributeEntityWorkload, "my-workload") + resourceMetrics.Resource().Attributes().PutStr(entityattributes.AttributeEntityPlatformType, "AWS::EKS") + resourceMetrics.Resource().Attributes().PutStr(entityattributes.AttributeEntityAwsAccountId, "123456789") + assert.Equal(t, 9, resourceMetrics.Resource().Attributes().Len()) + expectedEntity := cloudwatch.Entity{ KeyAttributes: map[string]*string{ entityattributes.EntityType: aws.String(entityattributes.Service), entityattributes.ServiceName: aws.String("my-service"), entityattributes.DeploymentEnvironment: aws.String("my-environment"), + entityattributes.AwsAccountId: aws.String("123456789"), }, Attributes: map[string]*string{ entityattributes.Node: aws.String("my-node"), @@ -421,13 +444,15 @@ func TestFetchEntityFieldsOnK8s(t *testing.T) { resourceMetrics.Resource().Attributes().PutStr(entityattributes.AttributeEntityNamespace, "my-namespace") resourceMetrics.Resource().Attributes().PutStr(entityattributes.AttributeEntityWorkload, "my-workload") resourceMetrics.Resource().Attributes().PutStr(entityattributes.AttributeEntityPlatformType, "K8s") - assert.Equal(t, 8, resourceMetrics.Resource().Attributes().Len()) + resourceMetrics.Resource().Attributes().PutStr(entityattributes.AttributeEntityAwsAccountId, "123456789") + assert.Equal(t, 9, resourceMetrics.Resource().Attributes().Len()) expectedEntity := cloudwatch.Entity{ KeyAttributes: map[string]*string{ entityattributes.EntityType: aws.String(entityattributes.Service), entityattributes.ServiceName: aws.String("my-service"), entityattributes.DeploymentEnvironment: aws.String("my-environment"), + entityattributes.AwsAccountId: aws.String("123456789"), }, Attributes: map[string]*string{ entityattributes.Node: aws.String("my-node"), @@ -448,13 +473,15 @@ func TestFetchEntityFieldsOnEc2(t *testing.T) { resourceMetrics.Resource().Attributes().PutStr(entityattributes.AttributeEntityDeploymentEnvironment, "my-environment") resourceMetrics.Resource().Attributes().PutStr(entityattributes.AttributeEntityServiceName, "my-service") resourceMetrics.Resource().Attributes().PutStr(entityattributes.AttributeEntityPlatformType, "AWS::EC2") - assert.Equal(t, 4, resourceMetrics.Resource().Attributes().Len()) + resourceMetrics.Resource().Attributes().PutStr(entityattributes.AttributeEntityAwsAccountId, "123456789") + assert.Equal(t, 5, resourceMetrics.Resource().Attributes().Len()) expectedEntity := cloudwatch.Entity{ KeyAttributes: map[string]*string{ entityattributes.EntityType: aws.String(entityattributes.Service), entityattributes.ServiceName: aws.String("my-service"), entityattributes.DeploymentEnvironment: aws.String("my-environment"), + entityattributes.AwsAccountId: aws.String("123456789"), }, Attributes: map[string]*string{ entityattributes.Platform: aws.String("AWS::EC2"), diff --git a/plugins/outputs/cloudwatchlogs/pusher.go b/plugins/outputs/cloudwatchlogs/pusher.go index 2c1036f1e8..ff0bb1dd0e 100644 --- a/plugins/outputs/cloudwatchlogs/pusher.go +++ b/plugins/outputs/cloudwatchlogs/pusher.go @@ -222,18 +222,15 @@ func (p *pusher) send() { if p.needSort { sort.Stable(ByTimestamp(p.events)) } - var entity *cloudwatchlogs.Entity - if p.logSrc != nil { - entity = p.logSrc.Entity() - } - input := &cloudwatchlogs.PutLogEventsInput{ LogEvents: p.events, LogGroupName: &p.Group, LogStreamName: &p.Stream, SequenceToken: p.sequenceToken, } - input.Entity = entity + if p.logSrc != nil { + input.Entity = p.logSrc.Entity() + } startTime := time.Now()