Skip to content
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

Cyclic bean definition when a Spring Data repository is a dependency of a MeterBinder #27591

Closed
joshiste opened this issue Aug 8, 2021 · 3 comments
Assignees
Labels
type: regression A regression from a previous release
Milestone

Comments

@joshiste
Copy link
Contributor

joshiste commented Aug 8, 2021

I'm using a MeterBinder as suggested by the docs to register a gauge.
While this worked perfectly fine on Spring Boot 2.4.9 it results in a bean definition cycle with 2.5.3,

Minimal project to reproduce the issue:
https://github.com/joshiste/issues/tree/boot-meterbinder

the dependencies of some of the beans in the application context form a cycle:

┌─────┐
|  service defined in com.example.demo.DemoApplication
↑     ↓
|  usersRepository defined in com.example.demo.UsersRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration
↑     ↓
|  metricsRepositoryMethodInvocationListener defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/data/RepositoryMetricsAutoConfiguration.class]
↑     ↓
|  simpleMeterRegistry defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/export/simple/SimpleMetricsExportAutoConfiguration.class]
↑     ↓
|  userMetrics defined in com.example.demo.DemoApplication
└─────┘

My Bean definitions:

   @Bean
    UserService service(UsersRepository repsitory) {
        return new UserService(repsitory);
    }

    @Bean
    MeterBinder userMetrics(UserService userService) {
        return registry -> Gauge.builder("user.cout", userService::count);
    }
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 8, 2021
@joshiste joshiste changed the title Cyclic Bean definition using MeterBinder and Spring Data JPA (2.5.3) Cyclic bean definition using MeterBinder and Spring Data JPA (2.5.3) Aug 8, 2021
@wilkinsona
Copy link
Member

Thanks for the report, @joshiste. The cycle is due to the introduction of metrics for Spring Data repositories. You can break it in your own code by making UserService lazy when you inject it into userMetrics:

@Bean
MeterBinder userMetrics(@Lazy UserService userService) {
    return registry -> Gauge.builder("user.cout", userService::count);
}

We'll have to see if there's something we can do in Spring Boot to make this unnecessary. If there isn't, we can at least update the documentation with some additional guidance.

@wilkinsona wilkinsona added this to the 2.5.x milestone Aug 9, 2021
@wilkinsona wilkinsona added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 9, 2021
@wilkinsona
Copy link
Member

We can break the cycle in Boot by annotating the MeterRegistry that's injected into the metricsRepositoryMethodInvocationListener @Bean method @Lazy. Another option may be to use ObjectFactory to defer the retrieval of the bean but this would require changes to MetricsRepositoryMethodInvocationListener to overload the constructor. I'd like to discuss the options with the team.

@wilkinsona wilkinsona added the for: team-meeting An issue we'd like to discuss as a team to make progress label Aug 9, 2021
@philwebb
Copy link
Member

We discussed this today and we're going to change the MetricsRepositoryMethodInvocationListener to accept a Supplier. We'll deprecate the existing constructor and call the new one with a SingletonSupplier.

@philwebb philwebb removed the for: team-meeting An issue we'd like to discuss as a team to make progress label Aug 11, 2021
@wilkinsona wilkinsona self-assigned this Aug 12, 2021
@wilkinsona wilkinsona changed the title Cyclic bean definition using MeterBinder and Spring Data JPA (2.5.3) Cyclic bean definition when a Spring Data repository is a dependency of a MeterBinder Aug 12, 2021
@wilkinsona wilkinsona added type: regression A regression from a previous release and removed type: bug A general bug labels Aug 12, 2021
@wilkinsona wilkinsona modified the milestones: 2.5.x, 2.5.4 Aug 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: regression A regression from a previous release
Projects
None yet
Development

No branches or pull requests

4 participants