func makeXRayAttributes()

in exporter/awsxrayexporter/internal/translator/segment.go [603:707]


func makeXRayAttributes(attributes map[string]pcommon.Value, resource pcommon.Resource, storeResource bool, indexedAttrs []string, indexAllAttrs bool) (
	string, map[string]any, map[string]map[string]any,
) {
	var (
		annotations = map[string]any{}
		metadata    = map[string]map[string]any{}
		user        string
	)
	userid, ok := attributes[conventionsv112.AttributeEnduserID]
	if ok {
		user = userid.Str()
		delete(attributes, conventionsv112.AttributeEnduserID)
	}

	if len(attributes) == 0 && (!storeResource || resource.Attributes().Len() == 0) {
		return user, nil, nil
	}

	defaultMetadata := map[string]any{}

	indexedKeys := map[string]bool{}
	if !indexAllAttrs {
		for _, name := range indexedAttrs {
			indexedKeys[name] = true
		}
	}

	annotationKeys, ok := attributes[awsxray.AWSXraySegmentAnnotationsAttribute]
	if ok && annotationKeys.Type() == pcommon.ValueTypeSlice {
		slice := annotationKeys.Slice()
		for i := 0; i < slice.Len(); i++ {
			value := slice.At(i)
			if value.Type() != pcommon.ValueTypeStr {
				continue
			}
			key := value.AsString()
			indexedKeys[key] = true
		}
		delete(attributes, awsxray.AWSXraySegmentAnnotationsAttribute)
	}

	if storeResource {
		for key, value := range resource.Attributes().All() {
			key = "otel.resource." + key
			annoVal := annotationValue(value)
			indexed := indexAllAttrs || indexedKeys[key]
			if annoVal != nil && indexed {
				key = fixAnnotationKey(key)
				annotations[key] = annoVal
			} else {
				metaVal := value.AsRaw()
				if metaVal != nil {
					defaultMetadata[key] = metaVal
				}
			}
		}
	}

	if indexAllAttrs {
		for key, value := range attributes {
			key = fixAnnotationKey(key)
			annoVal := annotationValue(value)
			if annoVal != nil {
				annotations[key] = annoVal
			}
		}
	} else {
		for key, value := range attributes {
			switch {
			case indexedKeys[key]:
				key = fixAnnotationKey(key)
				annoVal := annotationValue(value)
				if annoVal != nil {
					annotations[key] = annoVal
				}
			case strings.HasPrefix(key, awsxray.AWSXraySegmentMetadataAttributePrefix) && value.Type() == pcommon.ValueTypeStr:
				namespace := strings.TrimPrefix(key, awsxray.AWSXraySegmentMetadataAttributePrefix)
				var metaVal map[string]any
				err := json.Unmarshal([]byte(value.Str()), &metaVal)
				switch {
				case err != nil:
					// if unable to unmarshal, keep the original key/value
					defaultMetadata[key] = value.Str()
				case strings.EqualFold(namespace, defaultMetadataNamespace):
					for k, v := range metaVal {
						defaultMetadata[k] = v
					}
				default:
					metadata[namespace] = metaVal
				}
			default:
				metaVal := value.AsRaw()
				if metaVal != nil {
					defaultMetadata[key] = metaVal
				}
			}
		}
	}

	if len(defaultMetadata) > 0 {
		metadata[defaultMetadataNamespace] = defaultMetadata
	}

	return user, annotations, metadata
}