Skip to content

Instantly share code, notes, and snippets.

@giljae
Created November 28, 2018 04:45
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 giljae/7ea7c9ff3b12b65a26cf24ac3f3600f9 to your computer and use it in GitHub Desktop.
Save giljae/7ea7c9ff3b12b65a26cf24ac3f3600f9 to your computer and use it in GitHub Desktop.
@RestController
public class HelloController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "/hello")
public String hello() {
return "Hello from Spring Boot!";
}
@RequestMapping("/chaining")
public String chaining(@RequestHeader HttpHeaders headers) {
HttpHeaders tracingHeaders = new HttpHeaders();
extractHeader(headers, tracingHeaders, "x-request-id");
extractHeader(headers, tracingHeaders, "x-b3-traceid");
extractHeader(headers, tracingHeaders, "x-b3-spanid");
extractHeader(headers, tracingHeaders, "x-b3-parentspanid");
extractHeader(headers, tracingHeaders, "x-b3-sampled");
extractHeader(headers, tracingHeaders, "x-b3-flags");
extractHeader(headers, tracingHeaders, "x-ot-span-context");
ResponseEntity<String> response = restTemplate
.exchange("http://spring-boot:8080/hello", HttpMethod.GET, new HttpEntity<>(tracingHeaders), String.class);
return "Chaining + " + response.getBody();
}
private static void extractHeader(HttpHeaders headers, HttpHeaders extracted, String key) {
List<String> vals = headers.get(key);
if (vals != null && !vals.isEmpty()) {
extracted.put(key, Arrays.asList(vals.get(0)));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment