Search code examples
javaspringprometheusspring-kafka

missing metriks spring kafka listener


I use KafkaListener to get a message from kafka, but I don't get the spring_kafka_listener metrics

registering a metric with the name spring_kaf_ka_listener did not give results, an error message will appear that such a metric has already been registered

the addition did not give any result cf.addListener(new MicrometerConsumerListener<>(new SimpleMeterRegistry()));

@Configuration
@EnableKafka
public class KafkaConsumerConfig {

    @Autowired
    private KafkaProperties kafkaProperties;

    @Bean
    public Map<String, Object> consumerConfigs() {
        Map<String, Object> props = new HashMap<>(kafkaProperties.buildConsumerProperties());
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.GROUP_ID_CONFIG, kafkaProperties.getConsumer().getGroupId());
        return props;
    }


    @Bean
    public ConsumerFactory<String, String> consumerFactory() {
        final StringDeserializer jsonDeserializer = new StringDeserializer();
        ConsumerFactory<String,String> cf = new DefaultKafkaConsumerFactory<>(
            kafkaProperties.buildConsumerProperties(),
            new StringDeserializer(), jsonDeserializer
        );
        //I tried to add it but it didn't give any result
        cf.addListener(new MicrometerConsumerListener<>(new SimpleMeterRegistry()));//
        return cf;

    }

    @Bean
    public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory =
            new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        factory.setBatchListener(false);

        return factory;
    }

    @Bean
    public DefaultKafkaHeaderMapper headerMapper() {
        return new DefaultKafkaHeaderMapper();
    }
}

Solution

  • The consumer listener enables the native Kafka metrics, the spring_kafka_listener metrics are controlled by the micrometerEnabled container property, which is true by default.

    https://docs.spring.io/spring-kafka/docs/current/reference/html/#micrometer

    The metrics will only be captured if Micrometer is on the class path.

    If you can't figure it out, provide an MCRE so we can see what is wrong.