Comment From: Dr-wgy

spring-cloud-dependencie version Greenwich.SR1 spring-boot version 2.1.3.RELEASE

Comment From: Dr-wgy

class MeterRegistryPostProcessor implements BeanPostProcessor {

private final ObjectProvider<MeterBinder> meterBinders;

private final ObjectProvider<MeterFilter> meterFilters;

private final ObjectProvider<MeterRegistryCustomizer<?>> meterRegistryCustomizers;

private final ObjectProvider<MetricsProperties> metricsProperties;

private volatile MeterRegistryConfigurer configurer;

MeterRegistryPostProcessor(ObjectProvider<MeterBinder> meterBinders,
        ObjectProvider<MeterFilter> meterFilters,
        ObjectProvider<MeterRegistryCustomizer<?>> meterRegistryCustomizers,
        ObjectProvider<MetricsProperties> metricsProperties) {
    this.meterBinders = meterBinders;
    this.meterFilters = meterFilters;
    this.meterRegistryCustomizers = meterRegistryCustomizers;
    this.metricsProperties = metricsProperties;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
        throws BeansException {
          // the following code is invalid
    if (bean instanceof MeterRegistry) {
        getConfigurer().configure((MeterRegistry) bean);
    }
    return bean;
}

private MeterRegistryConfigurer getConfigurer() {
    if (this.configurer == null) {
        this.configurer = new MeterRegistryConfigurer(this.meterRegistryCustomizers,
                this.meterFilters, this.meterBinders,
                this.metricsProperties.getObject().isUseGlobalRegistry());
    }
    return this.configurer;
}

}

Comment From: wilkinsona

Thanks for the report but I am afraid it's not clear to me what the problem is that you are trying to describe. You seem to have added a comment to postProcessAfterInitialization that says the code is invalid but I do not understand why you think that's the case. I am also not sure what the issue title means. Sorry.

If you'd like us to spend some time investigating, can you please take some time to produce a minimal application that reproduces the problem you're experiencing?

Comment From: Dr-wgy

@wilkinsona When spring factory instantiate MeterRegistry, the MeterRegistryPostProcessor should execute if (bean instanceof MeterRegistry) { getConfigurer().configure((MeterRegistry) bean); } . but in our project it doesn't work. I guess maybe BeanPostProcessor load order and its impact on beans right?

Comment From: wilkinsona

Yes, that's possible. If something causes a MeterRegistry to be instantiated during bean post-processing, the MeterRegistryPostProcessor may not get a chance to post-process that particular MeterRegistry.

I can't tell why that may be happening, or indeed if that definitely is what's happening, from what you've shared thus far. As I said above, if you'd like us to spend some time investigating, can you please take some time to produce a minimal application that reproduces the problem you're experiencing?

Comment From: Dr-wgy

SpringBoot MeterRegistryPostProcessor does not work when Instantiated MeterRegistry

SpringBoot MeterRegistryPostProcessor does not work when Instantiated MeterRegistry

MeterPostProcessor not in BeanProcessorList

Comment From: wilkinsona

Thanks, but a screenshot isn't sufficient to diagnose why that is the case. I can't justify spending too much more time on this without the information that I've asked for. If you'd like us to spend some time investigating, can you please take some time to produce a minimal application that reproduces the problem you're experiencing? That should be in the form of a small project that you've zipped and attached to this issue.

Comment From: Dr-wgy

Our project dependency is complex, maybe Inconvenient to provide right away . I want add annoation Order on class MeterRegistryPostProcessor SpringBoot MeterRegistryPostProcessor does not work when Instantiated MeterRegistry But It is source code, I can't modify, Do you anyway to resolve this issue ?

Comment From: Dr-wgy

(┬_┬)↘

Comment From: wilkinsona

Setting the order won't have any effect on your problem as I currently understand it. If you want me to have a better understanding of it and to be able to help you, you're going to have to provide the information that I've asked for.

Comment From: Dr-wgy

ok

Comment From: codingman1990

i found the answer: https://github.com/spring-projects/spring-boot/issues/13410

Comment From: wilkinsona

@wuguanyu can you confirm that #13410 solves your problem?

Comment From: codingman1990

@wuguanyu can you confirm that #13410 solves your problem?

yes,i'm sure it works well.

Comment From: wilkinsona

I'm a bit confused, but I guess that you have two accounts (@wuguanyu and @codingman1990). Thanks for letting us know that you figured out your problem.

Comment From: codingman1990

@wilkinsona i'm sorry for confusing you.But i'm not the quetioner,i just resovled the same problem.

Comment From: Dr-wgy

@codingman1990 I guess we have different problem @wilkinsona I solve my problem in another way, The MeterPostProcessor doesn't work indeed

Comment From: Dr-wgy

@wilkinsona Can you reopen this issue ?

Comment From: wilkinsona

Yes, we can re-open the issue if you can provide us with the minimal sample that I've asked for a few times above.

Comment From: kaushikvira

@wuguanyu -- https://github.com/micrometer-metrics/micrometer/issues/513 is much better explanation of problem is being faced. We might want to have way to hook MeterRegistryCustomizer either way early on jvm start OR Retrigger it in case MeterRegistry was eagerly created.

Comment From: kaushikvira

@wilkinsona -- here is minimal sample https://github.com/ptahchiev/spring-boot-metrics-problem credit goes to @ptahchiev

Comment From: wilkinsona

Thanks for trying to help, @kaushikvira. Unfortunately, the problems are not the same. @ptahchiev's problem involved HikariDataSourceMetricsPostProcessor. That class was removed in 2.1.0.M1 as part of upgrading to Hikari 3.2. @wuguanyu is using Spring Boot 2.1.3.

Comment From: asibross

Hey @wilkinsona, I actually encountered a similar issue to what @wuguanyu has reported.

When there is a BPP that causes early initialization of the MeterRegistry, the MeterRegistryPostProcessor will not be invoked for that MeterRegistry bean. The reason is that MeterRegistryPostProcessor does not implement the Ordered or PriorityOrdered interface which means it falls into the nonOrderedPostProcessors bucket in PostProcessorRegistrationDelegate. When there is another BPP in that same bucket that causes early initialization the MeterRegistryPostProcessor won't register before the MeterRegistry bean is created. https://github.com/spring-projects/spring-framework/blob/master/spring-context/src/main/java/org/springframework/context/support/PostProcessorRegistrationDelegate.java#L246

I was able to work around this issue by copying the MeterRegistryPostProcessor code and implementing the Ordered interface. Doing so bumped the MeterRegistryPostProcessor to the orderedPostProcessors bucket and got it registered before the other BPP early init happened. I also had to use the same bean name "meterRegistryPostProcessor" so that I can override the bean creation of the original one.

The reason I had to go through this hack is that MeterRegistryPostProcessor is not public and there is no @ConditionalOnMissingBean in MetricsAutoConfiguration which would allow me to just provide my own alternative.

I think simply making the MeterRegistryPostProcessor implement Ordered (even with lowest precedence) will resolve this issue. Regardless, it would be nice to make it public and allow users to replace that bean if they wish to.

Please let me know if that makes sense, and I will be happy to send a PR.

FYI the BPP that caused my early init issue is TraceLettuceClientResourcesBeanPostProcessor, a new class introduced in 2.2.

Comment From: wilkinsona

Thanks for the analysis, @asibross. TraceLettuceClientResourcesBeanPostProcessor is part of Spring Cloud Sleuth. If it's causing eager bean initialization, then I suspect a general fix needs to be made there. I can't be certain without seeing an example of the problem though. Can you please provide one?

Comment From: asibross

Hi @wilkinsona, here is an example: https://github.com/asibross/metricsdemo When running as is this line is not printed. If you remove redis from the classpath you will see it is printed.

Please note that this is just one case where this problem manifested. We have seen this happen before with other BPP, so addressing the redis specific one will not solve the actual problem.

Comment From: wilkinsona

Thanks for the sample. There are numerous beans that are affected by the eager initialization:

2019-12-16 11:23:08.606  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.619  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.sleuth.propagation.SleuthTagPropagationAutoConfiguration$TagPropagationConfiguration' of type [org.springframework.cloud.sleuth.propagation.SleuthTagPropagationAutoConfiguration$TagPropagationConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.623  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'spring.sleuth-org.springframework.cloud.sleuth.autoconfig.SleuthProperties' of type [org.springframework.cloud.sleuth.autoconfig.SleuthProperties] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.626  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'spring.sleuth.propagation.tag-org.springframework.cloud.sleuth.propagation.SleuthTagPropagationProperties' of type [org.springframework.cloud.sleuth.propagation.SleuthTagPropagationProperties] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.627  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'sleuthFinishedSpanHandler' of type [org.springframework.cloud.sleuth.propagation.TagPropagationFinishedSpanHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.629  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.sleuth.log.SleuthLogAutoConfiguration$Slf4jConfiguration' of type [org.springframework.cloud.sleuth.log.SleuthLogAutoConfiguration$Slf4jConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.632  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'spring.sleuth.log.slf4j-org.springframework.cloud.sleuth.log.SleuthSlf4jProperties' of type [org.springframework.cloud.sleuth.log.SleuthSlf4jProperties] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.633  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'slf4jSpanDecorator' of type [org.springframework.cloud.sleuth.log.Slf4jScopeDecorator] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.638  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration' of type [org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.643  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'sleuthPropagation' of type [brave.propagation.B3Propagation$Factory] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.646  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'sleuthCurrentTraceContextBuilder' of type [brave.propagation.ThreadLocalCurrentTraceContext$Builder] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.648  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'sleuthCurrentTraceContext' of type [brave.propagation.ThreadLocalCurrentTraceContext] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.666  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'errorParser' of type [brave.ErrorParser] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.668  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.sleuth.zipkin2.ZipkinAutoConfiguration' of type [org.springframework.cloud.sleuth.zipkin2.ZipkinAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.671  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'myConfig' of type [com.example.metricsdemo.MyConfig$$EnhancerBySpringCGLIB$$64f56d89] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.673  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration' of type [org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.675  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'management.metrics.export.simple-org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleProperties' of type [org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleProperties] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.677  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'simpleConfig' of type [org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimplePropertiesConfigAdapter] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.680  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration' of type [org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.681  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'micrometerClock' of type [io.micrometer.core.instrument.Clock$1] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.695  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'simpleMeterRegistry' of type [io.micrometer.core.instrument.simple.SimpleMeterRegistry] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.715  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'micrometerTracingReporterMetrics' of type [com.example.metricsdemo.MyConfig$1] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.719  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'spring.zipkin-org.springframework.cloud.sleuth.zipkin2.ZipkinProperties' of type [org.springframework.cloud.sleuth.zipkin2.ZipkinProperties] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.724  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.sleuth.zipkin2.sender.ZipkinRestTemplateSenderConfiguration$DiscoveryClientZipkinUrlExtractorConfiguration$ZipkinClientLoadBalancedConfiguration' of type [org.springframework.cloud.sleuth.zipkin2.sender.ZipkinRestTemplateSenderConfiguration$DiscoveryClientZipkinUrlExtractorConfiguration$ZipkinClientLoadBalancedConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.726  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'loadBalancerClientZipkinLoadBalancer' of type [org.springframework.cloud.sleuth.zipkin2.sender.LoadBalancerClientZipkinLoadBalancer] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.727  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'zipkinUrlExtractor' of type [org.springframework.cloud.sleuth.zipkin2.sender.ZipkinRestTemplateSenderConfiguration$CachingZipkinUrlExtractor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.728  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.sleuth.zipkin2.sender.ZipkinRestTemplateSenderConfiguration' of type [org.springframework.cloud.sleuth.zipkin2.sender.ZipkinRestTemplateSenderConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.730  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'zipkinRestTemplateCustomizer' of type [org.springframework.cloud.sleuth.zipkin2.DefaultZipkinRestTemplateCustomizer] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.737  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'zipkinSender' of type [org.springframework.cloud.sleuth.zipkin2.sender.RestTemplateSender] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.766  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'zipkinReporter' of type [zipkin2.reporter.AsyncReporter$BoundedAsyncReporter] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.773  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'tracing' of type [brave.Tracing$Default] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 11:23:08.777  INFO [,,,] 87413 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'spring.sleuth.redis-org.springframework.cloud.sleuth.instrument.redis.TraceRedisProperties' of type [org.springframework.cloud.sleuth.instrument.redis.TraceRedisProperties] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

Changing the ordering of the post-processor MeterRegistryPostProcessor may address some of these, but it will not address all of them as metrics only play a role once your myConfig bean is reached. It would also be a breaking change for anyone that is reliant upon the current ordering.

I've opened https://github.com/spring-cloud/spring-cloud-sleuth/issues/1508 to tackle the problem on the Spring Cloud Sleuth side. I also opened https://github.com/spring-cloud/spring-cloud-commons/issues/657. While not directly related as it's a separate chain of eager initialization, it is responsible for a couple of the info messages above.

Comment From: asibross

Thank you @wilkinsona. I will follow the above tickets till resolution.

Comment From: wilkinsona

@asibross You may also want to keep an eye on https://github.com/spring-projects/spring-framework/issues/24092.

Comment From: 445990772

this problem I just solved with this code

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(MeterBinder.class)
@Slf4j
class CustomSystemMetricsAutoConfiguration {

    @Bean
    public MetricsEndpoint metricsAutoConfiguration(@Lazy MeterRegistry registry, ObjectProvider<MeterBinder> binders) {
        binders.stream().iterator()
               .forEachRemaining(binder -> binder.bindTo(registry));
        return new MetricsEndpoint(registry);
    }

}

then i request /actuator/metrics, JVM metrics has appeared SpringBoot MeterRegistryPostProcessor does not work when Instantiated MeterRegistry

I'm not sure why the postProcessAfterinitialization of MeterRegistry did not load config,hope this solution can help others。

Our project dependency is complex, maybe Inconvenient to provide right away . I want add annoation Order on class MeterRegistryPostProcessor SpringBoot MeterRegistryPostProcessor does not work when Instantiated MeterRegistry But It is source code, I can't modify, Do you anyway to resolve this issue ?

Comment From: wilkinsona

I'm not sure why the postProcessAfterinitialization of MeterRegistry did not load config

It's probably because something triggered early initialization that prevented something else from being post-processed. You can look for log messages similar to those shown above to check if that's the case. If that doesn't help and you would like us to investigate further please create a minimal sample that reproduces the problem and then share it with us by opening a new issue.