Created
November 28, 2018 04:45
-
-
Save giljae/7ea7c9ff3b12b65a26cf24ac3f3600f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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