1

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";
    }

}
  • Have you set the sampler probability to 1.0 to ensure that your endpoint gets sampled? You could try sending 10 requests to see that 1 of them is actually traced. We describe this behaviour in the documentation. – Marcin Grzejszczak Dec 02 '20 at 09:16
  • I have changed setting to sleuth.sampler.probability: 1.0 but none of the logs changed –  Dec 02 '20 at 09:37
  • I don't see your code - what you're doing is absolutely basic stuff so that has to work fine. Maybe you've changed the logging format, maybe you have a mismatch of dependencies (I already see that instead of a BOM you're using hardcoded versions). If you go to start.spring.io, generate a fresh project with sleuth on the classpath things will work out of the box. – Marcin Grzejszczak Dec 02 '20 at 09:39
  • 1
    Hey @MarcinGrzejszczak I asked similar question, I have my config file there but still have same problem as user that asked this question. You can check my question here https://stackoverflow.com/questions/65688048/cant-see-traceid-and-spanid-in-log-for-sleuth – laban_luka Jan 12 '21 at 17:34

3 Answers3

2

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";
    }
}
Sreekant Shenoy
  • 1,420
  • 14
  • 23
1

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).

Jonatan Ivanov
  • 4,895
  • 2
  • 15
  • 30
1

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

Sumit Desai
  • 1,542
  • 9
  • 22