public class TracingConsumerInterceptor<K,V> extends Object implements org.apache.kafka.clients.consumer.ConsumerInterceptor<K,V>
A plugin interface that allows you to intercept the records received by the consumer. Span propagation over message bus is always async, where the sender/producer takes the responsibility of creating a new span and passes on the context data, which will eventually be acknowledged by the consumer, as it creates a follows_from span and finished it immediately. Thus the two spans in question represents the amount of time a kafka message actually waited in the queue until before it's consumption.
This is how you can use the interceptor.
.....
public Consumer createConsumer() {
Consumer<String, byte[]> consumer;
Properties config = new Properties();
config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
config.put(ConsumerConfig.CLIENT_ID_CONFIG, "Consumer-1");
....
....
config.put(ConsumerConfig.INTERCEPTOR_CLASSES_CONFIG
, "io.opns.otl.kafka.TracingConsumerInterceptor");
consumer = new KafkaConsumer<String, byte[]>(config);
return consumer
}
public void receive(String topic) {
Consumer<String, byte[]> consumer = createConsumer();
consumer.subscribe(Collections.singletonList(topic));
while (true) {
ConsumerRecords<String, byte[]> records = consumer.poll(Duration.ofMillis(1000L));
if (!records.isEmpty()) {
for (ConsumerRecord<String, byte[]> record : records) {
// process the individual message
}
}
consumer.commitSync();
}
// consumer.close();
}
Note that the interceptor will not create any new span. It will acknowledge the span present in individual message, and alose it accordingly, just before consuming the same, thus marking the end of that span. The application code (followup from consumer) may choose to create a new span to represent it's own work.
| Constructor and Description |
|---|
TracingConsumerInterceptor() |
| Modifier and Type | Method and Description |
|---|---|
void |
close() |
void |
configure(Map<String,?> map) |
void |
onCommit(Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> map) |
org.apache.kafka.clients.consumer.ConsumerRecords<K,V> |
onConsume(org.apache.kafka.clients.consumer.ConsumerRecords<K,V> records) |
public void configure(Map<String,?> map)
configure in interface org.apache.kafka.common.Configurablepublic org.apache.kafka.clients.consumer.ConsumerRecords<K,V> onConsume(org.apache.kafka.clients.consumer.ConsumerRecords<K,V> records)
public void onCommit(Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> map)
public void close()
close in interface AutoCloseableclose in interface org.apache.kafka.clients.consumer.ConsumerInterceptor<K,V>Copyright © 2020. All rights reserved.