0

I recently switched to RestClient from RestTemplate and using SpringBoot 3.2.7 and Java 21 in my project. I noticed when microservice1 is calling microservice2 then the TraceId is not getting propagated. Shouldnt it be out-of-the-box or needs some tweak? This is my RestClient. Please advise.

 @Bean(name = "restClient1")
    public RestClient restClient() {
        return RestClient
                .builder()
                .baseUrl("http://localhost:9100/v3/rest")
                .defaultHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
                .build();
    }

2 Answers 2

0

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";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Based on the spring doc, you have to define a @Bean for RestClient.Builder for auto instrument with java agent

@Bean
public RestClient.Builder restClientBuilder() {
    return RestClient.builder()
            .defaultRequest(new TraceInterceptor());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.