Now (Spring Boot v2.2.4.RELEASE) Kafka Autoconfiguration metrics are using JMX , but by Micrometer is not mandatory found MBeanServer bean in Spring Context. I think that KafkaMetricsAutoConfiguration.java should coded like this:

@Bean
    @ConditionalOnMissingBean
    public KafkaConsumerMetrics kafkaConsumerMetrics(Optional<MBeanServer> mbeanServer) {
                if(mbeanServer.isPresent()){
                      return new KafkaConsumerMetrics(mbeanServer.get(), Collections.emptyList());
                }else{
                       return new KafkaConsumerMetrics(Collections.emptyList());
                }

    }

What do you think?

Thanks !

Comment From: snicoll

@garridobarrera The second constructor will attempt to locate the platform mBeanServer and will use JMX anyway. Spring Boot 2.2 has disabled JMX support by default so I don't think it makes sense to keep exposing metrics to JMX when the support is disabled.

The auto-configuration is guarded properly and will back-off so I don't think there is a problem here. You'll need to enable the JMX support again to benefit from Kafka metrics. Adding spring.jmx.enabled=true in your config is all that's needed.

Comment From: garridobarrera

The Second constructor invoke this method in Micrometer:

 private static MBeanServer getMBeanServer() {
        List<MBeanServer> mBeanServers = MBeanServerFactory.findMBeanServer(null);
        if (!mBeanServers.isEmpty()) {
            return mBeanServers.get(0);
        }
        return ManagementFactory.getPlatformMBeanServer();
    }

If mBeanServers is empty returns ManagementFactory.getPlatformMBeanServer. In my test I can retrieve kafka metrics with jmx disabled.

Comment From: juliojgd

@snicoll spring.jmx.enabled=false disables the Spring support for JMX, not JMX support from JVM itself (and that is what the second constructor makes use of, by means of Micrometer code with PlatformMBeanServer).

I think here is some confussion about if spring.jmx.enabled enables or disables JMX (JVM) capabilities. My guess is it don't. In this case Micrometer code is prepared for both situations, I think Spring Boot should let Micrometer to manage both situations. For Kafka consumer metrics there is no alternative in kafka client aside from getting the metric data from JMX.

Comment From: snicoll

Yes, I am aware of that (on both counts). The point I was trying to make is that if JMX support is disabled in Spring Boot, it doesn't make much sense to me that we start auto-configuring something that does use it.

If you disagree or don't want to set spring.jmx.enabled=true, feel free to create the kafkaConsumerMetrics bean yourself.

Comment From: juliojgd

@snicoll Thanks, your last answer makes sense for me