func getToken(imdsURL string, client *http.Client) (string, error) {
	ttlHeader := "X-aws-ec2-metadata-token-ttl-seconds"
	defaultTTL := "60"
	tokenURL := imdsURL + "api/token"
	req, _ := http.NewRequest("PUT", tokenURL, nil)
	req.Header.Add(ttlHeader, defaultTTL)
	resp, err := client.Do(req)
	if err != nil {
		return "", err
	}
	buf := new(bytes.Buffer)
	if _, err := buf.ReadFrom(resp.Body); err != nil {
		logger.Errorf("Error while reading data from response buffer: %v", err)
		return "", err
	}
	token := buf.String()
	return token, err
}
getToken fetches token to fetch EC2 metadata
package ec2
import (
	"bytes"
	"encoding/json"
	"net/http"
	"github.com/aws/aws-xray-sdk-go/internal/logger"
	"github.com/aws/aws-xray-sdk-go/internal/plugins"
)
const Origin = "AWS::EC2::Instance"
type metadata struct {
	AvailabilityZone string
	ImageID          string
	InstanceID       string
	InstanceType     string
}
func Init() {
	if plugins.InstancePluginMetadata != nil && plugins.InstancePluginMetadata.EC2Metadata == nil {
		addPluginMetadata(plugins.InstancePluginMetadata)
	}
}
func addPluginMetadata(pluginmd *plugins.PluginMetadata) {
	var instanceData metadata
	imdsURL := "http://169.254.169.254/latest/"
	client := &http.Client{
		Transport: http.DefaultTransport,
	}
	token, err := getToken(imdsURL, client)
	if err != nil {
		logger.Debugf("Unable to fetch EC2 instance metadata token fallback to IMDS V1: %v", err)
	}
	resp, err := getMetadata(imdsURL, client, token)
	if err != nil {
		logger.Errorf("Unable to read EC2 instance metadata: %v", err)
		return
	}
	buf := new(bytes.Buffer)
	if _, err := buf.ReadFrom(resp.Body); err != nil {
		logger.Errorf("Error while reading data from response buffer: %v", err)
		return
	}
	metadata := buf.String()
	if err := json.Unmarshal([]byte(metadata), &instanceData); err != nil {
		logger.Errorf("Error while unmarshal operation: %v", err)
		return
	}
	pluginmd.EC2Metadata = &plugins.EC2Metadata{InstanceID: instanceData.InstanceID, AvailabilityZone: instanceData.AvailabilityZone}
	pluginmd.Origin = Origin
}