Skip to content

Commit 1be68c4

Browse files
committed
update kafka client usage example
1 parent 4fdb583 commit 1be68c4

File tree

1 file changed

+22
-10
lines changed
  • instrumentation/spring/spring-kafka-2.7/library

1 file changed

+22
-10
lines changed

instrumentation/spring/spring-kafka-2.7/library/README.md

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Library Instrumentation for Spring Kafka version 2.7 and higher
22

33
Provides OpenTelemetry instrumentation for [Spring Kafka](https://spring.io/projects/spring-kafka),
4-
enabling consumer messaging spans for Spring Kafka listeners
4+
enabling consumer and producer messaging spans.
55

66
## Quickstart
77

@@ -30,26 +30,38 @@ implementation("io.opentelemetry.instrumentation:opentelemetry-spring-kafka-2.7:
3030
### Usage
3131

3232
The instrumentation library provides interceptors that can be added to Spring Kafka message
33-
listener containers to provide OpenTelemetry-based spans and context propagation.
33+
listener containers and producers to provide spans and context propagation.
3434

3535
```java
3636
import io.opentelemetry.api.OpenTelemetry;
37+
import io.opentelemetry.instrumentation.kafkaclients.v2_6.KafkaTelemetry;
3738
import io.opentelemetry.instrumentation.spring.kafka.v2_7.SpringKafkaTelemetry;
39+
import org.springframework.boot.autoconfigure.kafka.DefaultKafkaProducerFactoryCustomizer;
40+
import org.springframework.context.annotation.Bean;
41+
import org.springframework.context.annotation.Configuration;
3842
import org.springframework.kafka.config.ContainerCustomizer;
3943
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;
4044

41-
public class SpringKafkaConfiguration {
45+
@Configuration
46+
public class KafkaInstrumentationConfig {
4247

43-
// Use this ContainerCustomizer to add interceptors to your Kafka listener containers.
48+
// Instrument Kafka producers
49+
@Bean
50+
public DefaultKafkaProducerFactoryCustomizer producerInstrumentation(
51+
OpenTelemetry openTelemetry) {
52+
KafkaTelemetry kafkaTelemetry = KafkaTelemetry.create(openTelemetry);
53+
return producerFactory -> producerFactory.addPostProcessor(kafkaTelemetry::wrap);
54+
}
55+
56+
// Instrument Kafka consumers
57+
@Bean
4458
public ContainerCustomizer<String, String, ConcurrentMessageListenerContainer<String, String>>
45-
createListenerCustomizer(OpenTelemetry openTelemetry) {
46-
SpringKafkaTelemetry telemetry = SpringKafkaTelemetry.builder(openTelemetry).build();
59+
listenerCustomizer(OpenTelemetry openTelemetry) {
60+
SpringKafkaTelemetry springKafkaTelemetry = SpringKafkaTelemetry.create(openTelemetry);
4761
return container -> {
48-
container.setRecordInterceptor(telemetry.createRecordInterceptor());
49-
container.setBatchInterceptor(telemetry.createBatchInterceptor());
62+
container.setRecordInterceptor(springKafkaTelemetry.createRecordInterceptor());
63+
container.setBatchInterceptor(springKafkaTelemetry.createBatchInterceptor());
5064
};
5165
}
52-
53-
// Configure the customizer in your Spring Kafka configuration.
5466
}
5567
```

0 commit comments

Comments
 (0)