Skip to content

Propagate context to Spring MessageListenerAdapter#2478

Merged
richardstartin merged 1 commit into
masterfrom
rgs/spring-rabbit
Mar 11, 2021
Merged

Propagate context to Spring MessageListenerAdapter#2478
richardstartin merged 1 commit into
masterfrom
rgs/spring-rabbit

Conversation

@richardstartin
Copy link
Copy Markdown
Contributor

@richardstartin richardstartin commented Mar 10, 2021

When Spring users use RabbitMQ MessageListenerAdapter they get broken traces, because the scope of the basic.deliver span is not propagated to the MessageListenerAdapter's delegate, where any traceable activity will start a new trace. This happens because Spring AMQP (wisely) invokes user callbacks on a different thread to the MQ subscription, and the handoff is not done via an executor. Instead, RabbitMQ Envelopes are wrapped in Delivery objects which are enqueued by BlockingQueueConsumer, which drains said queue and wraps the Deliverys as Message objects, which will eventually be passed to the MessageListenerAdapter via AbstractMessageListenerContainer.executeListener.

The basic data flow here

  • capture any active scope and field inject it into Delivery in construction (in the subscription thread)
  • transfer state from Delivery to Message in BlockingQueueConsumer.handle (on the handling thread)
  • extract and activate the continuation in AbstractMessageListenerContainer.executeListener and close the scope afterwards.

Since this instrumentation depends on two libraries (spring-rabbit and spring-amqp) it is muzzled via spring-boot-starter-amqp which depends on both.

Since there is a circular dependency between this instrumentation and rabbit-amqp-2.7, it needs to be tested that this instrumentation does not interfere with the other instrumentation, but this cannot be done by adding a dependency both ways. I copied the rabbitmq tests into this module and they pass here, but removed the copied test before committing. I can add it back if it's felt important enough.

Comment thread settings.gradle Outdated
Comment thread dd-java-agent/instrumentation/spring-rabbit/src/test/groovy/SpringAmqpTest.groovy Outdated
Comment thread dd-java-agent/instrumentation/spring-rabbit/src/test/groovy/SpringAmqpTest.groovy Outdated
@richardstartin richardstartin added inst: others All other instrumentations inst: rabbitmq RabbitMQ instrumentation inst: spring Spring instrumentation comp: context propagation Trace context propagation labels Mar 11, 2021
@richardstartin richardstartin changed the title propagate context to Spring MessageListenerAdapter Propagate context to Spring MessageListenerAdapter Mar 11, 2021
@richardstartin richardstartin merged commit 67aa079 into master Mar 11, 2021
@richardstartin richardstartin deleted the rgs/spring-rabbit branch March 11, 2021 15:58
@github-actions github-actions Bot added this to the 0.76.0 milestone Mar 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp: context propagation Trace context propagation inst: others All other instrumentations inst: rabbitmq RabbitMQ instrumentation inst: spring Spring instrumentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants