首页 > 解决方案 > Why isn't Kafka ProducerListener logging trace id and span id?

问题描述

I have a Kafka instance and a simple spring boot application with one REST controller and a ProducerListener bean. The controller accepts a simple String message and sends it to Kafka via KafkaTemplate. I want the ProducerListener to log an info message, but the trace id and span id is missing. In my opinion, it should be included in the log.

I use Spring Cloud Sleuth and Spring Kafka starters.

The message itself is successfully sent via Kafka topic to another spring boot application that gets the trace id correctly, so I assume that the problem is related just to the ProducerListener.

Debugging

I have tried debugging the code and I ended up in the method TracingProducer.send(..). The span instance there is NoopSpan (this was weird) and because of that the TracingCallback doesn't wrap the Callback and the tracing information gets lost. There are some nasty bit operations that I couldn't understand. The jar was this one brave-instrumentation-kafka-clients-5.7.0.jar

The code

The controller

@PostMapping("/pass-to-kafka")
public void passToKafka(@RequestBody String message) {
   logger.info("This log message has trace id and span id");
   kafkaTemplate.send("my-test-topic", message);
}

The producer listener

@Bean
public ProducerListener myProducerListener() {
    return new ProducerListener<>() {
        @Override
        public void onSuccess(ProducerRecord producerRecord, RecordMetadata recordMetadata) { 
            logger.info("This log message is missing trace id and span id");
        }
    };
}

The project with the code is on GitHub: https://github.com/vaclavnemec/kafka-sleuth-problem

I expect the info message to write out trace id and span id. It should be the same values in both of the loggers.

INFO [bar,208706b5c40f8e93,208706b5c40f8e93,false] com.example.demo.Controller: This log message has trace id and span id
.
.
.
INFO [bar,,,] com.example.demo.DemoApplication: This log message is missing trace id and span id

标签: spring-cloudspring-cloud-sleuth

解决方案


spring.sleuth.probability=1.0按照文档和自述文件中的说明进行设置。然后,您将打开跟踪。


推荐阅读