2 years ago

#65649

test-img

Kiran K

Why does Spring Sleuth TraceId get carried over MQ when 'spanId' is added as a message header to the JMS message?

I have been agonizing over trying to get the Spring Cloud Sleuth traceId to carry over MQ for quite some time. As I understand based on various articles this is currently unsupported. Yet when I attempted to add "spanId" as a header property on a JMS message I observed that on the MQ listener side the traceId was carried over automatically and I cannot explain why.

Can someone please help me understand why the below code works ?

Note: spring-cloud-starter-sleuth version = 3.0.0, Spring Boot version = 2.4.3

    @AllArgsConstructor
    public class MQMessagePublisher {
       private final JMSTemplate;
       private final Tracer tracer;
       
       public void publishDummyMessage(){
          TextMessage textMessage = new JMSTextMessage("some data");
          Span span = tracer.currentSpan();
          message.setStringProperty("spanId", span.context().spanId());
          jmsTemplate.convertAndSend("SOME_Q_NAME", message);
       }
    }       
 
  public class MQListener {
     @JmsListener(id="listener", destination = "SOME_Q_NAME", concurrency = "2-4")
     public void listen(Message message) throws JMSException {
       
        log.info("Span Id: {}", message.getStringProperty("spanId"));
     }
  }

Apologies for any syntax errors. I did not copy this code from my editor - just wrote it freehand just now but you should be able to replicate my observations with this.

The thing I cant understand is why does this work ? The log statement in the listener clearly carries across the same traceId without any additional work.

UPDATE :

Earlier the question was written incorrectly - its actually the traceId that gets carried over when the 'spanId' is added to the jms message. I have updated the question above with the correct terms.

java

spring

spring-cloud-sleuth

0 Answers

Your Answer

Accepted video resources