I am calling my Springboot app from another micro service to get the trace & span id's but getting error while starting the app. What I want to do is create Trace & Span in app1 and then call app2 and get the Trace id passed from app1 in app2. In app1 I am able to get the trace & span but in app2 where I want to fetch that trace its failing. I have tried to refer this.

Error that I get is:

Description:

Parameter 0 of constructor in com.test.demo1.filter.MyFilter required a bean of type 'brave.Tracer' that could not be found.

Action:

Consider defining a bean of type 'brave.Tracer' in your configuration." I also tried creating the bean but is giving error "The constructor Tracer() is undefined"

Below is my simple RestController & pom.xml

package com.test.demo1.controller;

import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import brave.Tracer;


@RestController
@Component
public class Greeting {

private static final Logger logger = Logger.getLogger(Greeting.class.getName());
    
    @Autowired
    Tracer tracer;

    @RequestMapping("/greeting")
    public String greeting() {
        logger.info("Hello info from from spring sleuth");
        logger.info("tracer is -----> "+tracer.currentSpan().context().traceIdString());
        System.out.println("tracer-id :"+ tracer.currentSpan().context().traceIdString());
        return "Hello";
    }

}

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.test</groupId>
    <artifactId>demo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo1</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>
        
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-sleuth -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth</artifactId>
            <version>1.0.0.RELEASE</version>
            <type>pom</type>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/io.opentracing.brave/brave-opentracing -->
        <dependency>
            <groupId>io.opentracing.brave</groupId>
            <artifactId>brave-opentracing</artifactId>
            <version>0.15.0</version>
        </dependency>
                
                  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
            <version>${version.spring.boot}</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring-core</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <artifactId>spring-web</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-web</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-web</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-core</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-core</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
       
        <!-- https://mvnrepository.com/artifact/io.zipkin.brave/brave -->
        <dependency>
            <groupId>io.zipkin.brave</groupId>
            <artifactId>brave</artifactId>
            <version>5.12.6</version>
        </dependency>
        
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-core</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
                
    </dependencies>

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

</project>

What to do to fix this or achieve this?

Below is my mvn clean dependency:tree :--

[INFO] +- org.springframework.cloud:spring-cloud-starter-sleuth:jar:2.1.0.RELEASE:compile
[INFO] |  +- org.springframework.cloud:spring-cloud-starter:jar:2.1.0.RELEASE:compile
[INFO] |  |  +- org.springframework.cloud:spring-cloud-context:jar:2.1.0.RELEASE:compile
[INFO] |  |  |  \- org.springframework.security:spring-security-crypto:jar:5.3.4.RELEASE:compile
[INFO] |  |  +- org.springframework.cloud:spring-cloud-commons:jar:2.1.0.RELEASE:compile
[INFO] |  |  \- org.springframework.security:spring-security-rsa:jar:1.0.7.RELEASE:compile
[INFO] |  |     \- org.bouncycastle:bcpkix-jdk15on:jar:1.60:compile
[INFO] |  |        \- org.bouncycastle:bcprov-jdk15on:jar:1.60:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-aop:jar:2.3.4.RELEASE:compile
[INFO] |  \- org.springframework.cloud:spring-cloud-sleuth-core:jar:2.1.0.RELEASE:compile
[INFO] |     +- org.aspectj:aspectjrt:jar:1.9.6:compile
[INFO] |     +- io.zipkin.brave:brave:jar:5.6.1:compile
[INFO] |     |  +- io.zipkin.zipkin2:zipkin:jar:2.12.0:compile
[INFO] |     |  \- io.zipkin.reporter2:zipkin-reporter:jar:2.7.14:compile
[INFO] |     +- io.zipkin.brave:brave-context-log4j2:jar:5.6.1:compile
[INFO] |     +- io.zipkin.brave:brave-instrumentation-spring-web:jar:5.6.1:compile
[INFO] |     |  \- io.zipkin.brave:brave-instrumentation-http:jar:5.6.1:compile
[INFO] |     +- io.zipkin.brave:brave-instrumentation-spring-rabbit:jar:5.6.1:compile
[INFO] |     +- io.zipkin.brave:brave-instrumentation-kafka-clients:jar:5.6.1:compile
[INFO] |     +- io.zipkin.brave:brave-instrumentation-httpclient:jar:5.6.1:compile
[INFO] |     +- io.zipkin.brave:brave-instrumentation-httpasyncclient:jar:5.6.1:compile
[INFO] |     +- io.zipkin.brave:brave-instrumentation-spring-webmvc:jar:5.6.1:compile
[INFO] |     |  \- io.zipkin.brave:brave-instrumentation-servlet:jar:5.6.1:compile
[INFO] |     \- io.zipkin.brave:brave-instrumentation-jms:jar:5.6.1:compile
2

There are 2 answers

6
Marcin Grzejszczak On BEST ANSWER

You've provided Sleuth in version 1.0.0 which is a couple of years old. Please go to start.spring.io to generate a project with Sleuth as its dependency. Then you'll be sure that you don't have mixed up the dependencies manually.

Example of proper setup

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</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>

</project>
1
user23650312 On

In Spring Boot V3.0 & above brave.Trace is not supported.

Use org.apache.htrace instead of brave package

import org.apache.htrace.impl.MilliSpan;
import org.apache.htrace.Trace;

String traceId = Long.toString(new MilliSpan.Builder().build().getTraceId());