concept interceptor in category kafka

This is an excerpt from Manning's book Kafka in Action.
One of the options when using the producer is creating producer interceptors. They were introduced in KIP-42 whose main design was to help support measurement and monitoring. It is true that you can modify the message and since the interceptor will run before a partition gets assigned, it is important to note any key changes. The client might not know what to expect if the key changes the partition assignment they intended. In comparison to Kafka Streams workflows, the usage of these interceptors might not be your first choice. Kafka also does report various internal metrics using JMX that can be used if monitoring is a main concern.
If you do create an interceptor you wish to use, remember to set the producer config
interceptor.classes. At the current time, there are no default interceptors that run in the life cycle.
In practice, the interceptor that we define is a way to add logic to the producer, consumer, or both by hooking into the normal flow of our clients, intercepting the record, and adding our custom data before it moves along its normal path. Our changes to the clients is configuration-driven and helps keep our specific logic out of our clients for the most part.
Figure 9.5. Interceptors for Tracing
![]()
Let’s revisit the concept of interceptors that we touched on briefly in chapter 4 in introducing what producer interceptors could do for our messages. By adding an interceptor on both the producer and consumer clients that you are using, you can remove the monitoring logic from the application logic itself. The crosscutting concern of monitoring can hopefully be more encapsulated by this approach.