sysco-middleware / kafka-interceptors

Set of interceptors to integrate to your Kafka Clients.
MIT License
9 stars 0 forks source link

Add Type of Client to Config Collector #9

Closed jeqo closed 6 years ago

jeqo commented 6 years ago

Currently, config interceptor is only collecting Configurations, but it is not possible to recognize if it is a Producer or Consumer.

jeqo commented 6 years ago

CC @timurgen

timurgen commented 6 years ago

@jeqo we might try to use somethings like getStackTrace for current thread to determine caller of interceptor and add its class name to configs. Not sure if it will work well with streams as they use KafkaProducer/KafkaConsumer internally. I can do that and make pull request.

jeqo commented 6 years ago

Would be an option to just have 2 classes, one for consumer and other for the producer (as Zipkin module) and then map it independently on client configuration?

timurgen commented 6 years ago

@jeqo we can do it as well.

This code snippet can extract client kind from execution stack

private void resolveAppKind(final Map<String, ?> configs) {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        String interceptorCaller = "Unknown type";
        for (int i = stackTrace.length - 1; i > 0; i--) {
            String callerClassName = stackTrace[i].getClassName();
            if (Arrays.asList(
                    KafkaProducer.class.getName(), KafkaConsumer.class.getName(), KafkaStreams.class.getName())
                    .contains(callerClassName)) {
                interceptorCaller = callerClassName;
                break;
            }
        }
        this.addField(configs, ConfigHarvesterInterceptorConfig.APP_KIND, interceptorCaller);
    }
jeqo commented 6 years ago

Using KafkaProducer could not be a good idea, as the Producer interface. The same for Consumer.

If we could then use a wrapper client structure for configs: "{ type: PRODUCER, configs: }" and have your current implementation as base-class for specialized classes for Producer and Consumer.

Then, we also avoid the complexity of dealing with the Stack trace.