|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.test.context.observation; |
| 18 | + |
| 19 | +import java.lang.reflect.Method; |
| 20 | + |
| 21 | +import io.micrometer.observation.ObservationRegistry; |
| 22 | +import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor; |
| 23 | +import org.apache.commons.logging.Log; |
| 24 | +import org.apache.commons.logging.LogFactory; |
| 25 | +import org.junit.platform.launcher.TestExecutionListener; |
| 26 | + |
| 27 | +import org.springframework.context.ApplicationContext; |
| 28 | +import org.springframework.core.Conventions; |
| 29 | +import org.springframework.test.context.TestContext; |
| 30 | +import org.springframework.test.context.support.AbstractTestExecutionListener; |
| 31 | +import org.springframework.util.Assert; |
| 32 | +import org.springframework.util.ReflectionUtils; |
| 33 | + |
| 34 | +/** |
| 35 | + * {@code TestExecutionListener} which provides support for Micrometer's |
| 36 | + * {@link ObservationRegistry}. |
| 37 | + * |
| 38 | + * <p>This listener updates the {@link ObservationThreadLocalAccessor} with the |
| 39 | + * {@code ObservationRegistry} obtained from the test's {@link ApplicationContext}, |
| 40 | + * if present. |
| 41 | + * |
| 42 | + * @author Marcin Grzejszczak |
| 43 | + * @author Sam Brannen |
| 44 | + * @since 6.0.10 |
| 45 | + */ |
| 46 | +class MicrometerObservationRegistryTestExecutionListener extends AbstractTestExecutionListener { |
| 47 | + |
| 48 | + private static final Log logger = LogFactory.getLog(MicrometerObservationRegistryTestExecutionListener.class); |
| 49 | + |
| 50 | + private static final String OBSERVATION_THREAD_LOCAL_ACCESSOR_CLASS_NAME = |
| 51 | + "io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor"; |
| 52 | + |
| 53 | + /** |
| 54 | + * Attribute name for a {@link TestContext} attribute which contains the |
| 55 | + * {@link ObservationRegistry} that was previously stored in the |
| 56 | + * {@link ObservationThreadLocalAccessor}. |
| 57 | + * <p>After each test method, the previously stored {@code ObservationRegistry} |
| 58 | + * will be restored. If tests run concurrently this might cause issues unless |
| 59 | + * the {@code ObservationRegistry} is always the same (which should typically |
| 60 | + * be the case). |
| 61 | + */ |
| 62 | + private static final String PREVIOUS_OBSERVATION_REGISTRY = Conventions.getQualifiedAttributeName( |
| 63 | + MicrometerObservationRegistryTestExecutionListener.class, "previousObservationRegistry"); |
| 64 | + |
| 65 | + |
| 66 | + static { |
| 67 | + // Trigger eager resolution of Micrometer Observation types during static |
| 68 | + // initialization of this class to ensure that this listener can be properly |
| 69 | + // skipped when SpringFactoriesLoader attempts to load it, if micrometer-observation |
| 70 | + // is not in the classpath or if the version of ObservationThreadLocalAccessor |
| 71 | + // present does not include the getObservationRegistry() method. |
| 72 | + String errorMessage = |
| 73 | + "MicrometerObservationRegistryTestExecutionListener requires micrometer-observation 1.10.8 or higher"; |
| 74 | + Class<?> clazz; |
| 75 | + try { |
| 76 | + clazz = Class.forName(OBSERVATION_THREAD_LOCAL_ACCESSOR_CLASS_NAME, true, |
| 77 | + TestExecutionListener.class.getClassLoader()); |
| 78 | + } |
| 79 | + catch (Throwable ex) { |
| 80 | + throw new IllegalStateException(errorMessage, ex); |
| 81 | + } |
| 82 | + |
| 83 | + Method method = ReflectionUtils.findMethod(clazz, "getObservationRegistry"); |
| 84 | + Assert.state(method != null, errorMessage); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + /** |
| 89 | + * Returns {@code 2500}. |
| 90 | + */ |
| 91 | + @Override |
| 92 | + public final int getOrder() { |
| 93 | + return 2500; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * If the test's {@link ApplicationContext} contains an {@link ObservationRegistry} |
| 98 | + * bean, this method retrieves the {@code ObservationRegistry} currently stored |
| 99 | + * in {@link ObservationThreadLocalAccessor}, saves a reference to the original |
| 100 | + * registry as a {@link TestContext} attribute (to be restored in |
| 101 | + * {@link #afterTestMethod(TestContext)}), and sets the registry from the test's |
| 102 | + * {@code ApplicationContext} in {@link ObservationThreadLocalAccessor}. |
| 103 | + * @param testContext the test context for the test; never {@code null} |
| 104 | + * @see #afterTestMethod(TestContext) |
| 105 | + */ |
| 106 | + @Override |
| 107 | + public void beforeTestMethod(TestContext testContext) { |
| 108 | + testContext.getApplicationContext().getBeanProvider(ObservationRegistry.class) |
| 109 | + .ifAvailable(registry -> { |
| 110 | + if (logger.isDebugEnabled()) { |
| 111 | + logger.debug(""" |
| 112 | + Registering ObservationRegistry from ApplicationContext in \ |
| 113 | + ObservationThreadLocalAccessor for test class \ |
| 114 | + """ + testContext.getTestClass().getName()); |
| 115 | + } |
| 116 | + ObservationThreadLocalAccessor accessor = ObservationThreadLocalAccessor.getInstance(); |
| 117 | + testContext.setAttribute(PREVIOUS_OBSERVATION_REGISTRY, accessor.getObservationRegistry()); |
| 118 | + accessor.setObservationRegistry(registry); |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Retrieves the original {@link ObservationRegistry} that was saved in |
| 124 | + * {@link #beforeTestMethod(TestContext)} and sets it in |
| 125 | + * {@link ObservationThreadLocalAccessor}. |
| 126 | + * @param testContext the test context for the test; never {@code null} |
| 127 | + * @see #beforeTestMethod(TestContext) |
| 128 | + */ |
| 129 | + @Override |
| 130 | + public void afterTestMethod(TestContext testContext) { |
| 131 | + ObservationRegistry previousObservationRegistry = |
| 132 | + (ObservationRegistry) testContext.removeAttribute(PREVIOUS_OBSERVATION_REGISTRY); |
| 133 | + if (previousObservationRegistry != null) { |
| 134 | + if (logger.isDebugEnabled()) { |
| 135 | + logger.debug("Restoring ObservationRegistry in ObservationThreadLocalAccessor for test class " + |
| 136 | + testContext.getTestClass().getName()); |
| 137 | + } |
| 138 | + ObservationThreadLocalAccessor.getInstance().setObservationRegistry(previousObservationRegistry); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | +} |
0 commit comments