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

Introduce TestExecutionListener for Micrometer's ObservationRegistry in the TestContext framework #30658

Conversation

marcingrzejszczak
Copy link
Contributor

supersedes #30651

marcingrzejszczak and others added 10 commits June 12, 2023 20:25
without this change we have no option to pass the ObservationRegistry that is registered in the given TestContext's ApplicationContext as the one that should be used by the ObservationThreadLocalAccessor.

with this change we create a TestExecutionListener that will set an ObservationRegistry registered in the TestContext's ApplicationContext for each test class and after each test class will restore the previously stored ObservationRegistry instance
…ervation/ObservationThreadLocalTestListener.java

Co-authored-by: Sam Brannen <sam@sambrannen.com>
…ervation/ObservationThreadLocalTestListener.java

Co-authored-by: Sam Brannen <sam@sambrannen.com>
…ervation/ObservationThreadLocalTestListener.java

Co-authored-by: Sam Brannen <sam@sambrannen.com>
…ervation/package-info.java

Co-authored-by: Sam Brannen <sam@sambrannen.com>
…ervation/ObservationThreadLocalTestListenerTests.java

Co-authored-by: Sam Brannen <sam@sambrannen.com>
- dependencies ordered alphabetically
- renamed the test execution listener to contain "execution" and "micrometer" in the name
- production code lines are smaller (up to 100 chars)
- test execution attribute is a private static final field
- added javadocs in the test execution listener
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 13, 2023
@sbrannen sbrannen changed the title Introduce ObservationThreadLocalTestExecutionListener in the TestContext framework Introduce MicrometerObservationThreadLocalTestExecutionListener in the TestContext framework Jun 13, 2023
@sbrannen sbrannen changed the title Introduce MicrometerObservationThreadLocalTestExecutionListener in the TestContext framework Introduce TestExecutionListener for MicrometerObservationThreadLocalTestExecutionListener` in the TestContext framework Jun 13, 2023
@sbrannen sbrannen changed the title Introduce TestExecutionListener for MicrometerObservationThreadLocalTestExecutionListener` in the TestContext framework Introduce TestExecutionListener for Micrometer's ObservationThreadLocalAccessor in the TestContext framework Jun 13, 2023
@sbrannen sbrannen self-assigned this Jun 13, 2023
@sbrannen sbrannen added in: test Issues in the test module type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jun 13, 2023
@sbrannen sbrannen added this to the 6.1.0-M2 milestone Jun 13, 2023
@snicoll snicoll removed this from the 6.1.0-M2 milestone Jun 14, 2023
@snicoll snicoll added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 14, 2023
@sbrannen sbrannen removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 14, 2023
@sbrannen sbrannen added this to the 6.0.10 milestone Jun 14, 2023
sbrannen pushed a commit that referenced this pull request Jun 14, 2023
Prior to this commit, there was no way to specify the
ObservationRegistry that is registered in the given test's
ApplicationContext as the one that should be used by Micrometer's
ObservationThreadLocalAccessor for context propagation.

This commit introduces a TestExecutionListener for Micrometer's
ObservationRegistry in the Spring TestContext Framework. Specifically,
this listener obtains the ObservationRegistry registered in the test's
ApplicationContext, stores it in ObservationThreadLocalAccessor for the
duration of each test method execution, and restores the original
ObservationRegistry in ObservationThreadLocalAccessor after each test.

Co-authored-by: Sam Brannen <sam@sambrannen.com>
See gh-30658
@sbrannen sbrannen closed this in aa20281 Jun 14, 2023
@sbrannen sbrannen changed the title Introduce TestExecutionListener for Micrometer's ObservationThreadLocalAccessor in the TestContext framework Introduce TestExecutionListener for Micrometer's ObservationRegistry in the TestContext framework Jun 14, 2023
@marcingrzejszczak marcingrzejszczak deleted the otla_test_listener_2 branch June 14, 2023 14:59
mdeinum pushed a commit to mdeinum/spring-framework that referenced this pull request Jun 29, 2023
Prior to this commit, there was no way to specify the
ObservationRegistry that is registered in the given test's
ApplicationContext as the one that should be used by Micrometer's
ObservationThreadLocalAccessor for context propagation.

This commit introduces a TestExecutionListener for Micrometer's
ObservationRegistry in the Spring TestContext Framework. Specifically,
this listener obtains the ObservationRegistry registered in the test's
ApplicationContext, stores it in ObservationThreadLocalAccessor for the
duration of each test method execution, and restores the original
ObservationRegistry in ObservationThreadLocalAccessor after each test.

Co-authored-by: Sam Brannen <sam@sambrannen.com>
See spring-projectsgh-30658
mdeinum pushed a commit to mdeinum/spring-framework that referenced this pull request Jun 29, 2023
This commit overhauls the TestExecutionListener for Micrometer's
ObservationRegistry that was introduced in the previous commit.

Specifically, this commit:

- Renames the listener to MicrometerObservationRegistryTestExecutionListener
  since the use of a ThreadLocal is an implementation detail that may
  change over time.

- Makes the listener package-private instead of public in order to
  allow the team greater flexibility in evolving this feature.

- Eagerly loads the ObservationThreadLocalAccessor class and verifies
  that it has a getObservationRegistry() method to ensure that the
  listener is properly skipped when SpringFactoriesLoader attempts to
  load it, if Micrometer 1.10.8+ is not on the classpath.

- Switches the listener's automatic registration order to 2500 in order
  to register it after the DependencyInjectionTestExecutionListener.

- Only tracks the previous ObservationRegistry in beforeTestMethod() if
  the test's ApplicationContext contains an ObservationRegistry bean.

- Properly removes the TestContext attribute for the previous
  ObservationRegistry in afterTestMethod().

- Introduces DEBUG logging for diagnostics.

- Adds an entry in the Javadoc for TestExecutionListener as well as in
  the Testing chapter in the reference manual.

Closes spring-projectsgh-30658
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants