0

We have a microservice architecture built with Spring Boot 2.2 and we are using Spring Cloud Sleuth to propagate the trace id.

However, we have one old huge monolithic built with Spring Boot 1.5.2 that it is not using Sleuth (upgrading this monolithic to Spring Boot 2.x is not an option). I have tried integrating Spring Cloud Sleuth 1.3.5 into this but it is not generating the Trace ID either (and found no documentation about it).

I currently coded this filter to log my transaction as an alternative, but I don't want to reinvent the wheel:

@Component
public class TransactionLoggingFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;

        String txnId = ofNullable(request.getHeader(TXN_ID_HEADER))
                .orElse(randomUUID().toString().substring(0, 8));

        MDC.put(TXN_ID, txnId);
        chain.doFilter(request, servletResponse);
        MDC.remove(TXN_ID);
    }
}

I was wondering how I can add Sleuth and programmatically create the trace/span id, so it can propagate seamlessly to other microservices using Spring Boot 2.2.

The older documentation I found about Spring Cloud Sleuth is 2.1.6. So, I found no way to use Sleuth with Spring Boot 1.5. Do you know if it is compatible with it and how I can integrate it?

I don't like above snippet because I'm creating another trace id that is not caught by Sleuth.

Federico Piazza
  • 30,085
  • 15
  • 87
  • 123

2 Answers2

0

If you follow the B3 Headers convention that should work out of the box. In general you should upgrade to the latest version as soon as possible cause 1.3.x is ancient.

The headers that I'm talking about are here for 1.3.6 (https://github.com/spring-cloud/spring-cloud-sleuth/blob/v1.3.6.RELEASE/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/Span.java#L79-L86) and here for the 2.x version of Sleuth (https://github.com/openzipkin/brave/blob/release-5.10.2/brave/src/main/java/brave/propagation/B3Propagation.java#L102-L125)

Marcin Grzejszczak
  • 10,624
  • 1
  • 16
  • 32
  • Definitely, I want to upgrade asap although not easy and time consuming. Can you please post a code snippet of the headers you are talking about that are needed to propagate the trace id? – Federico Piazza Mar 30 '20 at 16:25
  • Updated the answer. – Marcin Grzejszczak Mar 30 '20 at 16:28
  • Thanks again. Marcin I'm still not 100% clear with your answer, so what you say is that I have to populate all those headers into the Request (servletRequest) through my `TransactionLoggingFilter`? Or should I create that Span object and put it somewhere in Sleuth? Apologize if this is obvious to you, I'm trying to understand it – Federico Piazza Mar 30 '20 at 16:37
  • What I'm saying is that if you have a Boot 1.5 Sleuth 1.3 app then you don't have to do anything for it to communicate with Boot 2.x Sleuth 2.x app cause those headers are propagated automatically. – Marcin Grzejszczak Mar 30 '20 at 16:39
  • I see... then something is not working for me because I don't see any trace/span in the logs. This is why I was asking how to do it programmatically. Do you know if by simply adding the 1.3 start it should work? I can't find any documentation about it – Federico Piazza Mar 30 '20 at 16:41
  • I don't understand your problem. You can build a span using Sleuth.builder() and continue it using Tracer.continueSpan – Marcin Grzejszczak Mar 30 '20 at 16:43
  • Sorry, my problem is that I added Sleuth 1.3 and I don't see any trace id in the logs, so it seems that Sleuth is either not properly configured or not working, this is why I wanted to use Sleuth programmatically. On the other hand, I was looking for documentation because having the starter 1.3 seems not enough to make it work – Federico Piazza Mar 30 '20 at 16:47
0

Have found the problem to my answer. Sharing for others that might find it useful.

In order to use spring boot 1.5, the version of Spring Cloud Sleuth must be 1.3.6. The problem we had was that we have a custom logback.xml, and this was restricting Sleuth of printing the trace/span id.

Therefore, when we added spring boot default.xml configuration in logback.xml, it started printing the needed logs:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!-- Needed for Sleuth to print the logs -->
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>

    <variable name="LOGS_DIR_VAR" value="${LOGS_DIR:-/logs}"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>
                %clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}
            </pattern>
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123