Kafka
Kafka is observed through producer and consumer spans. Trace context travels in the message headers, so the service that publishes a message and the service that processes it appear in one trace.
The Kafka client in your application is instrumented by OpenTelemetry. Publishing a message produces a producer span, processing one produces a consumer span, and the trace context carried in the message headers connects the two across services. Nothing is installed on the brokers, so the same setup applies to a self-hosted cluster, MSK, Confluent Cloud, and Kafka-compatible brokers.
What is collected
| Producer span | One PRODUCER span per message sent, in the trace of the request that sent it |
| Consumer span | One CONSUMER span per message processed, in the consuming service |
| Identity | messaging.system = kafka |
| Destination | Topic name, and the partition where the instrumentation records it |
| Outcome | Duration, and an error status when the send or the handler fails |
Trace propagation
The producer instrumentation writes the W3C traceparent (and baggage) into the message headers. The consumer instrumentation reads them and continues the same trace, so the time between publishing and processing, and everything the consumer does afterwards, is attached to the original request.
Three conditions have to hold.
- Both sides are instrumented. A producer without instrumentation sends no headers, and the consumer starts a trace of its own.
- The headers survive the path. Kafka has carried record headers since 0.11. Anything that re-publishes messages, such as a mirroring or transformation step, has to copy them.
- Both sides report to Sophonz. The producer and consumer are usually different services with their own
service.name. Each needs a validservice.key.
Sampling follows the producer. With the default parent-based sampler, a message published inside a sampled trace is processed inside that trace, and one published inside an unsampled trace is not recorded by the consumer either.
NOTE — Batch consumption uses links
When messages are handled as a batch, one consumer span cannot be the child of many producers. The instrumentation then starts the consumer in a trace of its own and attaches a span link to each producer instead of a parent. The two sides are no longer one trace. This applies to eachBatch in kafkajs and to the Java agent when receive telemetry is enabled.
Enabling it
The Sophonz Node.js SDK loads the instrumentation for kafkajs (0.3 up to, not including, 3). No configuration is needed.
producer.send()produces a producer span per message and injects the context into its headers.eachMessageproduces a consumer span that is a child of the producer's span.eachBatchproduces one span for the batch and a span per message, each linked to its producer.
The bundle has no instrumentation for node-rdkafka or @confluentinc/kafka-javascript.
In Sophonz
A trace that crosses Kafka 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. Kafka 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 = 'kafka'
GROUP BY serviceName, name
ORDER BY spans DESC
LIMIT 20Broker-side figures such as consumer lag, partition counts, and under-replicated partitions 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
- RabbitMQ — the same propagation model over AMQP
- Python instrumentation — how installed instrumentations are loaded and configured