0

I am using the spring boot 2.2.2.Release version.

this is my controller class.

@RestController
public class HelloController {

    private static final Logger LOGGER = LogManager.getLogger(HelloController.class);
    @GetMapping(value = "{userId}")
    public String test(@PathVariable("userId") String userId){
        LOGGER.info("user =  {}",userId);
        return "ok";
    }
}

this is my pom.xml and in the detail i have added the micrometer tracing library and use the spring boot 2.2.2 version. and also i use the log4j2 library.

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>loglama</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>loglama</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
         </properties>
    <dependencies>

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

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.17.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-jul</artifactId>
            <version>2.17.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.17.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.17.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-log4j-appender</artifactId>
            <version>3.5.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

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

        <!-- https://mvnrepository.com/artifact/io.micrometer/micrometer-tracing -->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-tracing</artifactId>
            <version>1.1.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.micrometer/micrometer-tracing-bridge-brave -->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-tracing-bridge-brave</artifactId>
            <version>1.1.2</version>
        </dependency>


    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.micrometer</groupId>
                <artifactId>micrometer-tracing-bom</artifactId>
                <version>1.1.2</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>

this is the pattern and i added [%X{traceId},%X{spanId}].

appender.console.layout.pattern = [%d{yyy-MM-dd HH:mm:ss:SSS}] [%X{traceId},%X{spanId}] [%p] - %l - %m%n

the log output is :

[2023-07-11 10:45:49:889] [,] [INFO] - com.example.loglama.controller.HelloController.test(HelloController.java:18) - user =  demo123

if you can see the traceId and spaceId are empty. the company doesn't want to upgrade the spring boot version for that I have to fix this issue with this version. How can fix this issue ?

Sezer Aydın
  • 187
  • 1
  • 2
  • 10
  • You only added dependencies as there is no auto configuration for Micrometer in this version of Spring Boot you have to manually register all necessary components. – M. Deinum Jul 11 '23 at 07:57
  • Thank you so much for your answer. what do you mean manually register necessary components ? I didn't find do you have any links,pls ? – Sezer Aydın Jul 11 '23 at 08:43
  • Check what Spring Boot 3 is registering for components in the context to enable micrometer tracing, you will need to replicate that for Spring Boot 2.2. – M. Deinum Jul 11 '23 at 08:46
  • sorry, I still didn't understand what is registering for components in the context to enable micrometer tracing. they said that just add the library and use it in the all tutorial. am I missing something? – Sezer Aydın Jul 11 '23 at 08:59
  • Why do you think adding some dependencies, write a log line will automagically work? Support for Micrometer was **added in Spring Boot 3**, so if you try it with **Spring Boot 2** this won't automagically work. You will need to setup micrometer yourself. – M. Deinum Jul 11 '23 at 09:01

1 Answers1

1

If you want Distributed Tracing and if you are on:

  • Spring Boot 2.7.x -> you should use Spring Cloud Sleuth
  • Spring Boot 3.x. -> you should use Micrometer Tracing

(Boot 2.2.x is very old and no longer supported, you should upgrade.)

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