Skip to content

Add support for easily disabling metrics export #21658

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

Closed
wants to merge 2 commits into from
Closed

Add support for easily disabling metrics export #21658

wants to merge 2 commits into from

Conversation

onobc
Copy link
Contributor

@onobc onobc commented Jun 3, 2020

Add global metrics exporter disable property; set during tests with ability to override via @AutoConfigureMetrics.

Closes gh-21616

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 3, 2020
*
* @author Chris Bono
*/
class ExcludeMetricExportersContextCustomizerFactoryTests {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test and the sibling ones AutoConfigureMetricsMissing/PresentIntegrationTest overlap on coverage. If I choose between them I think that the integration test ones are more valuable as they verify against @SpringBootTest as the annotated class.

@@ -52,7 +52,14 @@ void autoConfiguresItsConfigAndMeterRegistry() {
}

@Test
void autoConfigurationCanBeDisabled() {
void autoConfigurationCanBeDisabledWithGlobalEnabledProperty() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will do this same mod to the other export config tests once I know we are good w/ the direction of this proposal.

@onobc
Copy link
Contributor Author

onobc commented Jun 3, 2020

@snicoll here is what I captured along the way while trying different approaches.

Background

Metircs auto-configuration topology (wrt AutoConfigureBefore/After of each other)

MetricsAutoConfiguration	
	WavefrontMetricsExportAutoConfiguration (enabled!=false -> WavefrontMeterRegistry)
	DatadogMetricsExportAutoConfiguration (enabled!=false -> DatadogMeterRegistry)
	..
		SimpleMetricsExportAutoConfiguration (COMB-MR, enabled!=false -> SimpleMeterRegistry)
			CompositeMeterRegistryAutoConfiguration	
					- NoOpMeterRegistryConfiguration (COMB-MR -> CompositeMeterRegistry empty)
					- CompositeMeterRegistryConfiguration (multi MRs -> CompositeMeterRegistry w/ MRs) 
				MetricsEndpointAutoConfiguration

Goal

To disable everything between MetricsAutoConfiguration and SimpleMetricsExportAutoConfiguration by default in tests. With ability to override and re-enable all/individual exporters.

Approaches

I tried several approaches leveraging ImportAutoConfiguration and built-in auto-config exclusion etc. but they all ended w/ non-success or non-starters shortcomings.

TLDR; Jump to approach #5 to see the content of this code proposal.

The following are of the "Don't modify any of the existing exporter auto-configurations" variety:

Approach #1:

  • @AutoConfigureMetrics as an @ImportAutoConfiguration which maps to list of ACs to use from spring.factories
  • AutoConfigurationImportFilter that exclude ACs ending in "MetricsExportAutoConfiguration"
    Status: Does not work. The ACIF exclusions take precedence over the IAC list.

Approach #2:

  • @AutoConfigureMetrics as an @ImportAutoConfiguration which maps to list of ACs to use from spring.factories
  • ContextCustomizer that sets the 'spring.autoconfigure.exclude' property to list of ACs from spring.factories (above)
    Status: Does not work. The exclusion always overrides the inclusions.

Approach #3:

  • @AutoConfigureMetrics simple marker interface, nothing more
  • ContextCustomizer that sets 'spring.autoconfigure.metric-exporters.enabled' based on whether the test is marked as @AutoConfigureMetrics
  • AutoConfigurationImportFilter that exclude ACs ending in "MetricsExportAutoConfiguration" unless above property is set (aka marked w/ @AutoConfigureMetrics)

Status: works

  • Pros:
    • simple to implement (no change to existing auto-configs)
    • users custom metric exporters will be respected IF named properly
  • Cons:
    • excludes by naming scheme ("*MetricExportAutoConfiguration")
    • binary enablement (all or nothing) - no way to override and add an inclusion/exclusion

Approach #4:

  • @AutoConfigureMetrics simple marker interface, nothing more
  • ContextCustomizer that sets the 'spring.autoconfigure.exclude' property to hardcoded set of metric exporter configs unless the test is marked w/ @AutoConfigureMetrics

Status: works

  • Pros:
    • simple to implement (no change to existing auto-configs)
  • Cons:
    • excludes by naming scheme ("*MetricExportAutoConfiguration") (NOTE: this can be alleviated by reading list from spring.factories)
    • binary enablement (all or nothing)
    • users custom metric exporters will not be respected in exclusion

The following is of the "Do what you need to do to make this work properly" variety:

Approach #5:

  • @AutoConfigureMetrics simple marker interface, nothing more
  • ContextCustomizer that sets new "global" 'management.metrics.export.enabled' based on whether the test is marked as @AutoConfigureMetrics
  • @ConditionalOnMetricExportsEnabled that inspects global and specific property to decide match or not

Status: works

  • Pros:
    • allows all export to be disabled w/ single property but respects specific export overrides
    • can be used outside of test-land
  • Cons:
    • small change to existing auto-configs - but not functional breaking change
    • users custom metric exporters will be respected in exclusion if they copy pattern and use the @ConditionalOnMetricExportsEnabled

Outcome

New integration test defaults is that only SimpleMeterRegistry will be enabled. The management.metrics.export[.<name>].enabled properties can be used to adjust that.

Next

I have submitted a handful of tests to sanity check the functionality and illustrate the usage of the new annotation. If this is a direction that we agree on then I will add the missing tests and update the docs accordingly in a subsequent commit.

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.

Thank you very much for the PR and sorry for the delay. This looks good to me so if you want to complete this PR as you've offered that would be great.

If not, no worries and I can take care of that when merging. Thanks again.

Comment on lines 28 to 29
* exporters. By default, all metrics exporters other than the in-memory
* {@code SimpleMetricsExportAutoConfiguration} are disabled.
Copy link
Member

Choose a reason for hiding this comment

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

I think that could confuse users as it can be read what the default behaviour of adding the annotation does. I'd rather keep it simple and we can handle the rationale in the reference guide rather.

@snicoll snicoll added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 16, 2020
@snicoll snicoll added this to the 2.4.x milestone Jul 16, 2020
@onobc
Copy link
Contributor Author

onobc commented Jul 16, 2020

@snicoll
I should have to add the tests and update the comments based on the feedback w/in the next 12 hours.

@onobc
Copy link
Contributor Author

onobc commented Jul 17, 2020

@snicoll - I added the auto-config tests for each exporter and simplified the java doc.

@snicoll snicoll self-assigned this Jul 17, 2020
@onobc
Copy link
Contributor Author

onobc commented Jul 17, 2020

Is the failure around RestAssuredRestDocsAutoConfigurationAdvancedConfigurationIntegrationTests expected? Looks like its getting a 401 and when i hit the endpoint that its trying to hit I see a form based login screen appear. I don't think anything in this merge request could affect that.

@snicoll
Copy link
Member

snicoll commented Jul 17, 2020

Same. Don't worry about it, I'll investigate as part of polishing this change.

@snicoll snicoll changed the title Add global metrics exporter disable property Add support for easily disabling metrics export Jul 17, 2020
@snicoll
Copy link
Member

snicoll commented Jul 17, 2020

I don't think anything in this merge request could affect that.

Actually, it does indirectly as the actuator (and it's auto-configuration) is now added to the project. I don't know why that happens but on the other hand Actuator does not seem required. I'll investigate a bit more.

@onobc
Copy link
Contributor Author

onobc commented Jul 17, 2020 via email

@onobc
Copy link
Contributor Author

onobc commented Jul 19, 2020

@snicoll did you get a chance to follow up on the issue? If not, I can take a look in the next 24hrs.

@snicoll
Copy link
Member

snicoll commented Jul 20, 2020

I've removed actuator as it's not required although I might change my mind and write a test with an actual registry implementation to validate its auto-configuration isn't enabled. I am on PTO the next 2 days and I've already started to polish things quite a bit in a branch. If you want to investigate how to prevent security to kick in the way it does, I am definitely interested. Thanks!

@onobc
Copy link
Contributor Author

onobc commented Jul 20, 2020 via email

@onobc
Copy link
Contributor Author

onobc commented Jul 22, 2020

I just got around to look into this now.

  1. Yes, you are correct, we do not need those dependencies in there. I believe I had them in there as I was thinking initially of a heavier integration test - however ended up just verifying the expected properties were set etc. I forgot to remove the deps once I switched gears on that.

  2. The security is kicking in due to ManagementWebSecurityAutoConfiguration being available on classpath now. If you look at the RestDocsTestApplication used by the IT you will notice it was already excluding SecurityAutoConfiguration. The test goes green when we add the ManagementWebSecurityAutoConfiguration to the exclude list.

@SpringBootApplication(exclude = { SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class })

snicoll pushed a commit to snicoll/spring-boot that referenced this pull request Jul 24, 2020
This commit introduces a new property to globally disable metrics
export. In integration tests, this property is automatically set to
disable everything but in-memory metrics.

This commit also introduces a `@AutoConfigureMetrics` annotation that
can be used for integration tests that require metrics export to operate
as they would in an application.

See spring-projectsgh-21658
snicoll added a commit to snicoll/spring-boot that referenced this pull request Jul 24, 2020
@snicoll snicoll changed the title Add support for easily disabling metrics export Disable metrics export in integration tests Jul 24, 2020
@snicoll snicoll changed the title Disable metrics export in integration tests Add support for easily disabling metrics export Jul 24, 2020
@snicoll snicoll modified the milestones: 2.4.x, 2.4.0-M2 Jul 24, 2020
snicoll pushed a commit that referenced this pull request Jul 24, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
This commit introduces a new property to globally disable metrics
export. In integration tests, this property is automatically set to
disable everything but in-memory metrics.

This commit also introduces a `@AutoConfigureMetrics` annotation that
can be used for integration tests that require metrics export to operate
as they would in an application.

See gh-21658
snicoll added a commit that referenced this pull request Jul 24, 2020
@snicoll snicoll closed this in db462c4 Jul 24, 2020
@snicoll
Copy link
Member

snicoll commented Jul 24, 2020

@Bono007 thank you very much for making your first contribution to Spring Boot. I've polished your contribution in
3530ac9.

@onobc
Copy link
Contributor Author

onobc commented Jul 24, 2020

Ha! Its not my first contribution to Spring Boot. But thank you for the warm welcome ;)

@wilkinsona
Copy link
Member

wilkinsona commented Jul 24, 2020

This is your third first contribution no less 😬. Sorry about that. IIRC, GitHub kept listing you as a first-time contributor for a while. That's no longer the case so hopefully this will be your last first contribution.

@snicoll
Copy link
Member

snicoll commented Jul 25, 2020

I know something was off when I saw the badge but I decided to go with the warm welcome anyway. Thank you for making your third first contribution, indeed 😂

@onobc
Copy link
Contributor Author

onobc commented Jul 26, 2020

3rd times a charm - so they say. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for easily disabling metrics export
4 participants