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

Proper ordering of metrics auto-configurations #21134

Closed
wants to merge 1 commit into from
Closed

Proper ordering of metrics auto-configurations #21134

wants to merge 1 commit into from

Conversation

bendiscz
Copy link
Contributor

Metrics auto-configurations that depend on a MeterRegistry bean
should be configured AFTER configurations that create the
MeterRegistry. However, @AutoConfigureAfter(MetricsAutoConfiguration.class)
is not sufficient. MetricsAutoConfiguration does not export a MeterRegistry.

This commit changes the @AutoConfigureAfter annotations so that the
proper ordering of configurations in ensured.

The long story:

I ran into issues in one of my spring-boot services when trying to fix a Micrometer bug. After adding a simple custom auto-configuration with dependency on MeterRegistry and LogbackMetrics, the application failed to start because of a missing LogbackMetrics dependency. Debug output showed that the LogbackMetricsAutoConfiguration is disabled because the condition @ConditionalOnBean(MeterRegistry.class) is not met. After more debugging I found out that LogbackMetricsAutoConfiguration was evaluated before PrometheusMetricsExportAutoConfiguration.

I created a sample project that demonstrates this issue.

A simple workaround that I currently use in my projects is a dummy auto-configuration that inserts itself between LogbackMetricsAutoConfiguration and configurations that create the MeterRegistry:

@Configuration
@AutoConfigureAfter({ CompositeMeterRegistryAutoConfiguration.class, SimpleMetricsExportAutoConfiguration.class })
@AutoConfigureBefore({
        JvmMetricsAutoConfiguration.class,
        KafkaMetricsAutoConfiguration.class,
        Log4J2MetricsAutoConfiguration.class,
        LogbackMetricsAutoConfiguration.class,
        SystemMetricsAutoConfiguration.class
})
public class ProperMetricsOrderingAutoConfiguration {
    // empty
}

@pivotal-issuemaster
Copy link

@bendiscz Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 26, 2020
@pivotal-issuemaster
Copy link

@bendiscz Thank you for signing the Contributor License Agreement!

@snicoll snicoll added the for: team-attention An issue we'd like other members of the team to review label May 8, 2020
Copy link
Member

@snicoll snicoll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. Did you fix those 5 auto-configurations because you were affected by them or have you reviewed all of them and those particular 5 were a problem?

I've also added a suggestion as I don't really understand looking at the code why those two are necessary as one runs before the other.

@@ -37,7 +38,7 @@
* @since 2.1.0
*/
@Configuration(proxyBeanMethods = false)
@AutoConfigureAfter(MetricsAutoConfiguration.class)
@AutoConfigureAfter({ CompositeMeterRegistryAutoConfiguration.class, SimpleMetricsExportAutoConfiguration.class })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SimpleMetricsExportAutoConfiguration runs before CompositeMeterRegistryAutoConfigurationso I don't understand why both of them have to be defined here. If you run afterCompositeMeterRegistryAutoConfiguration, SimpleMetricsExportAutoConfiguration` should have been processed too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right, CompositeMeterRegistryAutoConfiguration should be sufficient. I added both because I saw this combination in other metrics auto-configurations, but –looking at it again – those were configurations for exporters (and the ordering was "before").

I'm going to change it.

@snicoll snicoll added the status: waiting-for-feedback We need additional information before we can continue label May 8, 2020
@bendiscz
Copy link
Contributor Author

bendiscz commented May 9, 2020

Did you fix those 5 auto-configurations because you were affected by them or have you reviewed all of them and those particular 5 were a problem?

I was directly affected by LogbackMetricsAutoConfiguration. Then I checked all other metrics auto-configurations in the same package and saw that they have the same problem.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels May 9, 2020
Metrics auto-configurations that depend on a MeterRegistry bean
should be configured AFTER configurations that create the
MeterRegistry. However, `@AutoConfigureAfter(MetricsAutoConfiguration.class)`
is not sufficient. MetricsAutoConfiguration does not export MeterRegistry.

This commit changes the `@AutoConfigureAfter` annotations so that the
proper ordering of configurations in ensured.
@philwebb philwebb added for: merge-with-amendments Needs some changes when we merge type: bug A general bug and removed status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged labels May 13, 2020
@philwebb philwebb added this to the 2.2.x milestone May 13, 2020
@philwebb philwebb self-assigned this May 13, 2020
@philwebb philwebb removed the for: team-attention An issue we'd like other members of the team to review label May 13, 2020
@philwebb
Copy link
Member

Possible related to #11890

philwebb pushed a commit that referenced this pull request Aug 17, 2020
Update metrics auto-configurations so that they are auto-configured
after `CompositeMeterRegistryAutoConfiguration` in order to ensure
the `MeterRegistry` bean has been defined.

Prior to this commit, metrics auto-configurations that depended on a
`MeterRegistry` has `@AutoConfigureAfter(MetricsAutoConfiguration.class)`
which is not sufficient since `MetricsAutoConfiguration` does not export
a `MeterRegistry`.

See gh-21134
philwebb added a commit that referenced this pull request Aug 17, 2020
@philwebb philwebb closed this in 460fb3c Aug 17, 2020
@philwebb
Copy link
Member

Thanks very much for the PR @bendiscz. One of the reasons we've got away with this for so long is that CompositeMeterRegistryAutoConfiguration just happens to be alphabetically before the configurations that use it.

Unfortunately, despite my best efforts, I couldn't find an easy way to test the fix. Each test I tried ended up causing a order cycle. I'm pretty confident with the fix, so we'll have to just run with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: merge-with-amendments Needs some changes when we merge type: bug A general bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

LogbackMetrics stop to count logback events after LoggerContext reset
5 participants