func fetchInstanceID() (string, error) {
	var err error
	var instanceID string

	config, _ := appconfig.Config(false)
	if config.Agent.ContainerMode {
		container := &containers.Container{}
		targetID, err := container.TargetID()
		infoArray := strings.Split(targetID, "_")
		containerID := infoArray[len(infoArray)-1]
		return containerID, err
	}

	// trying to get instance id from managed instance registration
	if instanceID = managedInstance.InstanceID(); instanceID != "" {
		return instanceID, nil
	}

	// trying to get instance id from ec2 metadata
	if instanceID, err = metadata.GetMetadata("instance-id"); instanceID != "" && err == nil {
		return instanceID, nil
	}

	// return combined error messages
	return "", fmt.Errorf(errorMessage, "instance ID", err)
}
// fetchInstanceID fetches the instance id with the following preference order.
// 1. managed instance registration
// 2. EC2 Instance Metadata
fetchInstanceID fetches the instance id with the following preference order.
1. managed instance registration
2. EC2 Instance Metadata
// fetchInstanceID fetches the instance id with the following preference order . // 1 . managed instance registration // 2 . EC2 Instance Metadata
func fetchInstanceID() (string, error) {
	var err error
	var instanceID string
	config, _ := appconfig.Config(false)
	if config.Agent.ContainerMode {
		container := &containers.Container{}
		targetID, err := container.TargetID()
		infoArray := strings.Split(targetID, "_")
		containerID := infoArray[len(infoArray)-1]
		return containerID, err
	}
	if instanceID = managedInstance.InstanceID(); instanceID != "" {
		return instanceID, nil
	}
	if instanceID, err = metadata.GetMetadata("instance-id"); instanceID != "" && err == nil {
		return instanceID, nil
	}
	return "", fmt.Errorf(errorMessage, "instance ID", err)
}
func fetchInstanceID ( ) ( string , error ) { var err error var instanceID string config , _ := appconfig . Config ( false ) if config . Agent . ContainerMode { container := & containers . Container { } targetID , err := container . TargetID ( ) infoArray := strings . Split ( targetID , " " ) containerID := infoArray [ len ( infoArray ) - 1 ] return containerID , err } if instanceID = managedInstance . InstanceID ( ) ; instanceID != " " { return instanceID , nil } if instanceID , err = metadata . GetMetadata ( " " ) ; instanceID != " " && err == nil { return instanceID , nil } return " " , fmt . Errorf ( errorMessage , " " , err ) }
fetchInstanceID fetches the instance id with the following preference order.
fetchInstanceID fetches the instance id with the following preference order .
// trying to get instance id from managed instance registration // trying to get instance id from ec2 metadata // return combined error messages