So, if you don`t use any external dependencies for tracing, you should implement this logic by yourself.
Just an simple example how to forward header with correlation information:
First, add interceptor to your RestClient
@Configuration
public class AppConfig {
@Bean(name = "myLocalhostRestClient")
public RestClient restClient() {
return RestClient.builder()
.baseUrl("http://localhost:8080")
.requestInterceptor((request, body, execution) -> {
var correlationId = MDC.get("X-Correlation-Id");
request.getHeaders().add("X-Correlation-Id", correlationId);
return execution.execute(request, body);
})
.build();
}
}
Than you can use it like this:
@RestController
public class Controller {
private final RestClient localhostRestClient;
public Controller(RestClient localhostRestClient) {
this.localhostRestClient = localhostRestClient;
}
@GetMapping
public String hello(@RequestHeader(name = "X-Correlation-ID", required = false) String correlationId) {
if (!StringUtils.hasText(correlationId)) {
System.out.println("this is a first request in a chain. generate correlation header by myself");
correlationId = UUID.randomUUID().toString();
}
MDC.put("X-Correlation-Id", correlationId);
var greetingResponse = localhostRestClient.get()
.uri("/greeting")
.retrieve()
.body(String.class);
return "Hello " + greetingResponse.toString();
}
@GetMapping("/greeting")
public String greeting(@RequestHeader(name = "X-Correlation-ID") String correlationId) {
System.out.println("Recieved correlation ID: " + correlationId);
return "Some random Person";
}
}