Introduce TestExecutionListener for Micrometer ObservationRegistry

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
This commit is contained in:
Marcin Grzejszczak
2023-06-12 20:25:38 +02:00
committed by Sam Brannen
parent 3415b04c73
commit a82659c837
6 changed files with 194 additions and 2 deletions

View File

@@ -50,6 +50,8 @@ dependencies {
optional("io.projectreactor:reactor-test")
optional("org.jetbrains.kotlinx:kotlinx-coroutines-core")
optional("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
optional('io.micrometer:context-propagation')
optional('io.micrometer:micrometer-observation')
testImplementation(project(":spring-core-test"))
testImplementation(project(":spring-context-support"))
testImplementation(project(":spring-oxm"))
@@ -58,7 +60,6 @@ dependencies {
testImplementation(testFixtures(project(":spring-core")))
testImplementation(testFixtures(project(":spring-tx")))
testImplementation(testFixtures(project(":spring-web")))
testImplementation('io.micrometer:context-propagation')
testImplementation("jakarta.annotation:jakarta.annotation-api")
testImplementation("javax.cache:cache-api")
testImplementation("jakarta.ejb:jakarta.ejb-api")