-
Notifications
You must be signed in to change notification settings - Fork 40.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spring Retry causes RetryListener beans to be initialised early, making them and their dependencies ineligible for post-processing #16977
Comments
Thanks for the sample. Here's what's happening:
In this sequence of events, it's step 8 that is causing the problem. Here are the info messages that record the beans the have been initialised too early to be eligible for post-processing:
I agree. AOP advice that retrieves beans from the context (
We can't fix this in I think this problem needs to be addressed in Spring Retry. In the meantime, you can work around the problem and minimise the number of beans the are ineligible for post-processing by making both @Bean
static StatisticsListener retryStatsListener(@Lazy StatisticsRepository statisticsRepository) {
return new StatisticsListener(statisticsRepository);
}
@Bean
static StatisticsRepository retryStatsRepository(MeterRegistry registry) {
return new MeteredStatisticsRepository(new DefaultStatisticsRepository(), registry);
} Can you please open a Spring Retry issue and comment here with a link to it? /cc @dsyer |
@wilkinsona Thanks a lot! I agree with your analysis, I will take a look at Spring Retry part. |
While it's certainly annoying, it doesn't happen silently. As shown above, there are 10 info log messages for early initialisation that suggest something isn't right and 6 of them are for metrics-related beans. |
@wilkinsona I was just stumbling over this myself and I tried adding the above 2 methods to my SpringBootApplication, and I see in the debugger that they are called, but still the metrics are not registered:
I am on Spring Boot 2.3.9. Is there anything else I can do to fix this? |
@robertpanzer Spring Retry may not be the (only) cause of eager initialization. I can't tell from that list of eagerly-initialised beans. If you can reproduce the problem in a minimal app, please open a new issue and we can take a look. If you want to try to diagnose the problem yourself, I'd place a breakpoint in the |
Thanks, that helped. It was my RetryListener that somehow triggered this problem too. I guess I need to learn more and better understand in detail how the bean construction and injection works in Spring. |
Rather than using the bean factory, you could inject an |
Thanks, that's even better! 👍 |
Hello,
I have encountered a case where I would like to send retry metrics to
MeterRegistry
with the help ofStatisticsListener
. Basically, I tried to decorate the existingDefaultStatisticsRepository
and use it inStatisticsListener
. The affected version is 2.1.5.RELEASE.But unfortunately, registering this StatisticsListener initialize the
MeterRegistry
way earlier than theMeterRegistryPostProcessor
manages to post-process theMeterRegistry
, which results in missing certain metrics like jvm memory, jvm threads, etc...I prepared a simple project to illustrate this issue:
https://github.com/chengchen/metrics-example/blob/master/src/main/java/com/chengchen/metricsexample/MetricsExampleApplication.java
If you try to call /actuator/metrics, you will not see all the missing metrics that I mentioned. If you remove these 2 beans, metrics will work again.
In my opinion, it's not up to the user to know that
StatisticsListener
will trigger early initialization of its dependent beans (in this caseMeterRegistry
), probably some fix could be in theMeterRegistryPostProcessor
level?The text was updated successfully, but these errors were encountered: