Integrating Spring Cloud Sleuth with Spring boot amqp

白昼怎懂夜的黑 提交于 2020-01-03 16:52:08

问题


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?


回答1:


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 ;) ).




回答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.



来源:https://stackoverflow.com/questions/40176907/integrating-spring-cloud-sleuth-with-spring-boot-amqp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!