Chapter 15. Setting up distributed tracing
Distributed tracing allows you to track the progress of transactions between applications in a distributed system. In a microservices architecture, tracing tracks the progress of transactions between services. Trace data is useful for monitoring application performance and investigating issues with target systems and end-user applications.
In AMQ Streams, tracing facilitates the end-to-end tracking of messages: from source systems to Kafka, and then from Kafka to target systems and applications. It complements the metrics that are available to view in JMX metrics, as well as the component loggers.
Support for tracing is built in to the following Kafka components:
- Kafka Connect
- MirrorMaker
- MirrorMaker 2
- AMQ Streams Kafka Bridge
Tracing is not supported for Kafka brokers.
You add tracing configuration to the properties file of the component.
To enable tracing, you set environment variables and add the library of the tracing system to the Kafka classpath. For Jaeger tracing, you can add tracing artifacts for the following systems:
- OpenTelemetry with the Jaeger Exporter
- OpenTracing with Jaeger
Support for OpenTracing is deprecated. The Jaeger clients are now retired and the OpenTracing project archived. As such, we cannot guarantee their support for future Kafka versions.
To enable tracing in Kafka producers, consumers, and Kafka Streams API applications, you instrument application code. When instrumented, clients generate trace data; for example, when producing messages or writing offsets to the log.
Setting up tracing for applications and systems beyond AMQ Streams is outside the scope of this content.
15.1. Outline of procedures Copy linkLink copied to clipboard!
To set up tracing for AMQ Streams, follow these procedures in order:
Set up tracing for MirrorMaker, MirrorMaker 2, and Kafka Connect:
Set up tracing for clients:
Instrument clients with tracers:
For information on enabling tracing for the Kafka Bridge, see Using the AMQ Streams Kafka Bridge.
15.2. Tracing options Copy linkLink copied to clipboard!
Use OpenTelemetry or OpenTracing (deprecated) with the Jaeger tracing system.
OpenTelemetry and OpenTracing provide API specifications that are independent from the tracing or monitoring system.
You use the APIs to instrument application code for tracing.
- Instrumented applications generate traces for individual requests across the distributed system.
- Traces are composed of spans that define specific units of work over time.
Jaeger is a tracing system for microservices-based distributed systems.
- Jaeger implements the tracing APIs and provides client libraries for instrumentation.
- The Jaeger user interface allows you to query, filter, and analyze trace data.
The Jaeger user interface showing a simple query
15.3. Environment variables for tracing Copy linkLink copied to clipboard!
Use environment variables when you are enabling tracing for Kafka components or initializing a tracer for Kafka clients.
Tracing environment variables are subject to change. For the latest information, see the OpenTelemetry documentation and OpenTracing documentation.
The following tables describe the key environment variables for setting up a tracer.
| Property | Required | Description |
|---|---|---|
|
| Yes | The name of the Jaeger tracing service for OpenTelemetry. |
|
| Yes | The exporter used for tracing. |
|
| Yes |
The exporter used for tracing. Set to |
| Property | Required | Description |
|---|---|---|
|
| Yes | The name of the Jaeger tracer service. |
|
| No |
The hostname for communicating with the |
|
| No |
The port used for communicating with the |
15.4. Enabling tracing for Kafka Connect Copy linkLink copied to clipboard!
Enable distributed tracing for Kafka Connect using configuration properties. Only messages produced and consumed by Kafka Connect itself are traced. To trace messages sent between Kafka Connect and external systems, you must configure tracing in the connectors for those systems.
You can enable tracing that uses OpenTelemetry or OpenTracing.
Procedure
-
Add the tracing artifacts to the
opt/kafka/libsdirectory. Configure producer and consumer tracing in the relevant Kafka Connect configuration file.
-
If you are running Kafka Connect in standalone mode, edit the
/opt/kafka/config/connect-standalone.propertiesfile. -
If you are running Kafka Connect in distributed mode, edit the
/opt/kafka/config/connect-distributed.propertiesfile.
Add the following tracing interceptor properties to the configuration file:
Properties for OpenTelemetry
producer.interceptor.classes=io.opentelemetry.instrumentation.kafkaclients.TracingProducerInterceptor consumer.interceptor.classes=io.opentelemetry.instrumentation.kafkaclients.TracingConsumerInterceptor
producer.interceptor.classes=io.opentelemetry.instrumentation.kafkaclients.TracingProducerInterceptor consumer.interceptor.classes=io.opentelemetry.instrumentation.kafkaclients.TracingConsumerInterceptorCopy to Clipboard Copied! Toggle word wrap Toggle overflow Properties for OpenTracing
producer.interceptor.classes=io.opentracing.contrib.kafka.TracingProducerInterceptor consumer.interceptor.classes=io.opentracing.contrib.kafka.TracingConsumerInterceptor
producer.interceptor.classes=io.opentracing.contrib.kafka.TracingProducerInterceptor consumer.interceptor.classes=io.opentracing.contrib.kafka.TracingConsumerInterceptorCopy to Clipboard Copied! Toggle word wrap Toggle overflow With tracing enabled, you initialize tracing when you run the Kafka Connect script.
-
If you are running Kafka Connect in standalone mode, edit the
- Save the configuration file.
- Set the environment variables for tracing.
Start Kafka Connect in standalone or distributed mode with the configuration file as a parameter (plus any connector properties):
Running Kafka Connect in standalone mode
su - kafka /opt/kafka/bin/connect-standalone.sh \ /opt/kafka/config/connect-standalone.properties \ connector1.properties \ [connector2.properties ...]
su - kafka /opt/kafka/bin/connect-standalone.sh \ /opt/kafka/config/connect-standalone.properties \ connector1.properties \ [connector2.properties ...]Copy to Clipboard Copied! Toggle word wrap Toggle overflow Running Kafka Connect in distributed mode
su - kafka /opt/kafka/bin/connect-distributed.sh /opt/kafka/config/connect-distributed.properties
su - kafka /opt/kafka/bin/connect-distributed.sh /opt/kafka/config/connect-distributed.propertiesCopy to Clipboard Copied! Toggle word wrap Toggle overflow The internal consumers and producers of Kafka Connect are now enabled for tracing.
15.5. Enabling tracing for MirrorMaker 2 Copy linkLink copied to clipboard!
Enable distributed tracing for MirrorMaker 2 by defining the Interceptor properties in the MirrorMaker 2 properties file. Messages are traced between Kafka clusters. The trace data records messages entering and leaving the MirrorMaker 2 component.
You can enable tracing that uses OpenTelemetry or OpenTracing.
Procedure
-
Add the tracing artifacts to the
opt/kafka/libsdirectory. Configure producer and consumer tracing in the
opt/kafka/config/connect-mirror-maker.propertiesfile.Add the following tracing interceptor properties to the configuration file:
Properties for OpenTelemetry
header.converter=org.apache.kafka.connect.converters.ByteArrayConverter producer.interceptor.classes=io.opentelemetry.instrumentation.kafkaclients.TracingProducerInterceptor consumer.interceptor.classes=io.opentelemetry.instrumentation.kafkaclients.TracingConsumerInterceptor
header.converter=org.apache.kafka.connect.converters.ByteArrayConverter producer.interceptor.classes=io.opentelemetry.instrumentation.kafkaclients.TracingProducerInterceptor consumer.interceptor.classes=io.opentelemetry.instrumentation.kafkaclients.TracingConsumerInterceptorCopy to Clipboard Copied! Toggle word wrap Toggle overflow Properties for OpenTracing
header.converter=org.apache.kafka.connect.converters.ByteArrayConverter producer.interceptor.classes=io.opentracing.contrib.kafka.TracingProducerInterceptor consumer.interceptor.classes=io.opentracing.contrib.kafka.TracingConsumerInterceptor
header.converter=org.apache.kafka.connect.converters.ByteArrayConverter producer.interceptor.classes=io.opentracing.contrib.kafka.TracingProducerInterceptor consumer.interceptor.classes=io.opentracing.contrib.kafka.TracingConsumerInterceptorCopy to Clipboard Copied! Toggle word wrap Toggle overflow ByteArrayConverterprevents Kafka Connect from converting message headers (containing trace IDs) to base64 encoding. This ensures that messages are the same in both the source and the target clusters.With tracing enabled, you initialize tracing when you run the Kafka MirrorMaker 2 script.
- Save the configuration file.
- Set the environment variables for tracing.
Start MirrorMaker 2 with the producer and consumer configuration files as parameters:
su - kafka /opt/kafka/bin/connect-mirror-maker.sh \ /opt/kafka/config/connect-mirror-maker.properties
su - kafka /opt/kafka/bin/connect-mirror-maker.sh \ /opt/kafka/config/connect-mirror-maker.propertiesCopy to Clipboard Copied! Toggle word wrap Toggle overflow The internal consumers and producers of MirrorMaker 2 are now enabled for tracing.
15.6. Enabling tracing for MirrorMaker Copy linkLink copied to clipboard!
Enable distributed tracing for MirrorMaker by passing the Interceptor properties as consumer and producer configuration parameters. Messages are traced from the source cluster to the target cluster. The trace data records messages entering and leaving the MirrorMaker component.
You can enable tracing that uses OpenTelemetry or OpenTracing.
Procedure
-
Add the tracing artifacts to the
opt/kafka/libsdirectory. Configure producer tracing in the
/opt/kafka/config/producer.propertiesfile.Add the following tracing interceptor property:
Producer property for OpenTelemetry
producer.interceptor.classes=io.opentelemetry.instrumentation.kafkaclients.TracingProducerInterceptor
producer.interceptor.classes=io.opentelemetry.instrumentation.kafkaclients.TracingProducerInterceptorCopy to Clipboard Copied! Toggle word wrap Toggle overflow Producer property for OpenTracing
producer.interceptor.classes=io.opentracing.contrib.kafka.TracingProducerInterceptor
producer.interceptor.classes=io.opentracing.contrib.kafka.TracingProducerInterceptorCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Save the configuration file.
Configure consumer tracing in the
/opt/kafka/config/consumer.propertiesfile.Add the following tracing interceptor property:
Consumer property for OpenTelemetry
consumer.interceptor.classes=io.opentelemetry.instrumentation.kafkaclients.TracingConsumerInterceptor
consumer.interceptor.classes=io.opentelemetry.instrumentation.kafkaclients.TracingConsumerInterceptorCopy to Clipboard Copied! Toggle word wrap Toggle overflow Consumer property for OpenTracing
consumer.interceptor.classes=io.opentracing.contrib.kafka.TracingConsumerInterceptor
consumer.interceptor.classes=io.opentracing.contrib.kafka.TracingConsumerInterceptorCopy to Clipboard Copied! Toggle word wrap Toggle overflow With tracing enabled, you initialize tracing when you run the Kafka MirrorMaker script.
- Save the configuration file.
- Set the environment variables for tracing.
Start MirrorMaker with the producer and consumer configuration files as parameters:
su - kafka /opt/kafka/bin/kafka-mirror-maker.sh \ --producer.config /opt/kafka/config/producer.properties \ --consumer.config /opt/kafka/config/consumer.properties \ --num.streams=2
su - kafka /opt/kafka/bin/kafka-mirror-maker.sh \ --producer.config /opt/kafka/config/producer.properties \ --consumer.config /opt/kafka/config/consumer.properties \ --num.streams=2Copy to Clipboard Copied! Toggle word wrap Toggle overflow The internal consumers and producers of MirrorMaker are now enabled for tracing.
15.7. Initializing tracing for Kafka clients Copy linkLink copied to clipboard!
Initialize a tracer, then instrument your client applications for distributed tracing. You can instrument Kafka producer and consumer clients, and Kafka Streams API applications. You can initialize a tracer for OpenTracing or OpenTelemetry.
Configure and initialize a tracer using a set of tracing environment variables.
Procedure
In each client application add the dependencies for the tracer:
Add the Maven dependencies to the
pom.xmlfile for the client application:Dependencies for OpenTelemetry
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Dependencies for OpenTracing
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Define the configuration of the tracer using the tracing environment variables.
Create a tracer, which is initialized with the environment variables:
Creating a tracer for OpenTelemetry
OpenTelemetry ot = GlobalOpenTelemetry.get();
OpenTelemetry ot = GlobalOpenTelemetry.get();Copy to Clipboard Copied! Toggle word wrap Toggle overflow Creating a tracer for OpenTracing
Tracer tracer = Configuration.fromEnv().getTracer();
Tracer tracer = Configuration.fromEnv().getTracer();Copy to Clipboard Copied! Toggle word wrap Toggle overflow Register the tracer as a global tracer:
GlobalTracer.register(tracer);
GlobalTracer.register(tracer);Copy to Clipboard Copied! Toggle word wrap Toggle overflow Instrument your client:
15.8. Instrumenting producers and consumers for tracing Copy linkLink copied to clipboard!
Instrument application code to enable tracing in Kafka producers and consumers. Use a decorator pattern or interceptors to instrument your Java producer and consumer application code for tracing. You can then record traces when messages are produced or retrieved from a topic.
OpenTelemetry and OpenTracing instrumentation projects provide classes that support instrumentation of producers and consumers.
- Decorator instrumentation
- For decorator instrumentation, create a modified producer or consumer instance for tracing. Decorator instrumentation is different for OpenTelemetry and OpenTracing.
- Interceptor instrumentation
- For interceptor instrumentation, add the tracing capability to the consumer or producer configuration. Interceptor instrumentation is the same for OpenTelemetry and OpenTracing.
Prerequisites
You have initialized tracing for the client.
You enable instrumentation in producer and consumer applications by adding the tracing JARs as dependencies to your project.
Procedure
Perform these steps in the application code of each producer and consumer application. Instrument your client application code using either a decorator pattern or interceptors.
To use a decorator pattern, create a modified producer or consumer instance to send or receive messages.
You pass the original
KafkaProducerorKafkaConsumerclass.Example decorator instrumentation for OpenTelemetry
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example decorator instrumentation for OpenTracing
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To use interceptors, set the interceptor class in the producer or consumer configuration.
You use the
KafkaProducerandKafkaConsumerclasses in the usual way. TheTracingProducerInterceptorandTracingConsumerInterceptorinterceptor classes take care of the tracing capability.Example producer configuration using interceptors
senderProps.put(ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, TracingProducerInterceptor.class.getName()); KafkaProducer<Integer, String> producer = new KafkaProducer<>(senderProps); producer.send(...);senderProps.put(ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, TracingProducerInterceptor.class.getName()); KafkaProducer<Integer, String> producer = new KafkaProducer<>(senderProps); producer.send(...);Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example consumer configuration using interceptors
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
15.9. Instrumenting Kafka Streams applications for tracing Copy linkLink copied to clipboard!
Instrument application code to enable tracing in Kafka Streams API applications. Use a decorator pattern or interceptors to instrument your Kafka Streams API applications for tracing. You can then record traces when messages are produced or retrieved from a topic.
- Decorator instrumentation
-
For decorator instrumentation, create a modified Kafka Streams instance for tracing. The OpenTracing instrumentation project provides a
TracingKafkaClientSupplierclass that supports instrumentation of Kafka Streams. You create a wrapped instance of theTracingKafkaClientSuppliersupplier interface, which provides tracing instrumentation for Kafka Streams. For OpenTelemetry, the process is the same but you need to create a customTracingKafkaClientSupplierclass to provide the support. - Interceptor instrumentation
- For interceptor instrumentation, add the tracing capability to the Kafka Streams producer and consumer configuration.
Prerequisites
You have initialized tracing for the client.
You enable instrumentation in Kafka Streams applications by adding the tracing JARs as dependencies to your project.
-
To instrument Kafka Streams with OpenTelemetry, you’ll need to write a custom
TracingKafkaClientSupplier. The custom
TracingKafkaClientSuppliercan extend Kafka’sDefaultKafkaClientSupplier, overriding the producer and consumer creation methods to wrap the instances with the telemetry-related code.Example custom
TracingKafkaClientSupplierCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Procedure
Perform these steps for each Kafka Streams API application.
To use a decorator pattern, create an instance of the
TracingKafkaClientSuppliersupplier interface, then provide the supplier interface toKafkaStreams.Example decorator instrumentation
KafkaClientSupplier supplier = new TracingKafkaClientSupplier(tracer); KafkaStreams streams = new KafkaStreams(builder.build(), new StreamsConfig(config), supplier); streams.start();
KafkaClientSupplier supplier = new TracingKafkaClientSupplier(tracer); KafkaStreams streams = new KafkaStreams(builder.build(), new StreamsConfig(config), supplier); streams.start();Copy to Clipboard Copied! Toggle word wrap Toggle overflow To use interceptors, set the interceptor class in the Kafka Streams producer and consumer configuration.
The
TracingProducerInterceptorandTracingConsumerInterceptorinterceptor classes take care of the tracing capability.Example producer and consumer configuration using interceptors
props.put(StreamsConfig.PRODUCER_PREFIX + ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, TracingProducerInterceptor.class.getName()); props.put(StreamsConfig.CONSUMER_PREFIX + ConsumerConfig.INTERCEPTOR_CLASSES_CONFIG, TracingConsumerInterceptor.class.getName());
props.put(StreamsConfig.PRODUCER_PREFIX + ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, TracingProducerInterceptor.class.getName()); props.put(StreamsConfig.CONSUMER_PREFIX + ConsumerConfig.INTERCEPTOR_CLASSES_CONFIG, TracingConsumerInterceptor.class.getName());Copy to Clipboard Copied! Toggle word wrap Toggle overflow
15.10. Specifying tracing systems with OpenTelemetry Copy linkLink copied to clipboard!
Instead of the default Jaeger system, you can specify other tracing systems that are supported by OpenTelemetry.
If you want to use another tracing system with OpenTelemetry, do the following:
- Add the library of the tracing system to the Kafka classpath.
Add the name of the tracing system as an additional exporter environment variable.
Additional environment variable when not using Jaeger
OTEL_SERVICE_NAME=my-tracing-service OTEL_TRACES_EXPORTER=zipkin OTEL_EXPORTER_ZIPKIN_ENDPOINT=http://localhost:9411/api/v2/spans
OTEL_SERVICE_NAME=my-tracing-service OTEL_TRACES_EXPORTER=zipkin1 OTEL_EXPORTER_ZIPKIN_ENDPOINT=http://localhost:9411/api/v2/spans2 Copy to Clipboard Copied! Toggle word wrap Toggle overflow
15.11. Custom span names Copy linkLink copied to clipboard!
A tracing span is a logical unit of work in Jaeger, with an operation name, start time, and duration. Spans have built-in names, but you can specify custom span names in your Kafka client instrumentation where used.
Specifying custom span names is optional and only applies when using a decorator pattern in producer and consumer client instrumentation or Kafka Streams instrumentation.
15.11.1. Specifying span names for OpenTelemetry Copy linkLink copied to clipboard!
Custom span names cannot be specified directly with OpenTelemetry. Instead, you retrieve span names by adding code to your client application to extract additional tags and attributes.
Example code to extract attributes
15.11.2. Specifying span names for OpenTracing Copy linkLink copied to clipboard!
To specify custom span names for OpenTracing, pass a BiFunction object as an additional argument when instrumenting producers and consumers.
For more information on built-in names and specifying custom span names to instrument client application code in a decorator pattern, see the OpenTracing Apache Kafka client instrumentation.