when upgrading to spring boot 2.5.8 (from 2.2.5) customizer for meter registry returns "object is not an instance of declaring class".

example:

@Bean
public MeterRegistryCustomizer<PrometheusMeterRegistry> customPrometheusMeterRegistry() {
    return registry -> registry.config().commonTags("application", appName);
}

returns:

BeanCreationException: Error creating bean with name 'prometheusMeterRegistry' defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customPrometheusMeterRegistry' defined in class path resource... Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer]: Illegal arguments to factory method 'customPrometheusMeterRegistry'; args: ; nested exception is java.lang.IllegalArgumentException: object is not an instance of declaring class

Same issue also for DatadogMeterRegistry (I did not check others). For 2.5.8 based on release notes i use: io.micrometer:micrometer-registry-prometheus:1.7.7

Version spring-boot 2.5.7 and less are working fine on same code base.

Comment From: snicoll

@bberus unfortunately your example is nowhere near enough to track down the problem you've described. Pasting this in a project generated from start.spring.io works as expected. If you want support, please share a minimal sample that we can run ourselves rather than code pasted in text. You can do so by attaching a zip to this issue or sharing a link to a GitHub repository.

Comment From: bberus

@snicoll thx for comment. I just tried reproduce this and apparently it works fine on new project.

However i did some more analysis on my project - i did some reconfiguration (in one class i have metrics and async configuration) so after migrating to separate MetricConfiguration it stopped complaining on MeterRegistryCustomizer, but I get same issue for getAsyncExecutor()

@EnableAsync
@Configuration
public class AsynchronousConfig extends AsyncConfigurerSupport {

  @Bean
  @Override
  public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor poolExecutor ... 

    return poolExecutor;
    }

Failed to instantiate [java.util.concurrent.Executor]: Illegal arguments to factory method 'getAsyncExecutor'; args: ; nested exception is java.lang.IllegalArgumentException: object is not an instance of declaring class

and again - it's working fine on 2.5.7, and failed on 2.5.8.

however on new project generated from start.spring.io I was not able to reproduce this.

looks like some clashing lib. I will try find it.

Comment From: snicoll

This might a bug in Spring Cloud Sleuth if you're using that that has been fixed in the meantime, see #29151 and the issues that it links. I am going to close this now as the original report turns out to be invalid. We can reopen if it turns out to be a problem in Spring Boot after all.

Comment From: bberus

@snicoll thx! i can confirm that issue was related to sleuth. I use solution described in https://github.com/spring-cloud/spring-cloud-sleuth/issues/2100 and works perfectly.