· 6 years ago · Mar 17, 2020, 10:10 PM
1public string Get(int id)
2 {
3 // pull data out of http context
4 List<string> traceParent = HttpContext.Current.Request.Headers["traceparent"].ToString().Split('-').ToList();
5 List<string> traceState = HttpContext.Current.Request.Headers["tracestate"].ToString().Split(',').ToList();
6
7 // parse out specific values
8 string traceVersion = traceParent.Count == 0 ? "00" : traceParent[0];
9 string traceId = traceParent.Count == 0 ? "00000000000000000000000000000000" : traceParent[1];
10 string traceParentId = traceParent.Count == 0 ? "0000000000000000" : traceParent[2];
11 string traceFlags = traceParent.Count == 0 ? "00" : traceParent[3];
12
13 // remove existing vendor key in trace state
14 traceState.RemoveAll(x => x.ToLower().StartsWith("jake"));
15 // insert new vendor key value in first location of trace state
16 traceState.Insert(0, "jake=" + traceParentId);
17
18 // get new trace id to forward to the next process
19 string thisTraceId = "0102030405060708";
20
21 // restructure the data for http context
22 string tp = traceVersion + "-" + traceId + "-" + thisTraceId + "-" + traceFlags;
23 string ts = "";
24 traceState.ForEach(x => ts += x + ",");
25 ts = ts.Remove(ts.LastIndexOf(','));
26
27
28 // make next call with the http context included.
29 HttpClient client = new HttpClient();
30
31 // set http header data for the trace parent and state
32 client.DefaultRequestHeaders.Add("traceparent", tp);
33 client.DefaultRequestHeaders.Add("tracestate", ts);
34
35 // call subprocess
36 HttpResponseMessage response = client.GetAsync($@"https://localhost:44347/api/values/get").Result;
37
38
39 return "new TP: " + tp + " new TS: " + ts + " Which was passed to the sub api call.";
40 }