4

I am working with spring-integration for data flow from a UDP endpoint to kafka. I have initialized a replyingKafkaTemplate as a @Bean in the @Configuration with both consumer and producer configurations. When my server is up and after sending some udp requests, I can see the consumer's metrics. However, I cannot see the producer's metrics even after setting a jmx reporter in the Producer Configuration.

I have tried not to set the producer metrics reporter assuming it will automatically appear as the consumer metrics did(with no extra configuration there)

producer configuration

Map<String, Object> configProps = new HashMap<>();
        configProps.put(
                ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,
                bootstrapAddress);
        configProps.put(
                ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
                StringSerializer.class);
        configProps.put(
                ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
                KafkaAvroSerializer.class);
        configProps.put("schema.registry.url", "http://schema-regisry-server:8081");
        configProps.put(
                ProducerConfig.RETRIES_CONFIG,
                3);
        configProps.put(ProducerConfig.RECONNECT_BACKOFF_MS_CONFIG, 500);
        configProps.put(ProducerConfig.RECONNECT_BACKOFF_MAX_MS_CONFIG, 5000);
        configProps.put(ProducerConfig.METRIC_REPORTER_CLASSES_CONFIG, "org.apache.kafka.common.metrics.JmxReporter");
        configProps.put(ProducerConfig.METRICS_RECORDING_LEVEL_CONFIG, "INFO");

        printConfigProps(configProps);
        return new DefaultKafkaProducerFactory<>(configProps);

consumer configuration

Map<String, Object> properties = new HashMap<>();
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, KafkaAvroDeserializer.class);
        properties.put("schema.registry.url", "http://schema-regisry-server:8081");
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, "spring-integration");
        // automatically reset the offset to the earliest offset
        properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");

        return properties;

kafka template creation

@Bean
    public ReplyingKafkaTemplate<String, DataModel, DataModel> replyKafkaTemplate(ProducerFactory<String, DataModel> pf, KafkaMessageListenerContainer<String, DataModel> container) {
        ReplyingKafkaTemplate<String, DataModel, DataModel> template = new ReplyingKafkaTemplate<>(pf, container);
        template.start();
        return template;
    }

Listener container creation:

@Bean
    public KafkaMessageListenerContainer<String, DataModel> replyContainer(ConsumerFactory<String, DataModel> cf) {
        ContainerProperties containerProperties = new ContainerProperties(destinationTopic);
        containerProperties.setGroupId("test");
        return new KafkaMessageListenerContainer<>(cf, containerProperties);
    }

ConsumerFactory creation

@Bean
    public ConsumerFactory<?, ?> consumerFactory() {
        return new DefaultKafkaConsumerFactory<>(consumerConfigs());
    }
Tal Aloni
  • 51
  • 1
  • 7
  • right, I have defined the template and the producer in beans within the same @configuration class. – Tal Aloni May 01 '19 at 09:53
  • I spoke with a Micrometer maintainer and he says kafka producer stats are not implemented in spring boot 2.1.x. So, it seems you either have to wait for that to be implemented, or implement it yourself (and hopefully contribute it back). – jolo May 23 '19 at 13:34

1 Answers1

2

Spring Boot 2 versions prior to 2.3.0 are only exposing consumer metrics by default. Spring Boot 2.3.0 (released a few weeks ago) is dependent on Micrometer 1.4, which is exposing both consumer and producer metrics by default. If you cant use the latest version of Spring Boot, you'll have to implement it yourself.

H. Opler
  • 351
  • 3
  • 8
  • Can you point to a documentation or link somewhere? Spring Boot still mentions Kafka consumer metrics only. https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-metrics-meter – xsreality Jun 11 '20 at 18:24
  • https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes – H. Opler Jun 13 '20 at 11:44
  • 1
    Thanks! Since I was using a custom producer factory, I had to add `factory.addListener(new MicrometerProducerListener<>(meterRegistry));` for the metrics to show up. The official doc still needs updating. – xsreality Jun 15 '20 at 10:18
  • 1
    Yep, after your comment I checked the documentation and saw its missing, opened them an issue and they fixed (https://github.com/spring-projects/spring-boot/issues/21910). will show on documentation on next build :) – H. Opler Jun 15 '20 at 11:18