// 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 for OpenTelemetry, then instrument your client applications for distributed tracing.
You can instrument Kafka producer and consumer clients, and Kafka Streams API applications.

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-{OpenTelemetryKafkaClientVersion}</artifactId>
  <version>{OpenTelemetryAlphaVersion}</version>
</dependency>
<dependency>
  <groupId>io.opentelemetry</groupId>
  <artifactId>opentelemetry-exporter-otlp</artifactId>
  <version>{OpenTelemetryVersion}</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();
----

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

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