Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TraceId in not getting pushed in logs when using micrometer

I am working on a demo project to enable distributed tracing on logs. I initially started with sleuth, but later on figured out that it is being migrated to micrometer(Sleuth is not compatible with spring boot 3.x)

But out of my surprise I am not able to see traceId and spanId in logs in my console.

Changes I have done.

Added dependency in pom.xml

 <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-tracing-bridge-brave</artifactId>
        <version>1.1.1</version>
            <exclusions>
                <exclusion>
                    <groupId>io.zipkin.brave</groupId>
                    <artifactId>brave</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

Excluded io.zipkin.brave as it was causing a dependency conflict.

Added a consoleAppender on logback.xml

 <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
+        <encoder>
+            <Pattern>
+                %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} [%X{X-B3-TraceId},%X{X-B3-SpanId}] - %msg%n
+            </Pattern>
+        </encoder>
+    </appender>

I have created a test controller:

@GetMapping("/log/test")
  public ResponseEntity<String> test()
  {

    log.info("Inside test");
    return ResponseEntity.ok("Done");
  }

When I am trying to hit this controller I am not getting traceId in logs. sample log:

2023-06-20 18:19:31.649 [http-nio-8090-exec-2] INFO  a.b.c.TestController [,] - Inside test

As per my understanding mdc puts the traceId and spanId directly in log lines. I am confused what I am missing here.

Any help will be highly appreciated.

like image 263
Geek_To_Learn Avatar asked Oct 30 '25 13:10

Geek_To_Learn


1 Answers

Change %X{X-B3-TraceId},%X{X-B3-SpanId} to %X{traceId},%X{spanId}

like image 151
Marcin Grzejszczak Avatar answered Nov 02 '25 04:11

Marcin Grzejszczak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!