-
Notifications
You must be signed in to change notification settings - Fork 38.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
3415b04
commit a82659c
Showing
6 changed files
with
194 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...ework/test/context/observation/MicrometerObservationThreadLocalTestExecutionListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 2002-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.test.context.observation; | ||
|
||
import io.micrometer.observation.ObservationRegistry; | ||
import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor; | ||
|
||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.core.Conventions; | ||
import org.springframework.test.context.TestContext; | ||
import org.springframework.test.context.TestExecutionListener; | ||
import org.springframework.test.context.support.AbstractTestExecutionListener; | ||
|
||
/** | ||
* {@code ObservationThreadLocalTestExecutionListener} is an implementation of the {@link TestExecutionListener} | ||
* SPI that updates the {@link ObservationThreadLocalAccessor} with the {@link ObservationRegistry} | ||
* taken from the {@link ApplicationContext} present in the {@link TestContext}. | ||
* | ||
* <p>This implementation is not thread-safe. | ||
* | ||
* @author Marcin Grzejszczak | ||
* @since 6.1 | ||
*/ | ||
public class MicrometerObservationThreadLocalTestExecutionListener extends AbstractTestExecutionListener { | ||
|
||
/** | ||
* Attribute name for a {@link TestContext} attribute which contains the previously | ||
* set {@link ObservationRegistry} on the {@link ObservationThreadLocalAccessor}. | ||
* <p>After all tests from the current test class have completed, the previously stored {@link ObservationRegistry} | ||
* will be restored. If tests are ran concurrently this might cause issues | ||
* unless the {@link ObservationRegistry} is always the same (which should be the case most frequently). | ||
*/ | ||
private static final String PREVIOUS_OBSERVATION_REGISTRY = Conventions.getQualifiedAttributeName( | ||
MicrometerObservationThreadLocalTestExecutionListener.class, "previousObservationRegistry"); | ||
|
||
/** | ||
* Retrieves the current {@link ObservationRegistry} stored | ||
* on {@link ObservationThreadLocalAccessor} instance and stores it | ||
* in the {@link TestContext} attributes and overrides it with | ||
* one stored in {@link ApplicationContext} associated with | ||
* the {@link TestContext}. | ||
* @param testContext the test context for the test; never {@code null} | ||
*/ | ||
@Override | ||
public void beforeTestMethod(TestContext testContext) { | ||
testContext.setAttribute(PREVIOUS_OBSERVATION_REGISTRY, | ||
ObservationThreadLocalAccessor.getInstance().getObservationRegistry()); | ||
testContext.getApplicationContext() | ||
.getBeanProvider(ObservationRegistry.class) | ||
.ifAvailable(observationRegistry -> | ||
ObservationThreadLocalAccessor.getInstance() | ||
.setObservationRegistry(observationRegistry)); | ||
} | ||
|
||
/** | ||
* Retrieves the previously stored {@link ObservationRegistry} and sets it back | ||
* on the {@link ObservationThreadLocalAccessor} instance. | ||
* @param testContext the test context for the test; never {@code null} | ||
*/ | ||
@Override | ||
public void afterTestMethod(TestContext testContext) { | ||
ObservationRegistry previousObservationRegistry = | ||
(ObservationRegistry) testContext.getAttribute(PREVIOUS_OBSERVATION_REGISTRY); | ||
if (previousObservationRegistry != null) { | ||
ObservationThreadLocalAccessor.getInstance() | ||
.setObservationRegistry(previousObservationRegistry); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Returns {@code 3500}. | ||
*/ | ||
@Override | ||
public final int getOrder() { | ||
return 3500; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
spring-test/src/main/java/org/springframework/test/context/observation/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Observation support classes for the <em>Spring TestContext Framework</em>. | ||
*/ | ||
@NonNullApi | ||
@NonNullFields | ||
package org.springframework.test.context.observation; | ||
|
||
import org.springframework.lang.NonNullApi; | ||
import org.springframework.lang.NonNullFields; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
.../test/context/observation/MicrometerObservationThreadLocalTestExecutionListenerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2002-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.test.context.observation; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import io.micrometer.observation.ObservationRegistry; | ||
import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.context.support.StaticApplicationContext; | ||
import org.springframework.test.context.TestContext; | ||
|
||
import static org.assertj.core.api.BDDAssertions.then; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.BDDMockito.willAnswer; | ||
import static org.mockito.Mockito.mock; | ||
|
||
class MicrometerObservationThreadLocalTestExecutionListenerTests { | ||
|
||
ObservationRegistry originalObservationRegistry = ObservationThreadLocalAccessor.getInstance().getObservationRegistry(); | ||
|
||
TestContext testContext = mock(); | ||
|
||
StaticApplicationContext applicationContext = new StaticApplicationContext(); | ||
|
||
Map<String, Object> attributes = new HashMap<>(); | ||
|
||
MicrometerObservationThreadLocalTestExecutionListener listener = new MicrometerObservationThreadLocalTestExecutionListener(); | ||
|
||
@BeforeEach | ||
void setup() { | ||
willAnswer(invocation -> attributes.put(invocation.getArgument(0), invocation.getArgument(1))).given(testContext).setAttribute(anyString(), any()); | ||
given(testContext.getAttribute(anyString())).willAnswer(invocation -> attributes.get(invocation.getArgument(0, String.class))); | ||
given(testContext.getApplicationContext()).willReturn(applicationContext); | ||
} | ||
|
||
@Test | ||
void observationRegistryShouldNotBeOverridden() throws Exception { | ||
listener.beforeTestMethod(testContext); | ||
thenObservationRegistryOnOTLAIsSameAsOriginal(); | ||
listener.afterTestMethod(testContext); | ||
thenObservationRegistryOnOTLAIsSameAsOriginal(); | ||
} | ||
|
||
@Test | ||
void observationRegistryOverriddenByBeanFromTestContext() throws Exception { | ||
ObservationRegistry newObservationRegistry = ObservationRegistry.create(); | ||
applicationContext.getDefaultListableBeanFactory().registerSingleton("observationRegistry", newObservationRegistry); | ||
|
||
listener.beforeTestMethod(testContext); | ||
ObservationRegistry otlaObservationRegistry = ObservationThreadLocalAccessor.getInstance().getObservationRegistry(); | ||
then(otlaObservationRegistry) | ||
.as("During the test we want the original ObservationRegistry to be replaced with the one present in this application context") | ||
.isNotSameAs(originalObservationRegistry) | ||
.isSameAs(newObservationRegistry); | ||
|
||
listener.afterTestMethod(testContext); | ||
thenObservationRegistryOnOTLAIsSameAsOriginal(); | ||
} | ||
|
||
private void thenObservationRegistryOnOTLAIsSameAsOriginal() { | ||
then(ObservationThreadLocalAccessor.getInstance().getObservationRegistry()).isSameAs(originalObservationRegistry); | ||
} | ||
|
||
} |