Skip to content

Commit ce0eafc

Browse files
Copilottrask
andcommitted
Update README with interceptor configuration documentation
Co-authored-by: trask <[email protected]>
1 parent 3b68915 commit ce0eafc

File tree

1 file changed

+47
-0
lines changed
  • instrumentation/kafka/kafka-clients/kafka-clients-2.6/library

1 file changed

+47
-0
lines changed

instrumentation/kafka/kafka-clients/kafka-clients-2.6/library/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,49 @@ The Kafka clients API provides a way to "intercept" messages before they are sen
3535
The OpenTelemetry instrumented Kafka library provides two interceptors to be configured to add tracing information automatically.
3636
The interceptor class has to be set in the properties bag used to create the Kafka client.
3737

38+
##### Recommended approach: Configuring interceptors with KafkaTelemetry
39+
40+
The recommended way to use interceptors is to configure them with a `KafkaTelemetry` instance.
41+
This gives you full control over the configuration, including which `OpenTelemetry` instance to use,
42+
whether to enable receive telemetry, and which headers to capture.
43+
44+
For the producer, configure the interceptor with the KafkaTelemetry instance:
45+
46+
```java
47+
KafkaTelemetry telemetry = KafkaTelemetry.builder(openTelemetry)
48+
.setCapturedHeaders(Arrays.asList("custom-header"))
49+
.build();
50+
51+
Map<String, Object> props = new HashMap<>();
52+
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
53+
props.put(ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, TracingProducerInterceptor.class.getName());
54+
props.putAll(telemetry.producerInterceptorConfigProperties());
55+
56+
Producer<String, String> producer = new KafkaProducer<>(props);
57+
```
58+
59+
For the consumer, configure the interceptor with the KafkaTelemetry instance:
60+
61+
```java
62+
KafkaTelemetry telemetry = KafkaTelemetry.builder(openTelemetry)
63+
.setMessagingReceiveInstrumentationEnabled(true)
64+
.setCapturedHeaders(Arrays.asList("custom-header"))
65+
.build();
66+
67+
Map<String, Object> props = new HashMap<>();
68+
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
69+
props.put(ConsumerConfig.GROUP_ID_CONFIG, "my-group");
70+
props.put(ConsumerConfig.INTERCEPTOR_CLASSES_CONFIG, TracingConsumerInterceptor.class.getName());
71+
props.putAll(telemetry.consumerInterceptorConfigProperties());
72+
73+
Consumer<String, String> consumer = new KafkaConsumer<>(props);
74+
```
75+
76+
##### Alternative: Using interceptors with global OpenTelemetry
77+
78+
If you don't explicitly configure the interceptors with a `KafkaTelemetry` instance, they will fall back to using
79+
`GlobalOpenTelemetry.get()` and system properties for configuration.
80+
3881
Use the `TracingProducerInterceptor` for the producer in order to create a "send" span automatically, each time a message is sent.
3982

4083
```java
@@ -47,6 +90,10 @@ Use the `TracingConsumerInterceptor` for the consumer in order to create a "rece
4790
props.setProperty(ConsumerConfig.INTERCEPTOR_CLASSES_CONFIG, TracingConsumerInterceptor.class.getName());
4891
```
4992

93+
The interceptors will use the following system properties for configuration:
94+
- `otel.instrumentation.messaging.experimental.receive-telemetry.enabled` - Enable receive telemetry (default: false)
95+
- `otel.instrumentation.messaging.experimental.capture-headers` - List of headers to capture as span attributes
96+
5097
#### Wrapping clients
5198

5299
The other way is by wrapping the Kafka client with a tracing enabled Kafka client.

0 commit comments

Comments
 (0)