springspring-bootmicroservicesspring-cloud-sleuthzipkin

TraceID for a request is not travelling between services


I am calling a microservice from a microservice and expecting the traceID (given by sleuth) initiated from base service should travel to the called services as implanted sleuth with zipkin.

Here is the call to the service

RestTemplate restTemplate = new RestTemplate();
logger.info("ApplicationController: controllerMessage() called: " + properties.getType() +
                " " + properties2.getMode());
String uri = "http://localhost:8089/poc1/message";
//String uri = "http://google.com";
        
        
HttpHeaders headers = new HttpHeaders();
//headers.add("Authorization", authToken);
HttpEntity request = new HttpEntity(headers);
        
ResponseEntity<String> response = restTemplate.exchange(
                uri,
                HttpMethod.GET,
                request,
                String.class
            );

Expecting that the same traceID printed in the logs of above service will be printed in the logs of called service. In this case, same trace id should print in the logs of http://localhost:8089/poc1/message. However this is not happening.

Using 2.7.5 version of spring boot and 2021.0.4 of spring cloud.

Any clue what is wrong here?

Expecting the same traceID generated by the initial request should print in the logs of called service.


Solution

  • You're creating RestTemplate via new. Please create it as a bean and inject it to your code.

    @Bean
    RestTemplate restTemplate() {
      return new RestTemplate();
    }
    

    We're writing it in the docs with a big exclamation mark here https://docs.spring.io/spring-cloud-sleuth/docs/current/reference/html/integrations.html#sleuth-http-client-rest-template-integration

    Let me paste that for your convenience

    You have to register RestTemplate as a bean so that the interceptors get injected. If you create a RestTemplate instance with a new keyword, the instrumentation does NOT work.