Skip to content

Commit

Permalink
Set cloudwatch logs metadata in attributes. (open-telemetry#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
jefchien authored Apr 9, 2024
1 parent 52eeafe commit da405ec
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 2 deletions.
12 changes: 12 additions & 0 deletions receiver/awsxrayreceiver/internal/translator/addtoattrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,15 @@ func addInt64(val *int64, attrKey string, attrs pcommon.Map) {
attrs.PutInt(attrKey, *val)
}
}

func addStringSlice(val *string, attrKey string, attrs pcommon.Map) {
if val != nil {
var slice pcommon.Slice
if attrVal, ok := attrs.Get(attrKey); ok && attrVal.Type() == pcommon.ValueTypeSlice {
slice = attrVal.Slice()
} else {
slice = attrs.PutEmptySlice(attrKey)
}
slice.AppendEmpty().SetStr(*val)
}
}
10 changes: 8 additions & 2 deletions receiver/awsxrayreceiver/internal/translator/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ func addAWSToResource(aws *awsxray.AWSData, attrs pcommon.Map) {
addString(aws.AccountID, conventions.AttributeCloudAccountID, attrs)

// based on https://docs.aws.amazon.com/xray/latest/devguide/xray-api-segmentdocuments.html#api-segmentdocuments-aws
// it's possible to have all ec2, ecs and beanstalk fields at the same time.
// it's possible to have all cloudwatch_logs, ec2, ecs and beanstalk fields at the same time.
if cwl := aws.CWLogs; cwl != nil {
for _, logGroupMetaData := range cwl {
addStringSlice(logGroupMetaData.Arn, conventions.AttributeAWSLogGroupARNs, attrs)
addStringSlice(logGroupMetaData.LogGroup, conventions.AttributeAWSLogGroupNames, attrs)
}
}

if ec2 := aws.EC2; ec2 != nil {
addString(ec2.AvailabilityZone, conventions.AttributeCloudAvailabilityZone, attrs)
addString(ec2.InstanceID, conventions.AttributeHostID, attrs)
Expand All @@ -51,7 +58,6 @@ func addAWSToResource(aws *awsxray.AWSData, attrs pcommon.Map) {
addString(eks.ContainerID, conventions.AttributeContainerID, attrs)
addString(eks.ClusterName, conventions.AttributeK8SClusterName, attrs)
addString(eks.Pod, conventions.AttributeK8SPodName, attrs)

}
}

Expand Down
119 changes: 119 additions & 0 deletions receiver/awsxrayreceiver/internal/translator/aws_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package translator

import (
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/pdata/pcommon"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"

awsxray "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/xray"
)

func TestAddAWSToResource(t *testing.T) {
testCases := map[string]struct {
input *awsxray.AWSData
want map[string]any
}{
"WithNil": {
want: map[string]any{
conventions.AttributeCloudProvider: "unknown",
},
},
"WithCloudWatchLogs": {
input: &awsxray.AWSData{
CWLogs: []awsxray.LogGroupMetadata{
{
LogGroup: aws.String("<log-group-1>"),
Arn: aws.String("arn:aws:logs:<region>:<account>:log-group:<log-group-1>:*"),
},
{
LogGroup: aws.String("<log-group-2>"),
Arn: aws.String("arn:aws:logs:<region>:<account>:log-group:<log-group-2>:*"),
},
},
},
want: map[string]any{
conventions.AttributeCloudProvider: conventions.AttributeCloudProviderAWS,
conventions.AttributeAWSLogGroupARNs: []any{
"arn:aws:logs:<region>:<account>:log-group:<log-group-1>:*",
"arn:aws:logs:<region>:<account>:log-group:<log-group-2>:*",
},
conventions.AttributeAWSLogGroupNames: []any{"<log-group-1>", "<log-group-2>"},
},
},
"WithEC2": {
input: &awsxray.AWSData{
EC2: &awsxray.EC2Metadata{
InstanceID: aws.String("<instance-id>"),
AvailabilityZone: aws.String("<ec2-az>"),
InstanceSize: aws.String("<instance-size>"),
AmiID: aws.String("<ami>"),
},
},
want: map[string]any{
conventions.AttributeCloudProvider: conventions.AttributeCloudProviderAWS,
conventions.AttributeCloudAvailabilityZone: "<ec2-az>",
conventions.AttributeHostID: "<instance-id>",
conventions.AttributeHostType: "<instance-size>",
conventions.AttributeHostImageID: "<ami>",
},
},
"WithECS": {
input: &awsxray.AWSData{
ECS: &awsxray.ECSMetadata{
ContainerName: aws.String("<container-name>"),
ContainerID: aws.String("<ecs-container-id>"),
AvailabilityZone: aws.String("<ecs-az>"),
},
},
want: map[string]any{
conventions.AttributeCloudProvider: conventions.AttributeCloudProviderAWS,
conventions.AttributeCloudAvailabilityZone: "<ecs-az>",
conventions.AttributeContainerName: "<container-name>",
conventions.AttributeContainerID: "<ecs-container-id>",
},
},
"WithEKS": {
input: &awsxray.AWSData{
EKS: &awsxray.EKSMetadata{
ClusterName: aws.String("<cluster-name>"),
Pod: aws.String("<pod>"),
ContainerID: aws.String("<eks-container-id>"),
},
},
want: map[string]any{
conventions.AttributeCloudProvider: conventions.AttributeCloudProviderAWS,
conventions.AttributeK8SPodName: "<pod>",
conventions.AttributeK8SClusterName: "<cluster-name>",
conventions.AttributeContainerID: "<eks-container-id>",
},
},
"WithBeanstalk": {
input: &awsxray.AWSData{
Beanstalk: &awsxray.BeanstalkMetadata{
Environment: aws.String("<environment>"),
VersionLabel: aws.String("<version-label>"),
DeploymentID: aws.Int64(1),
},
},
want: map[string]any{
conventions.AttributeCloudProvider: conventions.AttributeCloudProviderAWS,
conventions.AttributeServiceNamespace: "<environment>",
conventions.AttributeServiceInstanceID: "1",
conventions.AttributeServiceVersion: "<version-label>",
},
},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
attrs := pcommon.NewMap()
addAWSToResource(testCase.input, attrs)
assert.Equal(t, testCase.want, attrs.AsRaw())
})
}
}

0 comments on commit da405ec

Please sign in to comment.