GH-87 - Expose module structure as actuator endpoint.
Introduce Spring Modulith Actuator module to expose the application module structure as Spring Boot actuator. This required a Spring Modulith Runtime module to be extracted from the Spring Modulith Observability one. The former now contains the auto-configured ApplicationRuntime and ApplicationModulesRuntime bean instances to be able to bootstrap a ApplicationModules asynchronously on application startup. The actuator module then contains a Spring Boot @Endpoint implementation consuming the ApplicationModules to render JSON describing the application module structure.
This commit is contained in:
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
* Copyright 2022 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;
|
||||
|
||||
/**
|
||||
* Abstraction of the application runtime environment. Primarily to keep references to Spring Boot out of the core
|
||||
* observability implementation.
|
||||
*
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
public interface ApplicationRuntime {
|
||||
|
||||
/**
|
||||
* Returns the identifier of the application.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String getId();
|
||||
|
||||
/**
|
||||
* Returns the primary application class.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Class<?> getMainApplicationClass();
|
||||
|
||||
/**
|
||||
* Obtain the end user class for the given bean and bean name. Necessary to reveal the actual user type from
|
||||
* potentially proxied instances.
|
||||
*
|
||||
* @param bean
|
||||
* @param beanName
|
||||
* @return
|
||||
*/
|
||||
Class<?> getUserClass(Object bean, String beanName);
|
||||
|
||||
/**
|
||||
* Returns whether the given type is an application class, i.e. user code in one of the application packages.
|
||||
*
|
||||
* @param type
|
||||
* @return
|
||||
* @see #getApplicationPackages()
|
||||
*/
|
||||
boolean isApplicationClass(Class<?> type);
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.PayloadApplicationEvent;
|
||||
import org.springframework.modulith.model.ApplicationModule;
|
||||
import org.springframework.modulith.runtime.ApplicationModulesRuntime;
|
||||
|
||||
/**
|
||||
* @author Oliver Drotbohm
|
||||
@@ -32,7 +33,7 @@ import org.springframework.modulith.model.ApplicationModule;
|
||||
@RequiredArgsConstructor
|
||||
public class ModuleEventListener implements ApplicationListener<ApplicationEvent> {
|
||||
|
||||
private final ModulesRuntime modules;
|
||||
private final ApplicationModulesRuntime modules;
|
||||
private final Supplier<Tracer> tracer;
|
||||
|
||||
/*
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.springframework.aop.support.StaticMethodMatcher;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.modulith.model.ApplicationModules;
|
||||
import org.springframework.modulith.runtime.ApplicationModulesRuntime;
|
||||
|
||||
/**
|
||||
* @author Oliver Drotbohm
|
||||
@@ -40,11 +41,11 @@ public class ModuleTracingBeanPostProcessor extends ModuleTracingSupport impleme
|
||||
|
||||
public static final String MODULE_BAGGAGE_KEY = "org.springframework.modulith.module";
|
||||
|
||||
private final ApplicationRuntime runtime;
|
||||
private final ApplicationModulesRuntime runtime;
|
||||
private final Tracer tracer;
|
||||
private final Map<String, Advisor> advisors = new HashMap<>();
|
||||
|
||||
public ModuleTracingBeanPostProcessor(ApplicationRuntime runtime, Tracer tracer) {
|
||||
public ModuleTracingBeanPostProcessor(ApplicationModulesRuntime runtime, Tracer tracer) {
|
||||
|
||||
super(runtime);
|
||||
|
||||
@@ -65,7 +66,7 @@ public class ModuleTracingBeanPostProcessor extends ModuleTracingSupport impleme
|
||||
return bean;
|
||||
}
|
||||
|
||||
ApplicationModules modules = getModules();
|
||||
ApplicationModules modules = runtime.get();
|
||||
|
||||
return modules.getModuleByType(type.getName())
|
||||
.map(DefaultObservedModule::new)
|
||||
|
||||
@@ -16,13 +16,12 @@
|
||||
package org.springframework.modulith.observability;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.aop.Advisor;
|
||||
import org.springframework.aop.framework.Advised;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.modulith.model.ApplicationModules;
|
||||
import org.springframework.modulith.runtime.ApplicationModulesRuntime;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
@@ -30,16 +29,14 @@ import org.springframework.util.Assert;
|
||||
*/
|
||||
class ModuleTracingSupport implements BeanClassLoaderAware {
|
||||
|
||||
private final Supplier<ApplicationModules> modules;
|
||||
private final ApplicationRuntime context;
|
||||
private final ApplicationModulesRuntime runtime;
|
||||
private ClassLoader classLoader;
|
||||
|
||||
protected ModuleTracingSupport(ApplicationRuntime context) {
|
||||
protected ModuleTracingSupport(ApplicationModulesRuntime runtime) {
|
||||
|
||||
Assert.notNull(context, "ApplicationContext must not be null!");
|
||||
Assert.notNull(runtime, "ModulesRuntime must not be null!");
|
||||
|
||||
this.modules = ModulesRuntime.of(context);
|
||||
this.context = context;
|
||||
this.runtime = runtime;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -51,17 +48,8 @@ class ModuleTracingSupport implements BeanClassLoaderAware {
|
||||
this.classLoader = classLoader;
|
||||
}
|
||||
|
||||
protected final ApplicationModules getModules() {
|
||||
|
||||
try {
|
||||
return modules.get();
|
||||
} catch (Exception o_O) {
|
||||
throw new RuntimeException(o_O);
|
||||
}
|
||||
}
|
||||
|
||||
protected final Class<?> getBeanUserClass(Object bean, String beanName) {
|
||||
return context.getUserClass(bean, beanName);
|
||||
return runtime.getUserClass(bean, beanName);
|
||||
}
|
||||
|
||||
protected final Object addAdvisor(Object bean, Advisor advisor) {
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
/*
|
||||
* Copyright 2022 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 lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.modulith.model.ApplicationModules;
|
||||
|
||||
/**
|
||||
* Bootstrap type to make sure we only bootstrap the initialization of a {@link ApplicationModules} instance per application class
|
||||
* once.
|
||||
*
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class ModulesRuntime implements Supplier<ApplicationModules> {
|
||||
|
||||
private static final Map<String, ModulesRuntime> MODULES = new HashMap<>();
|
||||
|
||||
private final Supplier<ApplicationModules> modules;
|
||||
private final ApplicationRuntime runtime;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.util.function.Supplier#get()
|
||||
*/
|
||||
@Override
|
||||
public ApplicationModules get() {
|
||||
return modules.get();
|
||||
}
|
||||
|
||||
boolean isApplicationClass(Class<?> type) {
|
||||
return runtime.isApplicationClass(type);
|
||||
}
|
||||
|
||||
public static ModulesRuntime of(ApplicationRuntime runtime) {
|
||||
|
||||
return MODULES.computeIfAbsent(runtime.getId(), it -> {
|
||||
|
||||
Class<?> mainClass = runtime.getMainApplicationClass();
|
||||
Future<ApplicationModules> modules = Executors.newFixedThreadPool(1).submit(() -> ApplicationModules.of(mainClass));
|
||||
|
||||
return new ModulesRuntime(toSupplier(modules), runtime);
|
||||
});
|
||||
}
|
||||
|
||||
private static Supplier<ApplicationModules> toSupplier(Future<ApplicationModules> modules) {
|
||||
|
||||
return () -> {
|
||||
try {
|
||||
return modules.get();
|
||||
} catch (Exception o_O) {
|
||||
throw new RuntimeException(o_O);
|
||||
// TODO: handle exception
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,8 @@ package org.springframework.modulith.observability;
|
||||
import io.micrometer.tracing.Tracer;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.aopalliance.aop.Advice;
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
@@ -30,6 +32,7 @@ import org.springframework.data.rest.webmvc.BasePathAwareController;
|
||||
import org.springframework.data.rest.webmvc.RootResourceInformation;
|
||||
import org.springframework.modulith.model.ApplicationModule;
|
||||
import org.springframework.modulith.model.ApplicationModules;
|
||||
import org.springframework.modulith.runtime.ApplicationModulesRuntime;
|
||||
|
||||
/**
|
||||
* @author Oliver Drotbohm
|
||||
@@ -37,9 +40,9 @@ import org.springframework.modulith.model.ApplicationModules;
|
||||
public class SpringDataRestModuleTracingBeanPostProcessor extends ModuleTracingSupport implements BeanPostProcessor {
|
||||
|
||||
private final Tracer tracer;
|
||||
private final ApplicationRuntime runtime;
|
||||
private final ApplicationModulesRuntime runtime;
|
||||
|
||||
public SpringDataRestModuleTracingBeanPostProcessor(ApplicationRuntime runtime, Tracer tracer) {
|
||||
public SpringDataRestModuleTracingBeanPostProcessor(ApplicationModulesRuntime runtime, Tracer tracer) {
|
||||
|
||||
super(runtime);
|
||||
|
||||
@@ -60,7 +63,7 @@ public class SpringDataRestModuleTracingBeanPostProcessor extends ModuleTracingS
|
||||
return bean;
|
||||
}
|
||||
|
||||
Advice interceptor = new DataRestControllerInterceptor(getModules(), tracer);
|
||||
Advice interceptor = new DataRestControllerInterceptor(runtime, tracer);
|
||||
Advisor advisor = new DefaultPointcutAdvisor(interceptor);
|
||||
|
||||
return addAdvisor(bean, advisor, it -> it.setProxyTargetClass(true));
|
||||
@@ -69,7 +72,7 @@ public class SpringDataRestModuleTracingBeanPostProcessor extends ModuleTracingS
|
||||
@RequiredArgsConstructor
|
||||
private static class DataRestControllerInterceptor implements MethodInterceptor {
|
||||
|
||||
private final ApplicationModules modules;
|
||||
private final Supplier<ApplicationModules> modules;
|
||||
private final Tracer tracer;
|
||||
|
||||
/*
|
||||
@@ -100,7 +103,7 @@ public class SpringDataRestModuleTracingBeanPostProcessor extends ModuleTracingS
|
||||
|
||||
RootResourceInformation info = (RootResourceInformation) argument;
|
||||
|
||||
return modules.getModuleByType(info.getDomainType().getName()).orElse(null);
|
||||
return modules.get().getModuleByType(info.getDomainType().getName()).orElse(null);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
@@ -27,13 +27,11 @@ import io.micrometer.tracing.Tracer;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.modulith.observability.ApplicationRuntime;
|
||||
import org.springframework.modulith.observability.ModuleEventListener;
|
||||
import org.springframework.modulith.observability.ModuleTracingBeanPostProcessor;
|
||||
import org.springframework.modulith.observability.ModulesRuntime;
|
||||
import org.springframework.modulith.runtime.ApplicationModulesRuntime;
|
||||
|
||||
/**
|
||||
* @author Oliver Drotbohm
|
||||
@@ -43,19 +41,15 @@ import org.springframework.modulith.observability.ModulesRuntime;
|
||||
class ModuleObservabilityAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
static SpringBootApplicationRuntime modulithsApplicationRuntime(ApplicationContext context) {
|
||||
return new SpringBootApplicationRuntime(context);
|
||||
}
|
||||
|
||||
@Bean
|
||||
static ModuleTracingBeanPostProcessor moduleTracingBeanPostProcessor(ApplicationRuntime runtime,
|
||||
static ModuleTracingBeanPostProcessor moduleTracingBeanPostProcessor(ApplicationModulesRuntime runtime,
|
||||
Tracer tracer) {
|
||||
return new ModuleTracingBeanPostProcessor(runtime, tracer);
|
||||
}
|
||||
|
||||
@Bean
|
||||
static ModuleEventListener tracingModuleEventListener(ApplicationRuntime runtime, ObjectProvider<Tracer> tracer) {
|
||||
return new ModuleEventListener(ModulesRuntime.of(runtime), () -> tracer.getObject());
|
||||
static ModuleEventListener tracingModuleEventListener(ApplicationModulesRuntime runtime,
|
||||
ObjectProvider<Tracer> tracer) {
|
||||
return new ModuleEventListener(runtime, () -> tracer.getObject());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,92 +0,0 @@
|
||||
/*
|
||||
* Copyright 2022 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.autoconfigure;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.modulith.observability.ApplicationRuntime;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
class SpringBootApplicationRuntime implements ApplicationRuntime {
|
||||
|
||||
private static final Map<String, Boolean> APPLICATION_CLASSES = new ConcurrentHashMap<>();
|
||||
|
||||
private final ApplicationContext context;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.modulith.observability.ApplicationRuntime#getId()
|
||||
*/
|
||||
@Override
|
||||
public String getId() {
|
||||
return context.getId();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.modulith.observability.ApplicationRuntime#getApplicationClass()
|
||||
*/
|
||||
@Override
|
||||
public Class<?> getMainApplicationClass() {
|
||||
|
||||
String[] mainBeanNames = context.getBeanNamesForAnnotation(SpringBootApplication.class);
|
||||
|
||||
return context.getType(mainBeanNames[0]);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.modulith.observability.ApplicationRuntime#getBeanUserClass(java.lang.Object, java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public Class<?> getUserClass(Object bean, String beanName) {
|
||||
|
||||
Class<?> beanType = context.containsBean(beanName)
|
||||
? context.getType(beanName)
|
||||
: bean.getClass();
|
||||
|
||||
return ClassUtils.getUserClass(beanType);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.modulith.observability.ApplicationRuntime#isApplicationClass(java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public boolean isApplicationClass(Class<?> type) {
|
||||
|
||||
return APPLICATION_CLASSES.computeIfAbsent(type.getName(),
|
||||
it -> {
|
||||
|
||||
if (it.startsWith("org.springframework")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return it.startsWith(getMainApplicationClass().getPackage().getName())
|
||||
|| AutoConfigurationPackages.get(context).stream().anyMatch(pkg -> it.startsWith(pkg));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -21,8 +21,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.rest.webmvc.RepositoryController;
|
||||
import org.springframework.modulith.observability.ApplicationRuntime;
|
||||
import org.springframework.modulith.observability.SpringDataRestModuleTracingBeanPostProcessor;
|
||||
import org.springframework.modulith.runtime.ApplicationModulesRuntime;
|
||||
|
||||
/**
|
||||
* @author Oliver Drotbohm
|
||||
@@ -33,7 +33,7 @@ class SpringDataRestModuleObservabilityAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
static SpringDataRestModuleTracingBeanPostProcessor springDataRestModuleTracingBeanPostProcessor(
|
||||
ApplicationRuntime runtime, Tracer tracer) {
|
||||
ApplicationModulesRuntime runtime, Tracer tracer) {
|
||||
return new SpringDataRestModuleTracingBeanPostProcessor(runtime, tracer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
/*
|
||||
* Copyright 2022 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.autoconfigure;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.modulith.observability.ApplicationRuntime;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link SpringBootApplicationRuntime}.
|
||||
*
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class SpringBootApplicationRuntimeUnitTests {
|
||||
|
||||
@Mock ApplicationContext context;
|
||||
|
||||
@Test
|
||||
void extractsUserTypeFromClassBasedProxy() {
|
||||
|
||||
Object proxy = new ProxyFactory(new Sample()).getProxy();
|
||||
ApplicationRuntime runtime = new SpringBootApplicationRuntime(context);
|
||||
|
||||
assertThat(proxy.getClass()).isNotEqualTo(Sample.class);
|
||||
assertThat(runtime.getUserClass(proxy, "sample")).isEqualTo(Sample.class);
|
||||
}
|
||||
|
||||
static class Sample {}
|
||||
}
|
||||
Reference in New Issue
Block a user