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:
Oliver Drotbohm
2022-12-22 20:43:39 +01:00
parent 9dff5cc973
commit c1fc3032b7
34 changed files with 847 additions and 150 deletions

View File

@@ -0,0 +1,66 @@
/*
* 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.runtime;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
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 once per
* application class.
*
* @author Oliver Drotbohm
*/
@RequiredArgsConstructor
public class ApplicationModulesRuntime implements Supplier<ApplicationModules> {
private final @NonNull Supplier<ApplicationModules> modules;
private final @NonNull ApplicationRuntime runtime;
/*
* (non-Javadoc)
* @see java.util.function.Supplier#get()
*/
@Override
public ApplicationModules get() {
return modules.get();
}
/**
* Returns whether a given {@link Class} is considered an application one (versus Framework ones).
*
* @param type
* @return
*/
public boolean isApplicationClass(Class<?> type) {
return runtime.isApplicationClass(type);
}
/**
* Returns the actual user class for a given bean and bean name.
*
* @param bean must not be {@literal null}.
* @param beanName must not be {@literal null} or empty.
* @return will never be {@literal null}.
*/
public Class<?> getUserClass(Object bean, String beanName) {
return runtime.getUserClass(bean, beanName);
}
}

View File

@@ -0,0 +1,57 @@
/*
* 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.runtime;
/**
* 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 will never be {@literal null}.
*/
String getId();
/**
* Returns the primary application class.
*
* @return will never be {@literal null}.
*/
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 must not be {@literal null}.
* @param beanName must not be {@literal null} or empty.
* @return will never be {@literal null}.
*/
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 must not be {@literal null}.
* @return whether the given type is an application class, i.e. user code in one of the application packages.
*/
boolean isApplicationClass(Class<?> type);
}

View File

@@ -0,0 +1,92 @@
/*
* 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.runtime.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.runtime.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() {
var 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) {
var 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));
});
}
}

View File

@@ -0,0 +1,92 @@
/*
* 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.runtime.autoconfigure;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.function.Supplier;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.modulith.model.ApplicationModule;
import org.springframework.modulith.model.ApplicationModules;
import org.springframework.modulith.runtime.ApplicationModulesRuntime;
import org.springframework.modulith.runtime.ApplicationRuntime;
/**
* Auto-configuration to register a {@link SpringBootApplicationRuntime} and {@link ApplicationModulesRuntime} as Spring
* Bean.
*
* @author Oliver Drotbohm
*/
@Slf4j
@AutoConfiguration
class SpringModulithRuntimeAutoConfiguration {
@Bean
@ConditionalOnMissingBean(ApplicationRuntime.class)
SpringBootApplicationRuntime modulithsApplicationRuntime(ApplicationContext context) {
return new SpringBootApplicationRuntime(context);
}
@Bean
@ConditionalOnMissingBean
ApplicationModulesRuntime modulesRuntime(ApplicationRuntime runtime) {
var mainClass = runtime.getMainApplicationClass();
var modules = Executors.newFixedThreadPool(1)
.submit(() -> SpringModulithRuntimeAutoConfiguration.initializeApplicationModules(mainClass));
return new ApplicationModulesRuntime(toSupplier(modules), runtime);
}
private static ApplicationModules initializeApplicationModules(Class<?> applicationMainClass) {
LOG.debug("Obtaining Spring Modulith application modules…");
var result = ApplicationModules.of(applicationMainClass);
var numberOfModules = result.stream().count();
if (numberOfModules == 0) {
LOG.warn("No application modules detected!");
} else {
LOG.debug("Detected {} application modules: {}", //
result.stream().count(), //
result.stream().map(ApplicationModule::getName).toList());
}
return result;
}
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
}
};
}
}

View File

@@ -0,0 +1 @@
org.springframework.modulith.runtime.autoconfigure.SpringModulithRuntimeAutoConfiguration

View File

@@ -0,0 +1,49 @@
/*
* 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.runtime.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.runtime.ApplicationRuntime;
/**
* Unit tests for {@link SpringBootApplicationRuntime}.
*
* @author Oliver Drotbohm
*/
@ExtendWith(MockitoExtension.class)
class SpringBootApplicationRuntimeUnitTests {
@Mock ApplicationContext context;
@Test // GH-87
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 {}
}

View File

@@ -0,0 +1,47 @@
/*
* 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.runtime.autoconfigure;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.modulith.runtime.ApplicationModulesRuntime;
import org.springframework.modulith.runtime.ApplicationRuntime;
/**
* Integration thest for {@link SpringModulithRuntimeAutoConfiguration}.
*
* @author Oliver Drotbohm
*/
@SpringBootTest
class SpringModulithRuntimeAutoConfigurationIntegrationTests {
@SpringBootApplication
static class SampleApp {}
@Autowired ApplicationContext context;
@Test // GH-87
void bootstrapRegistersRuntimeInstances() {
assertThat(context.getBean(ApplicationRuntime.class)).isNotNull();
assertThat(context.getBean(ApplicationModulesRuntime.class)).isNotNull();
}
}