2

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.

Geek_To_Learn
  • 1,816
  • 5
  • 28
  • 48

1 Answers1

2

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

Marcin Grzejszczak
  • 10,624
  • 1
  • 16
  • 32