Fix RabbitMQ Propagation

15 minutes

In this step, you’ll edit application code in the message producer and consumer so W3C Trace Context flows through RabbitMQ AMQP headers.

Note

RabbitMQ has no broker-level trace setting - propagation is always implemented in application code.

The async handoff from payment-api to fulfillment-worker requires manual context injection and extraction in AMQP message headers:

When either side omits this, each consumed message starts a new root trace - one of the most common async observability gaps in production.

The Fix

1. Producer - (payment-api)

Open file to edit:

text
cd ~/workshop/context-propagation
vi services/payment-api/server.js

Locate buildFulfillmentMessageHeaders() and wrap the return value with injectTraceHeaders():

javascript
function buildFulfillmentMessageHeaders(order, payment) {
  return {
    'x-order-id': order.orderId,
    'x-payment-id': payment.paymentId,
  };
}

Check your work before proceeding

Run the following command from ./workshop/context-propagation folder to compare your changes with the expected solution:

bash
diff ./services/payment-api/server.js  ./services/payment-api/server-fixed.js

2. Consumer - (fulfillment-worker)

Open file to edit:

text
cd ~/workshop/context-propagation
vi services/fulfillment-worker/worker.js

Add the import at the top:

javascript
import { extractTraceContext } from './shared/propagation.js';

Replace the extractMessageContext() stub with the shared extractor.

javascript
// Remove this stub:
function extractMessageContext(_headers) {
  return context.active(); // ignores AMQP headers
}

Check your work before proceeding

Run the following command from ./workshop/context-propagation folder to compare your changes with the expected solution:

bash
diff ./services/fulfillment-worker/worker.js  ./services/fulfillment-worker/worker-fixed.js