|
| 1 | +# Library Instrumentation for Spring Kafka version 2.7 and higher |
| 2 | + |
| 3 | +Provides OpenTelemetry instrumentation for [Spring Kafka](https://spring.io/projects/spring-kafka), |
| 4 | +enabling consumer and producer messaging spans. |
| 5 | + |
| 6 | +## Quickstart |
| 7 | + |
| 8 | +### Add these dependencies to your project |
| 9 | + |
| 10 | +Replace `OPENTELEMETRY_VERSION` with the [latest release](https://central.sonatype.com/artifact/io.opentelemetry.instrumentation/opentelemetry-spring-kafka-2.7). |
| 11 | + |
| 12 | +For Maven, add to your `pom.xml` dependencies: |
| 13 | + |
| 14 | +```xml |
| 15 | +<dependencies> |
| 16 | + <dependency> |
| 17 | + <groupId>io.opentelemetry.instrumentation</groupId> |
| 18 | + <artifactId>opentelemetry-spring-kafka-2.7</artifactId> |
| 19 | + <version>OPENTELEMETRY_VERSION</version> |
| 20 | + </dependency> |
| 21 | +</dependencies> |
| 22 | +``` |
| 23 | + |
| 24 | +For Gradle, add to your dependencies: |
| 25 | + |
| 26 | +```kotlin |
| 27 | +implementation("io.opentelemetry.instrumentation:opentelemetry-spring-kafka-2.7:OPENTELEMETRY_VERSION") |
| 28 | +``` |
| 29 | + |
| 30 | +### Usage |
| 31 | + |
| 32 | +The instrumentation library provides interceptors that can be added to Spring Kafka message |
| 33 | +listener containers and producers to provide spans and context propagation. |
| 34 | + |
| 35 | +```java |
| 36 | +import io.opentelemetry.api.OpenTelemetry; |
| 37 | +import io.opentelemetry.instrumentation.kafkaclients.v2_6.KafkaTelemetry; |
| 38 | +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; |
| 42 | +import org.springframework.kafka.config.ContainerCustomizer; |
| 43 | +import org.springframework.kafka.listener.ConcurrentMessageListenerContainer; |
| 44 | + |
| 45 | +@Configuration |
| 46 | +public class KafkaInstrumentationConfig { |
| 47 | + |
| 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 |
| 58 | + public ContainerCustomizer<String, String, ConcurrentMessageListenerContainer<String, String>> |
| 59 | + listenerCustomizer(OpenTelemetry openTelemetry) { |
| 60 | + SpringKafkaTelemetry springKafkaTelemetry = SpringKafkaTelemetry.create(openTelemetry); |
| 61 | + return container -> { |
| 62 | + container.setRecordInterceptor(springKafkaTelemetry.createRecordInterceptor()); |
| 63 | + container.setBatchInterceptor(springKafkaTelemetry.createBatchInterceptor()); |
| 64 | + }; |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
0 commit comments