Skip to content

Instantly share code, notes, and snippets.

@moderation
Last active June 6, 2017 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moderation/481f1fe5e943656d896479922b772189 to your computer and use it in GitHub Desktop.
Save moderation/481f1fe5e943656d896479922b772189 to your computer and use it in GitHub Desktop.

First create a map to propagate headers and then variables to hold the header names and the header values.

prop             map[string]string
headern          = []string{"l5dctxtrace", "xrequestidid", "xotspancontextontext", "xb3traceid", "xb3spanid", "xb3parentspanid", "xb3sampled", "xb3flags"}
headerv          = []string{"l5d-ctx-trace", "x-request-id", "x-ot-span-context", "x-b3-traceid", "x-b3-spanid", "x-b3-parentspanid", "x-b3-sampled", "x-b3-flags"}

In the function that receives the inbound HTTP request, capture the headers (passed by Envoy)

prop = make(map[string]string)
for i <= len(headern)-1 {
	prop[headern[i]] = req.Header.Get(headerv[i])
	i = i + 1
}

In the function that makes the HTTP call to the next service, propogate the headers.

for i <= len(headern)-1 {
	if prop[headern[i]] != "" {
		req.Header.Set(headerv[i], prop[headern[i]])
		log.Println("#### getJSON " + headerv[i] + ": " + prop[headern[i]])
	}
	i = i + 1
}

As you are creating HTTP requests with custom headers you need to make your call using http.Client. Sample code below.

var (
	err  error
	resp *http.Response
	req  *http.Request
)

client := &http.Client{}

req, err = http.NewRequest("GET", urlStr+"?"+params.Encode(), nil)
if err != nil {
	fmt.Println(err)
}

resp, err = client.Do(req)
if err != nil {
	log.Print(err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment