public class TracingProducerInterceptor extends Object implements org.apache.kafka.clients.producer.ProducerInterceptor<Object,Object>
An interceptor is a kind of plugin/hook that allows you to intercept the records received by the producer before they are actually published to the Kafka cluster. The idea is to capture the record(s) to be sent to remote broker and inject the opentracing headers.
This is how you can use the interceptor.
.....
public Producer createProducer() {
Producer<String, T> producer;
Properties config = new Properties();
config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
config.put(ProducerConfig.CLIENT_ID_CONFIG, "Producer-1");
....
....
config.put(ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, "io.opns.otl.kafka.TracingProducerInterceptor");
producer = new KafkaProducer<String, T>(config);
return producer
}
public void send(String topic, T msg) {
Producer<String, T> producer = createProducer();
ProducerRecord<String, T> record = new ProducerRecord<>(topic, msg);
producer.send(record);
producer.close();
}
Note that the interceptor will always create a new span whether a parent span exist or not. If no active span exists, then no contextual info will be sent to broker. The idea of creating a new span is to represent the duration in time the message waited at the queue to be consumed later. The consumer interceptor on the other side, therefore must create a new follows_from span immediately after polling the message and close it. So effectively for same message, we would have two different spans created, and the relationship would FOLLOWS_FROM as opposed to CHILD_OF. You can, however, disable this feature by setting the system property
-Dkafka.producer.spanto false. In which case, it will check if any span exists in the current thread context, if so, then helps propagate the span context to the broker. Such that any consumer running other side would eventually receive the same span. Note that, prior 0.8.0 there was no concept of a message header. So if you are on 0.8.0, upgrade you kafka client to the latest version.
| Constructor and Description |
|---|
TracingProducerInterceptor() |
| Modifier and Type | Method and Description |
|---|---|
void |
close() |
void |
configure(Map<String,?> config) |
void |
onAcknowledgement(org.apache.kafka.clients.producer.RecordMetadata rm,
Exception excptn) |
org.apache.kafka.clients.producer.ProducerRecord<Object,Object> |
onSend(org.apache.kafka.clients.producer.ProducerRecord<Object,Object> record) |
public void configure(Map<String,?> config)
configure in interface org.apache.kafka.common.Configurablepublic org.apache.kafka.clients.producer.ProducerRecord<Object,Object> onSend(org.apache.kafka.clients.producer.ProducerRecord<Object,Object> record)
public void onAcknowledgement(org.apache.kafka.clients.producer.RecordMetadata rm,
Exception excptn)
Copyright © 2020. All rights reserved.