question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Missing traceId in Kafka listener when consuming in batches

See original GitHub issue

Trace info is lost and current span is null when consuming in batches from a KafkaListener.

I’m working with

  • Spring Boot 2.3.3
  • Spring Kafka 2.5.5
  • Spring Cloud Starter Sleuth 2.4.4

I have a listener like

@KafkaListener(topics = "topic")
public void receiveMessage(List<String> messages) {
        messages.forEach(log::info);
}

And following property in application.yml: spring.kafka.listener.type: batch

If I remove the property then traceId is back

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jonatan-ivanovcommented, Jan 28, 2021

@KostasD21 I was able to repro this issue using your sample project, thanks for that (btw here’s some help for local Kafka).

As I was debugging it, I found that the batch and the non-batch scenarios are handled differently (see: KafkaMessageListenerContainer). This is so different that there are two different components that are calling your @KafkaListener.

The root cause is that the non-batch listener is decorated by Sleuth, while the batch listener is not. If you stop the execution in KafkaMessageListenerContainer where the listener or the batchListener is used you will see that one of them is proxied, the other one is not (by MessageListenerMethodInterceptor):

org.springframework.kafka.listener.adapter.RecordMessagingMessageListenerAdapter$$EnhancerBySpringCGLIB$$2296d4d9
org.springframework.kafka.listener.adapter.BatchMessagingMessageListenerAdapter

This explains why batch does not have traceIds so I looked into where the proxy is set-up: https://github.com/spring-cloud/spring-cloud-sleuth/blob/6b0ceb7ede2a8e3d842be72350c728628d199407/spring-cloud-sleuth-brave/src/main/java/org/springframework/cloud/sleuth/brave/instrument/messaging/SleuthKafkaAspect.java#L105-L109

Unfortunately this scenario is not supported which does make some sense since if there are 10 incoming messages with different traceIDs, which traceId would you pick to propagate to the consumer? If you would like this feature to be implemented, could you please open a new issue as a feature request (please reference this one)?

0reactions
jonatan-ivanovcommented, May 25, 2022

@Johny-Ch No, currently this is not supported. If there are 10 incoming messages with different traceIDs, which traceId would you pick to propagate to the consumer?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Put trace id for each message in kafka listener - Stack Overflow
How can i use KafkaListenerAnnotationBeanPostProcessor to add trace id for each message coming to the consumer?
Read more >
Put trace id for each message in kafka listener-Springboot
Put trace id for each message in kafka listener · Separate Kafka listener for each topic using annotations in spring kafka · Stop...
Read more >
Distributed Tracing for Kafka with OpenTelemetry | New Relic
Learn how to implement open source distributed tracing in Kafka with OpenTelemetry and the otelsarama library.
Read more >
spring-projects/spring-kafka - Gitter
We consume a message, process it and produce a result, and even sometimes the ... Hi Guys need help on get trace details...
Read more >
Spring Cloud Sleuth customization
We decorate the Kafka clients ( KafkaProducer and KafkaConsumer ) to create a span for each event that is produced or consumed. You...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found