7

I've been using Spring Boot for a long time. I'm working on Micronaut now.

I'm used to using Sleuth to print trace and span IDs automatically on logs. What is the sleuth equivalent in Micronaut?

If there is no equivalent, how to print the trace and span IDs in Micronaut using Jaeger?

Anoop Hallimala
  • 625
  • 1
  • 11
  • 25

1 Answers1

-1

The first trace of how it should be done could be seen over this link:

https://blogs.ashrithgn.com/adding-transaction-trace-id-correlation-id-for-each-request-in-micronaut-for-tracing-the-log-easily/

and the most important part is the following, the rest i have ignored:

@Filter("/**")
@Singleton
@Slf4j
public class HttpFilter implements HttpServerFilter {

    @Override
    public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) {
        String uuid = request.getHeaders().contains("X-TRACE-ID") ? request.getHeaders().get("X-TRACE-ID") : UUID.randomUUID().toString();
        request.setAttribute("traceId", uuid);

        log.debug("filter is working");
        return Flux.from(chain.proceed(request)).map(mutableHttpResponse -> {
            MDC.put("trace", uuid);
            mutableHttpResponse.getHeaders().add("X-TRACE-ID",uuid);
            log.debug(mutableHttpResponse.getBody(String.class).orElse("empty"));
            return mutableHttpResponse;
        });
    }
}

however, it may happen that the header does not contain the X-TREACE-ID , in that case just debugg the request, and than see what headers are there, for example I have used instead of H-TRACE-IF an uber-trace-id where trace id was manually extraced:

        val traceId = uberTraceId!!.substringBefore(":")

and placed in MDC

        MDC.put("trace", traceId)

the next step is to enable micronaut tracing bean

tracing:
  jaeger:
    enabled: true
    sender:
      agentHost: "your jeager host"
      agentPort: "your jaeger port"

that should be it. If the Trace is in header, than it means that the tracing is started by another service. However, if this service does is the one that starts tracing, the traceId is initialised later and here as it is not seen in header, of course it will not be available for logging, but it is sent to jaeger instance.