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)
}