4

Looking for an example that shows integrating spring cloud sleuth with spring boot amqp (rabbit) publisher and subscriber.

I do see the following messages in the log

2016-10-21 08:35:15.708 INFO [producer,9148f56490e5742f,943ed050691842ab,false] 30928 --- [nio-8080-exec-1] a.b.c.controllers.MessagingController : Received Request to pulish with Activity OrderShipped 2016-10-21 08:35:15.730 INFO [producer,9148f56490e5742f,943ed050691842ab,false] 30928 --- [nio-8080-exec-1] a.b.c.service.ProducerService : Message published

When I look at messages on Queue, I don't see traceId or any other details added to the header. Should I use MessagePostProcessor to add these to the header?

Also what should be done on the receiving service?

basu76
  • 441
  • 10
  • 19
  • If we are using Spring Boot 2.x and Sleuth, it just enough to enable the property `spring.sleuth.messaging.rabbit.enabled=true` for this purpose. – Paramesh Korrakuti Nov 20 '20 at 07:21

2 Answers2

2

We don't instrument Spring AMQP out of the box. You can however use Spring Integration or Spring Cloud Stream that we do support and then everything will work out of the box. If you need to use Spring AMQP for some reason you'll have to instrument the code yourself (and sends us a PR ;) ).

Marcin Grzejszczak
  • 10,624
  • 1
  • 16
  • 32
  • Hi Marcin - thank you. I wouldn't mind writing the code. Is are some samples on how to integrate. – basu76 Oct 21 '16 at 14:26
  • We have a concept of SpanInjector and SpanExtractor. You can take a look at those used in messaging - https://github.com/spring-cloud/spring-cloud-sleuth/blob/master/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/messaging/MessagingSpanInjector.java and https://github.com/spring-cloud/spring-cloud-sleuth/blob/master/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/messaging/MessagingSpanExtractor.java . They take care of retreiving and passing of tracing info in messages. – Marcin Grzejszczak Oct 21 '16 at 14:41
  • Marcin - If I implement my own version of SpanInjector, how the inject method get called? – basu76 Oct 21 '16 at 17:52
  • Made some progress. Able to publish the info, but it would be nice, if I could do it in a interceptor – basu76 Oct 21 '16 at 18:51
  • Try to file an issue in the Spring AMQP project. BTW Can we mark this as answered? – Marcin Grzejszczak Oct 22 '16 at 09:02
  • @MarcinGrzejszczak can you please fix the links, they are broken – Yuval Simhon Jan 26 '17 at 11:05
  • They aren't broken - they are old. Currently in master we have a different version that no longer supports this concept. This is the old approach http://cloud.spring.io/spring-cloud-sleuth/1.1.x/#_customizations and this is the current new one http://cloud.spring.io/spring-cloud-sleuth/spring-cloud-sleuth.html#_customizations – Marcin Grzejszczak Jan 26 '17 at 12:19
2

Using Spring AMQP you can set MessagePostProcessor on the RabbitTemplateusing the setBeforePublishPostProcessors method.

We implemented the org.springframework.amqp.core.MessagePostProcessor and Overrided the postProcessMessage method this way:

@Override
public org.springframework.amqp.core.Message postProcessMessage(org.springframework.amqp.core.Message message)
        throws AmqpException {
    MessagingMessageConverter converter = new MessagingMessageConverter();
    MessageBuilder<?> mb = MessageBuilder.fromMessage((Message<?>) converter.fromMessage(message));
    inject(tracer.getCurrentSpan(), mb);
    return converter.toMessage(mb.build(), message.getMessageProperties());
}

The inject method can now set all the required headers on the message, and it will be passed to the rabbitMq with the changes.
You have a great example of how to implement such inject method in org.springframework.cloud.sleuth.instrument.messaging.MessagingSpanInjector

We are using v1.1.1 of spring-cloud-sleuth-stream so my example is based on this version, in next release(1.2) it will be easier.

Yuval Simhon
  • 1,439
  • 2
  • 19
  • 34