spring-cloud / spring-cloud/spring-cloud-stream
Add RecordInterceptor support for Kafka Streams binder
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 1.1k
- Forks
- 646
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 8
Description
Describe the issue
The Spring Kafka project provides RecordInterceptor for regular Kafka consumers, enabling cross-cutting concerns (logging, metrics, tracing) to be applied automatically before record processing. However, the Kafka Streams binder in Spring Cloud Stream has no equivalent mechanism.
Currently, users must manually add .process() or .peek() calls in every Function<KStream, KStream> bean to apply common logic. This is repetitive and error-prone, especially in applications with many stream functions.
Use case
A common need is to apply shared logic to every record across all KStream bindings, for example:
- Structured logging of record metadata (topic, partition, offset)
- Micrometer metrics collection per topic
- Distributed tracing context propagation from record headers
Proposed solution
Introduce a KafkaStreamsRecordInterceptor interface that users register as a Spring bean. The binder would auto-detect it and insert a .process() side-effect processor node into the topology in AbstractKafkaStreamsBinderProcessor.getKStream(), before event type routing and user function processing.
@FunctionalInterface
public interface KafkaStreamsRecordInterceptor {
void intercept(Record<Object, Object> record, RecordInterceptorContext context);
}
Multiple interceptors would be supported via @Order and composed through a CompositeKafkaStreamsRecordInterceptor.
Example usage
@Bean
public KafkaStreamsRecordInterceptor loggingInterceptor() {
return (record, ctx) -> {
log.info("topic={}, partition={}, offset={}", ctx.topic(), ctx.partition(), ctx.offset());
};
}
// Business logic stays clean, interceptor is applied automatically
@Bean
public Function<KStream<String, Order>, KStream<String, Result>> process() {
return input -> input.mapValues(this::processOrder);
}
Related issues
- #2577 Instrumentation of Kafka consumers for MDC logging
- #3137 Provide hook to intercept incoming and outgoing messages per binding
Version of the framework
Spring Cloud Stream 5.0.x / Kafka Streams binder
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading AbstractKafkaStreamsBinderProcessor.getKStream() and the Spring Kafka RecordInterceptor reference linked in the issue. Review related issues #2577 and #3137 before deciding how interceptor registration, ordering, context, and topology placement should work. Done means a reviewed interceptor design and implementation that applies registered interceptors to KStream bindings without requiring per-function process or peek calls.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, kafka, spring
- Domain
- stream-processing
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100