GH-621 - Make sure we always place the ModuleEntryInterceptor after the AsyncAnnotationAdvisor.

To properly create spans for the actual method invocation, not the async dispatch.
This commit is contained in:
Oliver Drotbohm
2024-05-21 23:59:29 +02:00
parent f3b012d97b
commit c80c3e02bb
7 changed files with 141 additions and 19 deletions

View File

@@ -17,10 +17,12 @@ package example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
/**
* @author Oliver Drotbohm
*/
@EnableAsync
@SpringBootApplication
public class ExampleApplication {

View File

@@ -15,6 +15,7 @@
*/
package example.sample;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/**
@@ -23,5 +24,6 @@ import org.springframework.stereotype.Component;
@Component
public class SampleComponent {
void someMethod() {}
@Async
public void someMethod() {}
}

View File

@@ -0,0 +1,87 @@
/*
* Copyright 2024 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.modulith.observability;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import example.ExampleApplication;
import example.sample.SampleComponent;
import io.micrometer.tracing.Tracer;
import org.junit.jupiter.api.Test;
import org.springframework.aop.Advisor;
import org.springframework.aop.framework.Advised;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.modulith.observability.ModuleTracingBeanPostProcessor.ApplicationModuleObservingAdvisor;
import org.springframework.modulith.runtime.ApplicationModulesRuntime;
import org.springframework.modulith.runtime.ApplicationRuntime;
import org.springframework.modulith.test.TestApplicationModules;
import org.springframework.scheduling.annotation.AsyncAnnotationAdvisor;
/**
* Integration tests for {@link ModuleTracingBeanPostProcessor}.
*
* @author Oliver Drotbohm
*/
class ModuleTracingBeanPostProcessorIntegrationTests {
@Test
void decoratesExposedComponentsWithTracingInterceptor() {
SampleComponent bean = SpringApplication
.run(new Class<?>[] { ExampleApplication.class, ModuleTracingConfiguration.class }, new String[] {})
.getBean(SampleComponent.class);
assertThat(bean).isInstanceOfSatisfying(Advised.class, it -> {
var advisors = it.getAdvisors();
var asyncIndex = advisorIndex(AsyncAnnotationAdvisor.class, advisors);
var tracingIndex = advisorIndex(ApplicationModuleObservingAdvisor.class, advisors);
assertThat(tracingIndex).isGreaterThan(asyncIndex);
});
}
@Configuration
static class ModuleTracingConfiguration {
@Bean
ModuleTracingBeanPostProcessor foo(ConfigurableApplicationContext context) {
var runtime = ApplicationRuntime.of(context);
var modulesRuntime = new ApplicationModulesRuntime(() -> TestApplicationModules.of(ExampleApplication.class),
runtime);
return new ModuleTracingBeanPostProcessor(modulesRuntime, () -> mock(Tracer.class), context.getBeanFactory());
}
}
private static int advisorIndex(Class<? extends Advisor> type, Advisor[] advisors) {
for (int i = 0; i < advisors.length; i++) {
if (type.isInstance(advisors[i])) {
return i;
}
}
throw new AssertionError("No advisor of type %s found!".formatted(type.getName()));
}
}

View File

@@ -31,7 +31,7 @@ import org.springframework.modulith.runtime.ApplicationModulesRuntime;
*
* @author Oliver Drotbohm
*/
public class ModuleTracingBeanPostProcessorUnitTests {
class ModuleTracingBeanPostProcessorUnitTests {
@Test // GH-503
void doesNotProxyConfiguationProperties() {

View File

@@ -33,7 +33,7 @@ import org.springframework.util.ReflectionUtils;
*
* @author Oliver Drotbohm
*/
public class ObservedModuleTypeUnitTests {
class ObservedModuleTypeUnitTests {
static final ApplicationModules modules = TestApplicationModules.of("example");