2

I am trying to use spring cloud sleuth with spring web mvc project. We are sending the request from spring web mvc to another spring boot project. I need to have the traceID and spanId to display in the UI. How can we achieve this?

EDIT

POM.xml for the spring boot application. I am using the latest version of Spring Boot and added the dependency of spring cloud sleuth.

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-sleuth</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Web MVC code: I am using spring rest template and calling the spring boot service from the spring web mvc application.

@Controller
public class HelloController {

    @Autowired
    RestTemplate restTemplate;

    private Logger logger = Logger.getLogger(HelloController.class);

    @GetMapping("/hello")
    public String hello(Model model) {
        logger.info("Hello from controller");
        ResponseEntity<String> entity = restTemplate.getForEntity("http://localhost:8092/test", String.class);
        String response = entity.getBody();
        logger.info("Response from Service: "+ response);
        model.addAttribute("name", "Test");
        return "welcome";
    }
}
Jiten Vij
  • 23
  • 4

1 Answers1

0

In order to do that, you need to inject the Tracer bean and then if you call tracer.currentSpan().context().traceIdString() and tracer.currentSpan().context().spanIdString().

Marcin Grzejszczak
  • 10,624
  • 1
  • 16
  • 32
  • Can we use this trace_id and span_id while tracing the logs? I mean we need to have a correlation ID which we have use in UI (like zipkin) – Jiten Vij Jan 24 '20 at 08:56
  • Excuse me but I don't understand the problem. If you use Sleuth you get full log correlation, the logs do contain trace id and span id. Have you tried using this before asking the question? Or you did use it and you don't see the trace id and span id in the logs? Have you changed the default logging format? In general if you just add starter-sleuth to the classpath you get the logging correlation out of the box. – Marcin Grzejszczak Jan 24 '20 at 09:44
  • Yes, I have tried using Sleuth but the actual problem is that I am not getting trace/span-id in spring MVC project even after changing the format of logs. I am able to see the correlation id in spring boot project but not in spring MVC project. – Jiten Vij Jan 24 '20 at 09:50
  • Sleuth will not work without spring boot. You can use project Brave directly instead – Marcin Grzejszczak Jan 24 '20 at 10:19
  • Do I need to configure Brave in both spring MVC and spring Boot application? – Jiten Vij Jan 24 '20 at 10:21
  • No. Sleuth is a boot based library so it will work out of the box. With a pure spring app you need to add brave and set it up yourself – Marcin Grzejszczak Jan 24 '20 at 10:23