Can not observe TraceId & SpanId in Spring Cloud Sleuth

6.8k Views Asked by At

I am trying to run Spring Cloud Sleuth and observe traceid, span id oin logs.

I have configured as below but when i call a requesti cnat see any traceId or logId in logs.

Is there anyone help with this. Thanks.

2020-12-02 11:40:02 [main] INFO  az.iba.ms.chasis.ChasisApplication - Started ChasisApplication in 21.425 seconds (JVM running for 23.816)
2020-12-02 11:40:03 [RMI TCP Connection(2)-172.31.109.104] INFO  o.a.c.c.C.[.[localhost].[/chasis-ms] - Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-12-02 11:40:03 [RMI TCP Connection(2)-172.31.109.104] INFO  o.s.web.servlet.DispatcherServlet - Initializing Servlet 'dispatcherServlet'
2020-12-02 11:40:03 [RMI TCP Connection(2)-172.31.109.104] INFO  o.s.web.servlet.DispatcherServlet - Completed initialization in 24 ms
2020-12-02 11:40:17 [http-nio-8081-exec-1] INFO  a.i.m.c.controller.ChasisController - Request {}helloChasis from chasis-ms

build.gradle

compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-sleuth', version: '2.2.6.RELEASE'

compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-zipkin', version: '2.2.6.RELEASE'

Controller.java

package az.iba.ms.chasis.controller;

import az.iba.ms.chasis.entity.Chasis;
import az.iba.ms.chasis.logger.MainLogger;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.log4j.Log4j2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.beans.factory.annotation.Autowired;

@RestController
@RequestMapping(value = "/v1")
@Log4j2
@Api(produces = MediaType.APPLICATION_JSON_VALUE, tags = "Chasis microservice")
public class ChasisController {

    private static final MainLogger LOGGER = MainLogger.getLogger(ChasisController.class);

    private static final Logger LOG = LoggerFactory.getLogger(ChasisController.class);


    @Autowired
    private RestTemplate restTemplate;

    @ApiOperation(value = "View a list of accounts for given CIF list", response = Chasis.class)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Successfully retrieved message"),
            @ApiResponse(code = 404, message = "The resource is not found")}
    )
    @GetMapping("/hello")
    public String helloChasis() {
        LOG.info("Request {}" + "helloChasis from chasis-ms");
        return "Greetings from Chasis";
    }

}
3

There are 3 best solutions below

0
On

If I need to guess, this is caused by some custom Log4J settings you have, more precisely your Log4J pattern (I don't see them so I'm just guessing). Spring Cloud Sleuth relies on log patterns that is setup by Spring Boot out of the box (see: logging config).

I suggest you to give it a try and use the default config first and if it works, you can customize the default pattern (I don't recommend customizing it, the defaults are pretty good).

0
On

In the new Spring Cloud Sleuth 3.1, the API is migrated from Sleuth to Micrometer Tracing. So even in sprint initializr, you will see zipkin and micrometer dependencies (not Sleuth).

Read the official migration guide.

To enable tracing and Zipkin ready in a distributed system, You must have:

application.properties

spring.application.name=microservice1
logging.pattern.level="%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]"

pom.xml

...
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-tracing-bridge-brave</artifactId>
    </dependency>
    <dependency>
        <groupId>io.zipkin.reporter2</groupId>
        <artifactId>zipkin-reporter-brave</artifactId>
    </dependency>
</dependencies>
...

MyController.java

@RestController
public class MyController {

    private Logger logger = LoggerFactory.getLogger(MyController.class);

    @Bean
    public Sampler alwaysSampler() {
        return Sampler.ALWAYS_SAMPLE;
    }

    @GetMapping("/microservice1")
    public String method1(){
        logger.info("in method1");
        logger.info("out of method1");
        return "hellooooo";
    }
}
0
On

You will need to add configuration to let your log-provider know that you want to log these additional fields. Sample logback.xml should look like following:-

<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} traceId: %X{traceId} spanId: %X{spanId} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="INFO">
    <appender-ref ref="STDOUT" />
    </root>
</configuration>

Spring-cloud sleuth automatically adds the properties traceId and spanId in the MDC of the log-provider you are using