rust_with_kafka_tls/
custom_context.rs

1use log::trace;
2use rdkafka::client::ClientContext;
3use rdkafka::consumer::stream_consumer::StreamConsumer;
4use rdkafka::consumer::ConsumerContext;
5use rdkafka::consumer::Rebalance;
6use rdkafka::error::KafkaResult;
7use rdkafka::topic_partition_list::TopicPartitionList;
8
9// A context can be used to change the behavior of producers and consumers by adding callbacks
10// that will be executed by librdkafka.
11// This particular context sets up custom callbacks to log rebalancing events.
12pub struct CustomContext;
13
14impl ClientContext for CustomContext {}
15
16impl ConsumerContext for CustomContext {
17    fn pre_rebalance(&self, rebalance: &Rebalance) {
18        trace!("Pre rebalance {:?}", rebalance);
19    }
20
21    fn post_rebalance(&self, rebalance: &Rebalance) {
22        trace!("Post rebalance {:?}", rebalance);
23    }
24
25    fn commit_callback(
26        &self,
27        result: KafkaResult<()>,
28        _offsets: &TopicPartitionList,
29    ) {
30        trace!("Committing offsets: {:?}", result);
31    }
32}
33
34// A type alias with your custom consumer can be created for convenience.
35pub type LoggingConsumer = StreamConsumer<CustomContext>;