Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[resourcedetectionprocessor]: Remove usage of insert in ecs, avoid unnecessary copies #13908

Merged
merged 1 commit into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 19 additions & 25 deletions processor/resourcedetectionprocessor/internal/aws/ecs/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (d *Detector) Detect(context.Context) (resource pcommon.Resource, schemaURL
attr.UpsertString(conventions.AttributeCloudRegion, region)
}

// TMDE returns the the cluster short name or ARN, so we need to construct the ARN if necessary
// TMDE returns the cluster short name or ARN, so we need to construct the ARN if necessary
attr.UpsertString(conventions.AttributeAWSECSClusterARN, constructClusterArn(tmdeResp.Cluster, region, account))

// The Availability Zone is not available in all Fargate runtimes
Expand All @@ -109,18 +109,7 @@ func (d *Detector) Detect(context.Context) (resource pcommon.Resource, schemaURL
return res, "", err
}

logAttributes := [4]string{
conventions.AttributeAWSLogGroupNames,
conventions.AttributeAWSLogGroupARNs,
conventions.AttributeAWSLogStreamNames,
conventions.AttributeAWSLogStreamARNs,
}

for i, attribVal := range getValidLogData(tmdeResp.Containers, selfMetaData, account) {
if attribVal.SliceVal().Len() > 0 {
attr.Insert(logAttributes[i], attribVal)
}
}
addValidLogData(tmdeResp.Containers, selfMetaData, account, attr)

return res, conventions.SchemaURL, nil
}
Expand Down Expand Up @@ -149,11 +138,12 @@ func parseRegionAndAccount(taskARN string) (region string, account string) {
// "init" containers which only run at startup then shutdown (as indicated by the "KnownStatus" attribute),
// containers not using AWS Logs, and those without log group metadata to get the final lists of valid log data
// See: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v4.html#task-metadata-endpoint-v4-response
func getValidLogData(containers []ecsutil.ContainerMetadata, self *ecsutil.ContainerMetadata, account string) [4]pcommon.Value {
logGroupNames := pcommon.NewValueSlice()
logGroupArns := pcommon.NewValueSlice()
logStreamNames := pcommon.NewValueSlice()
logStreamArns := pcommon.NewValueSlice()
func addValidLogData(containers []ecsutil.ContainerMetadata, self *ecsutil.ContainerMetadata, account string, dest pcommon.Map) {
initialized := false
var logGroupNames pcommon.Slice
var logGroupArns pcommon.Slice
var logStreamNames pcommon.Slice
var logStreamArns pcommon.Slice

for _, container := range containers {
logData := container.LogOptions
Expand All @@ -162,15 +152,19 @@ func getValidLogData(containers []ecsutil.ContainerMetadata, self *ecsutil.Conta
container.LogDriver == "awslogs" &&
self.DockerID != container.DockerID &&
logData != (ecsutil.LogOptions{}) {

logGroupNames.SliceVal().AppendEmpty().SetStringVal(logData.LogGroup)
logGroupArns.SliceVal().AppendEmpty().SetStringVal(constructLogGroupArn(logData.Region, account, logData.LogGroup))
logStreamNames.SliceVal().AppendEmpty().SetStringVal(logData.Stream)
logStreamArns.SliceVal().AppendEmpty().SetStringVal(constructLogStreamArn(logData.Region, account, logData.LogGroup, logData.Stream))
if !initialized {
logGroupNames = dest.UpsertEmptySlice(conventions.AttributeAWSLogGroupNames)
logGroupArns = dest.UpsertEmptySlice(conventions.AttributeAWSLogGroupARNs)
logStreamNames = dest.UpsertEmptySlice(conventions.AttributeAWSLogStreamNames)
logStreamArns = dest.UpsertEmptySlice(conventions.AttributeAWSLogStreamARNs)
initialized = true
}
logGroupNames.AppendEmpty().SetStringVal(logData.LogGroup)
logGroupArns.AppendEmpty().SetStringVal(constructLogGroupArn(logData.Region, account, logData.LogGroup))
logStreamNames.AppendEmpty().SetStringVal(logData.Stream)
logStreamArns.AppendEmpty().SetStringVal(constructLogStreamArn(logData.Region, account, logData.LogGroup, logData.Stream))
}
}

return [4]pcommon.Value{logGroupNames, logGroupArns, logStreamNames, logStreamArns}
}

func constructLogGroupArn(region, account, group string) string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,9 @@ func Test_ecsFiltersInvalidContainers(t *testing.T) {

containers := []ecsutil.ContainerMetadata{c1, c2, c3, c4}

ld := getValidLogData(containers, &c4, "123")

for _, attrib := range ld {
assert.Equal(t, 0, attrib.SliceVal().Len())
}
dest := pcommon.NewMap()
addValidLogData(containers, &c4, "123", dest)
assert.Equal(t, 0, dest.Len())
}

func Test_ecsDetectV4(t *testing.T) {
Expand Down