Fix RabbitMQ Propagation
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:
cd ~/workshop/context-propagation
vi services/payment-api/server.jsLocate buildFulfillmentMessageHeaders() and wrap the return value with injectTraceHeaders():
function buildFulfillmentMessageHeaders(order, payment) {
return {
'x-order-id': order.orderId,
'x-payment-id': payment.paymentId,
};
}function buildFulfillmentMessageHeaders(order, payment) {
return injectTraceHeaders({
'x-order-id': order.orderId,
'x-payment-id': payment.paymentId,
});
}cp ./services/payment-api/server-fixed.js ./services/payment-api/server.jsCheck your work before proceeding
Run the following command from ./workshop/context-propagation folder to compare your changes with the expected solution:
diff ./services/payment-api/server.js ./services/payment-api/server-fixed.js2. Consumer - (fulfillment-worker) #
Open file to edit:
cd ~/workshop/context-propagation
vi services/fulfillment-worker/worker.jsAdd the import at the top:
import { extractTraceContext } from './shared/propagation.js';Replace the extractMessageContext() stub with the shared extractor.
// Remove this stub:
function extractMessageContext(_headers) {
return context.active(); // ignores AMQP headers
}import { extractTraceContext } from './shared/propagation.js';
// Instead of ignoring AMQP headers, use the shared helper in processFulfillment instead:
const parentContext = extractTraceContext(msg.properties.headers ?? {});cp ./services/fulfillment-worker/worker-fixed.js ./services/fulfillment-worker/worker.jsCheck your work before proceeding
Run the following command from ./workshop/context-propagation folder to compare your changes with the expected solution:
diff ./services/fulfillment-worker/worker.js ./services/fulfillment-worker/worker-fixed.js
