// Module included in the following assemblies:
//
// assembly-distributed tracing.adoc

[id='proc-configuring-tracers-kafka-clients-{context}']
= Initializing tracing for Kafka clients

[role="_abstract"]
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 xref:ref-tracing-environment-variables-{context}[tracing environment variables].

.Procedure

In each client application add the dependencies for the tracer:

. Add the Maven dependencies to the `pom.xml` file for the client application:
+
.Dependencies for OpenTelemetry
[source,xml,subs="attributes+"]
----
<dependency>
    <groupId>io.opentelemetry</groupId>
    <artifactId>opentelemetry-sdk-extension-autoconfigure</artifactId>
    <version>{OpenTelemetryAlphaVersion}</version>
</dependency>
<dependency>
  <groupId>io.opentelemetry.instrumentation</groupId>
  <artifactId>opentelemetry-kafka-clients-{OpenTelemetryKafkaClient}</artifactId>
  <version>{OpenTelemetryAlphaVersion}</version>
</dependency>
<dependency>
  <groupId>io.opentelemetry</groupId>
  <artifactId>opentelemetry-exporter-otlp</artifactId>
  <version>{OpenTelemetryVersion}</version>
</dependency>
----
+
.Dependencies for OpenTracing
[source,xml,subs="attributes+"]
----
<dependency>
    <groupId>io.jaegertracing</groupId>
    <artifactId>jaeger-client</artifactId>
    <version>{JaegerClientVersion}</version>
</dependency>
<dependency>
  <groupId>io.opentracing.contrib</groupId>
  <artifactId>opentracing-kafka-client</artifactId>
  <version>{OpenTracingKafkaClient}</version>
</dependency>
----

. Define the configuration of the tracer using the xref:ref-tracing-environment-variables-{context}[tracing environment variables].

. Create a tracer, which is initialized with the environment variables:
+
.Creating a tracer for OpenTelemetry
[source,java,subs=attributes+]
----
OpenTelemetry ot = GlobalOpenTelemetry.get();
----
+
.Creating a tracer for OpenTracing
[source,java,subs=attributes+]
----
Tracer tracer = Configuration.fromEnv().getTracer();
----

. Register the tracer as a global tracer:
+
[source,java,subs=attributes+]
----
GlobalTracer.register(tracer);
----

. Instrument your client:
+
* xref:proc-instrumenting-producers-consumers-for-opentracing-{context}[]
* xref:proc-instrumenting-kafka-streams-with-tracers-{context}[]