func makeAws()

in exporter/awsxrayexporter/internal/translator/aws.go [18:273]


func makeAws(attributes map[string]pcommon.Value, resource pcommon.Resource, logGroupNames []string) (map[string]pcommon.Value, *awsxray.AWSData) {
	var (
		cloud        string
		service      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
		queueURL     string
		tableName    string
		tableNames   []string
		sdk          string
		sdkName      string
		sdkLanguage  string
		sdkVersion   string
		autoVersion  string
		containerID  string
		clusterName  string
		podUID       string
		clusterArn   string
		containerArn string
		taskArn      string
		taskFamily   string
		launchType   string
		logGroups    pcommon.Slice
		logGroupArns pcommon.Slice
		cwl          []awsxray.LogGroupMetadata
		ec2          *awsxray.EC2Metadata
		ecs          *awsxray.ECSMetadata
		ebs          *awsxray.BeanstalkMetadata
		eks          *awsxray.EKSMetadata
	)

	filtered := make(map[string]pcommon.Value)
	for key, value := range resource.Attributes().All() {
		switch key {
		case conventionsv112.AttributeCloudProvider:
			cloud = value.Str()
		case conventionsv112.AttributeCloudPlatform:
			service = value.Str()
		case conventionsv112.AttributeCloudAccountID:
			account = value.Str()
		case conventionsv112.AttributeCloudAvailabilityZone:
			zone = value.Str()
		case conventionsv112.AttributeHostID:
			hostID = value.Str()
		case conventionsv112.AttributeHostType:
			hostType = value.Str()
		case conventionsv112.AttributeHostImageID:
			amiID = value.Str()
		case conventionsv112.AttributeContainerName:
			if container == "" {
				container = value.Str()
			}
		case conventionsv112.AttributeK8SPodName:
			podUID = value.Str()
		case conventionsv112.AttributeServiceNamespace:
			namespace = value.Str()
		case conventionsv112.AttributeServiceInstanceID:
			deployID = value.Str()
		case conventionsv112.AttributeServiceVersion:
			versionLabel = value.Str()
		case conventionsv112.AttributeTelemetrySDKName:
			sdkName = value.Str()
		case conventionsv112.AttributeTelemetrySDKLanguage:
			sdkLanguage = value.Str()
		case conventionsv112.AttributeTelemetrySDKVersion:
			sdkVersion = value.Str()
		case conventionsv112.AttributeTelemetryAutoVersion, conventions.AttributeTelemetryDistroVersion:
			autoVersion = value.Str()
		case conventionsv112.AttributeContainerID:
			containerID = value.Str()
		case conventionsv112.AttributeK8SClusterName:
			clusterName = value.Str()
		case conventionsv112.AttributeAWSECSClusterARN:
			clusterArn = value.Str()
		case conventionsv112.AttributeAWSECSContainerARN:
			containerArn = value.Str()
		case conventionsv112.AttributeAWSECSTaskARN:
			taskArn = value.Str()
		case conventionsv112.AttributeAWSECSTaskFamily:
			taskFamily = value.Str()
		case conventionsv112.AttributeAWSECSLaunchtype:
			launchType = value.Str()
		case conventionsv112.AttributeAWSLogGroupNames:
			logGroups = normalizeToSlice(value)
		case conventionsv112.AttributeAWSLogGroupARNs:
			logGroupArns = normalizeToSlice(value)
		}
	}

	if awsOperation, ok := attributes[awsxray.AWSOperationAttribute]; ok {
		operation = awsOperation.Str()
	} else if rpcMethod, ok := attributes[conventionsv112.AttributeRPCMethod]; ok {
		operation = rpcMethod.Str()
	}

	for key, value := range attributes {
		switch key {
		case conventionsv112.AttributeRPCMethod:
			// Deterministically handled with if else above
		case awsxray.AWSOperationAttribute:
			// Deterministically handled with if else above
		case awsxray.AWSAccountAttribute:
			if value.Type() != pcommon.ValueTypeEmpty {
				account = value.Str()
			}
		case awsxray.AWSRegionAttribute:
			remoteRegion = value.Str()
		case awsxray.AWSRequestIDAttribute:
			fallthrough
		case awsxray.AWSRequestIDAttribute2:
			requestID = value.Str()
		case awsxray.AWSQueueURLAttribute:
			fallthrough
		case awsxray.AWSQueueURLAttribute2:
			queueURL = value.Str()
		case awsxray.AWSTableNameAttribute:
			fallthrough
		case awsxray.AWSTableNameAttribute2:
			tableName = value.Str()
		default:
			filtered[key] = value
		}
	}
	if cloud != conventionsv112.AttributeCloudProviderAWS && cloud != "" {
		return filtered, nil // not AWS so return nil
	}

	// Favor Semantic Conventions for specific SQS and DynamoDB attributes.
	if value, ok := attributes[conventionsv112.AttributeMessagingURL]; ok {
		queueURL = value.Str()
	}
	if value, ok := attributes[conventionsv112.AttributeAWSDynamoDBTableNames]; ok {
		switch value.Type() {
		case pcommon.ValueTypeSlice:
			if value.Slice().Len() == 1 {
				tableName = value.Slice().At(0).Str()
			} else if value.Slice().Len() > 1 {
				tableName = ""
				tableNames = []string{}
				for i := 0; i < value.Slice().Len(); i++ {
					tableNames = append(tableNames, value.Slice().At(i).Str())
				}
			}
		case pcommon.ValueTypeStr:
			tableName = value.Str()
		}
	}

	// EC2 - add ec2 metadata to xray request if
	//       1. cloud.platfrom is set to "aws_ec2" or
	//       2. there is an non-blank host/instance id found
	if service == conventionsv112.AttributeCloudPlatformAWSEC2 || hostID != "" {
		ec2 = &awsxray.EC2Metadata{
			InstanceID:       awsxray.String(hostID),
			AvailabilityZone: awsxray.String(zone),
			InstanceSize:     awsxray.String(hostType),
			AmiID:            awsxray.String(amiID),
		}
	}

	// ECS
	if service == conventionsv112.AttributeCloudPlatformAWSECS {
		ecs = &awsxray.ECSMetadata{
			ContainerName:    awsxray.String(container),
			ContainerID:      awsxray.String(containerID),
			AvailabilityZone: awsxray.String(zone),
			ContainerArn:     awsxray.String(containerArn),
			ClusterArn:       awsxray.String(clusterArn),
			TaskArn:          awsxray.String(taskArn),
			TaskFamily:       awsxray.String(taskFamily),
			LaunchType:       awsxray.String(launchType),
		}
	}

	// Beanstalk
	if service == conventionsv112.AttributeCloudPlatformAWSElasticBeanstalk && deployID != "" {
		deployNum, err := strconv.ParseInt(deployID, 10, 64)
		if err != nil {
			deployNum = 0
		}
		ebs = &awsxray.BeanstalkMetadata{
			Environment:  awsxray.String(namespace),
			DeploymentID: aws.Int64(deployNum),
			VersionLabel: awsxray.String(versionLabel),
		}
	}

	// EKS or native Kubernetes
	if service == conventionsv112.AttributeCloudPlatformAWSEKS || clusterName != "" {
		eks = &awsxray.EKSMetadata{
			ClusterName: awsxray.String(clusterName),
			Pod:         awsxray.String(podUID),
			ContainerID: awsxray.String(containerID),
		}
	}

	// Since we must couple log group ARNs and Log Group Names in the same CWLogs object, we first try to derive the
	// names from the ARN, then fall back to recording the names, if they do not exist in the resource
	// then pull from them from config.
	switch {
	case logGroupArns != (pcommon.Slice{}) && logGroupArns.Len() > 0:
		cwl = getLogGroupMetadata(logGroupArns, true)
	case logGroups != (pcommon.Slice{}) && logGroups.Len() > 0:
		cwl = getLogGroupMetadata(logGroups, false)
	case logGroupNames != nil:
		configSlice := pcommon.NewSlice()
		configSlice.EnsureCapacity(len(logGroupNames))

		for _, s := range logGroupNames {
			configSlice.AppendEmpty().SetStr(s)
		}

		cwl = getLogGroupMetadata(configSlice, false)
	}

	if sdkName != "" && sdkLanguage != "" {
		// Convention for SDK name for xray SDK information is e.g., `X-Ray SDK for Java`, `X-Ray for Go`.
		// We fill in with e.g, `opentelemetry for java` by using the conventionsv112
		sdk = sdkName + " for " + sdkLanguage
	} else {
		sdk = sdkName
	}

	xray := &awsxray.XRayMetaData{
		SDK:                 awsxray.String(sdk),
		SDKVersion:          awsxray.String(sdkVersion),
		AutoInstrumentation: aws.Bool(autoVersion != ""),
	}

	awsData := &awsxray.AWSData{
		AccountID:    awsxray.String(account),
		Beanstalk:    ebs,
		CWLogs:       cwl,
		ECS:          ecs,
		EC2:          ec2,
		EKS:          eks,
		XRay:         xray,
		Operation:    awsxray.String(operation),
		RemoteRegion: awsxray.String(remoteRegion),
		RequestID:    awsxray.String(requestID),
		QueueURL:     awsxray.String(queueURL),
		TableName:    awsxray.String(tableName),
		TableNames:   tableNames,
	}
	return filtered, awsData
}