RabbitMQ
RabbitMQ is observed through publish and consume spans. Trace context travels in the AMQP message headers, so the service that publishes a message and the service that consumes it appear in one trace.
The AMQP client in your application is instrumented by OpenTelemetry. Publishing a message produces a producer span, consuming one produces a consumer span, and the trace context carried in the message headers connects the two across services. Nothing is installed on the broker, so the same setup applies to a self-hosted RabbitMQ and to hosted services that speak AMQP 0-9-1.
What is collected
| Producer span | One PRODUCER span per message published, in the trace of the request that published it |
| Consumer span | One CONSUMER span per message delivered to a consumer, in the consuming service |
| Identity | messaging.system = rabbitmq |
| Destination | Exchange and routing key, as recorded by the instrumentation |
| Outcome | Duration, and an error status when the publish or the handler fails |
Trace propagation
The producer instrumentation writes the W3C traceparent (and baggage) into the message's headers table. The consumer instrumentation reads them and continues the same trace, so the time the message spent queued, and everything the consumer does afterwards, is attached to the original request.
Three conditions have to hold.
- Both sides are instrumented. A publisher without instrumentation sends no headers, and the consumer starts a trace of its own.
- The headers survive the path. Exchanges, queues, and dead-lettering keep message headers. Code that consumes a message and publishes a new one has to be instrumented itself, or copy the headers across.
- Both sides report to Sophonz. The publisher and consumer are usually different services with their own
service.name. Each needs a validservice.key.
Sampling follows the publisher. With the default parent-based sampler, a message published inside a sampled trace is consumed inside that trace, and one published inside an unsampled trace is not recorded by the consumer either.
Enabling it
The Sophonz Node.js SDK loads the instrumentation for amqplib (0.5.5 up to, not including, 3). No configuration is needed.
channel.publish()andchannel.sendToQueue()produce a producer span and inject the context into the message headers.channel.consume()produces a consumer span per delivered message, as a child of the publisher's span.
Setting useLinksForConsume: true makes the consumer start a new trace that is linked to the publisher's instead. The two sides are then no longer one trace.
import { init } from '@sophonz/node-sdk';
init({
service: 'order-worker',
apiKey: process.env.SOPHONZ_API_KEY,
instrumentations: {
'@opentelemetry/instrumentation-amqplib': {
useLinksForConsume: false,
},
},
});In Sophonz
A trace that crosses RabbitMQ contains both services: the producer span under the request that published the message, and the consumer span with everything the handler did under it. In the Service Flow panel the two services are separate nodes, and an edge is drawn between them when the consumer span is a child of the producer span. RabbitMQ itself is not a node, because the spans belong to the services that produced them.
messaging.system is stored in its own column, msgSystem, so messaging spans can be selected directly in Grafana.
SELECT serviceName, name, count() AS spans, avg(durationNano) / 1e6 AS avg_ms
FROM sophonz_traces.distributed_sophonz_index_v2
WHERE $__timeFilter(timestamp)
AND msgSystem = 'rabbitmq'
GROUP BY serviceName, name
ORDER BY spans DESC
LIMIT 20Broker-side figures such as queue depth, consumer counts, and unacknowledged messages are metrics. Sophonz does not accept metrics, so they are not part of this path. See Connecting infrastructure.
Related documentation
- Connecting infrastructure — accepted signals and the collector configuration
- Kafka — the same propagation model over Kafka record headers
- Python instrumentation — how installed instrumentations are loaded and configured