Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring-cloud-sleuth does not show any info in logs after adding dependencies

I have added sleuth dependencies to my Spring boot project. Though the dependencies are present, no trace information is seen in logs. There are no compile or run time errors.

 <?xml version="1.0" encoding="UTF-8"?>
   <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>TestProject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>TestProject</name>
    <description>Test Project.</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath />
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.RC2</spring-cloud.version>
    </properties>

    <repositories>
        <repository>
            <id>ccp-master</id>
            <url>https://maven.teamccp.com/content/groups/master</url>
        </repository>

        <repository>
            <id>spring-release</id>
            <url>http://repo.spring.io/libs-release</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>

        <repository>
            <id>spring-milestone</id>
            <url>http://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>

        <repository>
            <id>spring-snapshot</id>
            <url>http://repo.spring.io/libs-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</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-cache</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>com.vaadin.external.google</groupId>
                    <artifactId>android-json</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- for jackson -->
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
        </dependency>

        <!-- Swagger Dependencies -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.2.2</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.2.2</version>
            <scope>compile</scope>
        </dependency>

        <!-- Hazelcast Dependencies -->
        <dependency>
            <groupId>javax.cache</groupId>
            <artifactId>cache-api</artifactId>
            <version>1.0.0</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>com.hazelcast</groupId>
            <artifactId>hazelcast-all</artifactId>
            <version>3.6.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>

        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.1</version>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
            <version>1.19.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.1</version>
        </dependency>

        <dependency>
            <groupId>com.comcast.sat</groupId>
            <artifactId>sat-core</artifactId>
            <version>1.3</version>
        </dependency>

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20160810</version>
        </dependency>

        <dependency>
            <groupId>com.github.ulisesbocchio</groupId>
            <artifactId>jasypt-spring-boot-starter</artifactId>
            <version>1.14</version>
        </dependency>

        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcpkix-jdk15on</artifactId>
            <version>1.48</version>
        </dependency>

        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15on</artifactId>
            <version>1.60</version>
        </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>
</project>

Heres the code for controller:

@RestController
@RequestMapping("/api/v1/device/")
public class TestProjectController {

private Logger logger = 
LoggerFactory.getLogger(this.getClass());

@Autowired
private TestProjectService tpService;

@RequestMapping(value = "/boottest", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<? extends Object> acquireLDAP(@RequestBody TestProjRequestModel onboardRequest,
        @RequestHeader HttpHeaders headers) {
    String traceId = UUID.randomUUID().toString();
    logger.info("traceId=" + traceId + "request=/boottest msg=Request to Onboard requestBody=" + onboardRequest);
    JsonEntity entity = new JsonEntity();
    try {
        ResponseEntity<? extends Object> responseEntity = new ResponseEntity<String>("test", HttpStatus.OK);
        HttpHeaders respHeaders =  responseEntity.getHeaders();
        for (Map.Entry<String, List<String>> entry : respHeaders.entrySet()) {
            String headerName = entry.getKey();
            for (String headerValue : entry.getValue()) {
                logger.info("Header Name::  " + headerName);
                logger.info("Header Val::  " + headerValue);
            }
        }
        return responseEntity;

Logs that I see as

   2019-01-08 12:22:01,400 INFO  (c.c.l.o.c.TestProjectController:52) | 

Please suggest what can be done to see the logs as expected? Expected logs are something like

2016-06-05 17:12:45.404  INFO [my-service3-id,73b62c0f90d11e06,73b62c0f90d11e06,false] 
like image 463
ERR Avatar asked Jan 08 '19 17:01

ERR


People also ask

Can we use sleuth without zipkin?

Only Sleuth (log correlation) If you want to use only Spring Cloud Sleuth without the Zipkin integration, add the spring-cloud-starter-sleuth module to your project. We recommend that you add the dependency management through the Spring BOM so that you need not manage versions yourself.

How does spring cloud sleuth work?

Spring Cloud Sleuth allows you to aggregate and track log entries as requests move through a distributed software system. In a monolithic system, it's relatively easy to track requests as they move through the codebase because all requests can easily be logged to the same log file.

Is zipkin deprecated?

spring-cloud-starter-zipkin is deprecated, you should not use it anymore. You can use spring-cloud-starter-sleuth and spring-cloud-sleuth-zipkin ( 3. x ).

How do I enable sleuth in spring boot?

Start out by going to start.spring.io and create a new Spring Boot app that has a dependency on Sleuth ( spring-cloud-starter-slueth ). Generate the project to download the code. It is good practice to give your application a name and also necessary to have meaningful tracing from Sleuth.


1 Answers

You need to add the MDC variable in your log config, like %X{X-B3-TraceId:-} See https://cloud.spring.io/spring-cloud-sleuth/single/spring-cloud-sleuth.html#_log_correlation

like image 136
petronius Avatar answered Sep 30 '22 09:09

petronius