Skip to content

Commit

Permalink
support aws plugins EC2/ECS/Beanstalk
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzlei committed Jun 18, 2020
1 parent cb734dc commit 5f2c39c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
14 changes: 14 additions & 0 deletions exporter/awsxrayexporter/translator/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ type AWSData struct {
type EC2Metadata struct {
InstanceID string `json:"instance_id"`
AvailabilityZone string `json:"availability_zone"`
InstanceSize string `json:"instance_size"`
AmiId string `json:"ami_id"`
}

// ECSMetadata provides the shape for unmarshalling ECS metadata.
Expand All @@ -74,9 +76,12 @@ func makeAws(attributes map[string]string, resource pdata.Resource) (map[string]
account string
zone string
hostID string
hostType string
amiId string
container string
namespace string
deployID string
versionLabel string
operation string
remoteRegion string
requestID string
Expand All @@ -99,6 +104,10 @@ func makeAws(attributes map[string]string, resource pdata.Resource) (map[string]
zone = value.StringVal()
case semconventions.AttributeHostID:
hostID = value.StringVal()
case semconventions.AttributeHostType:
hostType = value.StringVal()
case semconventions.AttributeHostImageID:
amiId = value.StringVal()
case semconventions.AttributeContainerName:
if container == "" {
container = value.StringVal()
Expand All @@ -109,6 +118,8 @@ func makeAws(attributes map[string]string, resource pdata.Resource) (map[string]
namespace = value.StringVal()
case semconventions.AttributeServiceInstance:
deployID = value.StringVal()
case semconventions.AttributeServiceVersion:
versionLabel = value.StringVal()
}
})
}
Expand Down Expand Up @@ -148,6 +159,8 @@ func makeAws(attributes map[string]string, resource pdata.Resource) (map[string]
ec2 = &EC2Metadata{
InstanceID: hostID,
AvailabilityZone: zone,
InstanceSize: hostType,
AmiId: amiId,
}
}
if container != "" {
Expand All @@ -163,6 +176,7 @@ func makeAws(attributes map[string]string, resource pdata.Resource) (map[string]
ebs = &BeanstalkMetadata{
Environment: namespace,
DeploymentID: deployNum,
VersionLabel: versionLabel,
}
}
awsData := &AWSData{
Expand Down
11 changes: 9 additions & 2 deletions exporter/awsxrayexporter/translator/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ import (

func TestAwsFromEc2Resource(t *testing.T) {
instanceID := "i-00f7c0bcb26da2a99"
hostType := "m5.xlarge"
imageId := "ami-0123456789"
resource := pdata.NewResource()
resource.InitEmpty()
attrs := pdata.NewAttributeMap()
attrs.InsertString(semconventions.AttributeCloudProvider, "aws")
attrs.InsertString(semconventions.AttributeCloudAccount, "123456789")
attrs.InsertString(semconventions.AttributeCloudZone, "us-east-1c")
attrs.InsertString(semconventions.AttributeHostID, instanceID)
attrs.InsertString(semconventions.AttributeHostType, "m5.xlarge")
attrs.InsertString(semconventions.AttributeHostType, hostType)
attrs.InsertString(semconventions.AttributeHostImageID, imageId)
attrs.CopyTo(resource.Attributes())

attributes := make(map[string]string)
Expand All @@ -47,6 +50,8 @@ func TestAwsFromEc2Resource(t *testing.T) {
assert.Equal(t, &EC2Metadata{
InstanceID: instanceID,
AvailabilityZone: "us-east-1c",
InstanceSize: hostType,
AmiId: imageId,
}, awsData.EC2Metadata)
}

Expand Down Expand Up @@ -86,6 +91,7 @@ func TestAwsFromEcsResource(t *testing.T) {

func TestAwsFromBeanstalkResource(t *testing.T) {
deployID := "232"
versionLabel := "4"
resource := pdata.NewResource()
resource.InitEmpty()
attrs := pdata.NewAttributeMap()
Expand All @@ -94,6 +100,7 @@ func TestAwsFromBeanstalkResource(t *testing.T) {
attrs.InsertString(semconventions.AttributeCloudZone, "us-east-1c")
attrs.InsertString(semconventions.AttributeServiceNamespace, "production")
attrs.InsertString(semconventions.AttributeServiceInstance, deployID)
attrs.InsertString(semconventions.AttributeServiceVersion, versionLabel)
attrs.CopyTo(resource.Attributes())

attributes := make(map[string]string)
Expand All @@ -107,7 +114,7 @@ func TestAwsFromBeanstalkResource(t *testing.T) {
assert.NotNil(t, awsData.BeanstalkMetadata)
assert.Equal(t, &BeanstalkMetadata{
Environment: "production",
VersionLabel: "",
VersionLabel: versionLabel,
DeploymentID: 232,
}, awsData.BeanstalkMetadata)
}
Expand Down
16 changes: 10 additions & 6 deletions exporter/awsxrayexporter/translator/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,19 @@ func newSegmentID() pdata.SpanID {
}

func determineAwsOrigin(resource pdata.Resource) string {
origin := OriginEC2
// EB > ECS > EC2
if resource.IsNil() {
return origin
return OriginEC2
}
_, ok := resource.Attributes().Get(semconventions.AttributeContainerName)
if ok {
origin = OriginECS
_, eb := resource.Attributes().Get(semconventions.AttributeServiceInstance)
if eb {
return OriginEB
}
_, ecs := resource.Attributes().Get(semconventions.AttributeContainerName)
if ecs {
return OriginECS
}
return origin
return OriginEC2
}

// convertToAmazonTraceID converts a trace ID to the Amazon format.
Expand Down

0 comments on commit 5f2c39c

Please sign in to comment.