Skip to content

Commit

Permalink
Drop entity if account ID is not present in cloudwatch exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
nathalapooja committed Oct 30, 2024
1 parent 07cd92a commit 83290c6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
4 changes: 3 additions & 1 deletion plugins/outputs/cloudwatch/convert_otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
33 changes: 30 additions & 3 deletions plugins/outputs/cloudwatch/convert_otel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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"),
Expand All @@ -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"),
Expand All @@ -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"),
Expand Down
9 changes: 3 additions & 6 deletions plugins/outputs/cloudwatchlogs/pusher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 83290c6

Please sign in to comment.