Reputation: 1956
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.
Upvotes: 3
Views: 7850
Reputation: 1
The Spring Actuator already the autoconfiguration for both Brave and otel Tracers but still I was unable to see the traceId and spanId after making all the suggested changes.
Then I came to know -
If it is a Spring Webflux project you won't be able to see the traceId and spanId as the micrometer does not support that in sprint boot 3. If its Spring MVC then you should be able to see the traceId and SpanId.
https://github.com/spring-projects/spring-boot/issues/33372
https://github.com/spring-projects/spring-framework/issues/29466
Upvotes: 0
Reputation: 11
Adding Hooks.enableAutomaticContextPropagation()
to the main()
function fix this issue for me
Upvotes: 0
Reputation: 11149
Change %X{X-B3-TraceId},%X{X-B3-SpanId}
to %X{traceId},%X{spanId}
Upvotes: 4