confluentinc / confluent-kafka-dotnet

Confluent's Apache Kafka .NET client
https://github.com/confluentinc/confluent-kafka-dotnet/wiki
Apache License 2.0
44 stars 857 forks source link

Need help to add Middleware to the Producer Or Can I extend the ProduceBuilder to add more Handler. #1936

Closed Leela-SR closed 6 months ago

Leela-SR commented 1 year ago

Description

ASP.Net core C#

I am trying to add the OpenTelemetry Kafka Instrumentation - Distributed Tracing to Producer. The Kafka Instrumentation is 100% working. I need to add this Instrumentation to Producer as a middleware or separate handler where I can easily hook or unhook this. I would like to whether we can add a middleware to the Producer. If yes, could any one provide the sample program.

Also, I am looking at the other option of extending the ProducerBuilder to add the handler - say TracingHandler.

How do I extend this and use the ProducerBuilder class?

mhowlett commented 1 year ago

it doesn't extend easily. i think you'd need to create a new ProducerBuilder class that replicated all the existing methods + added your new handler. And this would return a different type of Producer, which wraps the existing one to intercept the produce calls and delivery results. You wouldn't necessarily have to keep the builder pattern - you could have a different style of api.

gsferreira commented 1 year ago

I have a similar use case @Leela-SR, and I'm solving it with KafkaFlow, which is built on top of Confluent's .NET Client and exposes that kind of functionality. https://farfetch.github.io/kafkaflow/docs/guides/middlewares/

Leela-SR commented 1 year ago

I have resolved the Middleware problem using Chain of Responsibility Pattern. I add the Middleware (handlers) to LinkedList and execute the handlers on ProduceAsync call.