0
votes

I have a spring boot application (version 2.3.4) and I'm using @KafkaListener to consume records. I also use actuator and micrometer (version 1.5.5) for the metrics.

The problem is that I can't see the Kafka metrics in /actuator/prometheus. I'm using the following dependencies:

'org.springframework.boot' version '2.3.4.RELEASE'
implementation group: 'org.springframework.kafka', name: 'spring-kafka', version: '2.5.10.RELEASE'
implementation group: 'org.apache.kafka', name: 'kafka-clients', version: '2.5.1'

And added these properties to application.yaml:

management:
  server:
    port: 9091
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      probes:
        enabled: true

spring:
  jmx:
    enabled: true

Trying to under if I should add anything else to make the kafka metrics visible in /actuator/prometheus

Note that the metrics are visible when I use default KafkaTemplate, but when trying to create a custom KafkaTemplate, the metrics disappear:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public ProducerFactory<String, String> customProducerFactory() {
        Map<String, Object> configProps = new HashMap<>();
        configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");
        configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, Serdes.String().serializer().getClass().getName());
        configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, Serdes.String().serializer().getClass().getName());
        return new DefaultKafkaProducerFactory<>(configProps);
    }

    @Bean
    public KafkaTemplate<String, String> customProducer() {
        return new KafkaTemplate<>(customProducerFactory());
    }

    @KafkaListener(id = "test", topics = "test_topic")
    public void listen(String in) {
        System.out.println(in);
    }

    @Bean
    public NewTopic topic() {
        return TopicBuilder.name("test_topic").partitions(1).replicas(1).build();
    }


    @Bean
    public ApplicationRunner runner(KafkaTemplate<String, String> template) {
        return args -> {
            template.send("test_topic", "foo");
        };
    }
}
2

2 Answers

1
votes

The solution was to add a listener to the custom kafkaTemplate:

@Bean
    public ProducerFactory<String, String> customProducerFactory() {
        Map<String, Object> configProps = new HashMap<>();
        configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");
        configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, Serdes.String().serializer().getClass().getName());
        configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, Serdes.String().serializer().getClass().getName());
        DefaultKafkaProducerFactory<String, String> producerFactory = new DefaultKafkaProducerFactory<>(configProps);
        producerFactory.addListener(new MicrometerProducerListener<>(meterRegistry));
    }

@Bean
public KafkaTemplate<String, String> customProducer() {
    return new KafkaTemplate<>(customProducerFactory());
}
0
votes

I just tried it with Boot 2.4.2 (spring-kafka 2.6.5) with no problems:

@SpringBootApplication
public class So65791799Application {

    public static void main(String[] args) {
        SpringApplication.run(So65791799Application.class, args);
    }


    @KafkaListener(id = "so65791799", topics = "so65791799")
    public void listen(String in) {
        System.out.println(in);
    }

    @Bean
    public NewTopic topic() {
        return TopicBuilder.name("so65791799").partitions(1).replicas(1).build();
    }


    @Bean
    public ApplicationRunner runner(KafkaTemplate<String, String> template) {
        return args -> {
            template.send("so65791799", "foo");
        };
    }

}
server:
  port: 9091
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      probes:
        enabled: true

spring:
  jmx:
    enabled: true

  kafka:
    consumer:
      auto-offset-reset: earliest

http://localhost:9091/actuator/prometheus

...
# HELP kafka_consumer_fetch_manager_records_per_request_avg The average number of records in each request
# TYPE kafka_consumer_fetch_manager_records_per_request_avg gauge
kafka_consumer_fetch_manager_records_per_request_avg{client_id="consumer-so65791799-1",kafka_version="2.6.0",spring_id="kafkaConsumerFactory.consumer-so65791799-1",} 0.5
...

I dropped it back to Boot 2.3.5 and it still works for me.