GH-333 - Avoid proxying configuration classes for observability purposes.

We now skip configuration classes for observability-related proxying as it's hard to detect those during the ProxyFactory setup to configure target class proxying. Also, configuration classes are usually no targets of inter-module communication and thus don't need to be observed at all.
This commit is contained in:
Oliver Drotbohm
2023-10-16 21:11:30 +02:00
parent d14ba59837
commit bdd17e834c
4 changed files with 46 additions and 7 deletions

View File

@@ -140,7 +140,7 @@ class DefaultObservedModule implements ObservedModule {
.map(SpringBean::toArchitecturallyEvidentType)
.findFirst()
.map(it -> new ObservedModuleType(modules, this, it))
.filter(ObservedModuleType::shouldBeTraced)
.filter(ObservedModuleType::shouldBeObserved)
.orElse(null);
}

View File

@@ -23,6 +23,7 @@ import java.util.stream.Stream;
import org.springframework.aop.TargetClassAware;
import org.springframework.aop.framework.Advised;
import org.springframework.context.annotation.Configuration;
import org.springframework.modulith.core.ApplicationModules;
import org.springframework.modulith.core.ArchitecturallyEvidentType;
import org.springframework.modulith.core.ArchitecturallyEvidentType.ReferenceMethod;
@@ -34,7 +35,7 @@ import org.springframework.util.ReflectionUtils;
*
* @author Oliver Drotbohm
*/
public class ObservedModuleType {
class ObservedModuleType {
private static Collection<Class<?>> IGNORED_TYPES = List.of(Advised.class, TargetClassAware.class);
@@ -62,16 +63,20 @@ public class ObservedModuleType {
}
/**
* Returns whether the type should be traced at all. Can be skipped for types not exposed by the module unless they
* Returns whether the type should be observed at all. Can be skipped for types not exposed by the module unless they
* listen to events of other modules.
*
* @return
*/
public boolean shouldBeTraced() {
public boolean shouldBeObserved() {
boolean isApiType = module.exposes(type.getType());
if (type.getType().isMetaAnnotatedWith(Configuration.class)) {
return false;
}
return type.isController() || listensToOtherModulesEvents() || isApiType;
return type.isController()
|| listensToOtherModulesEvents()
|| module.exposes(type.getType());
}
/**

View File

@@ -0,0 +1,24 @@
/*
* Copyright 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 example.sample;
import org.springframework.context.annotation.Configuration;
/**
* @author Oliver Drotbohm
*/
@Configuration
public class SampleConfiguration {}

View File

@@ -18,6 +18,7 @@ package org.springframework.modulith.observability;
import static org.assertj.core.api.Assertions.*;
import example.sample.SampleComponent;
import example.sample.SampleConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.aop.framework.Advised;
@@ -55,6 +56,15 @@ public class ObservedModuleTypeUnitTests {
@Test // GH-106
void considersExposedTypeAsToBeIntercepted() {
assertThat(observedType.shouldBeTraced()).isTrue();
assertThat(observedType.shouldBeObserved()).isTrue();
}
@Test // GH-332, GH-333
void doesNotObserveConfigurationClasses() {
var type = module.getArchitecturallyEvidentType(SampleConfiguration.class);
var observedType = new ObservedModuleType(modules, new DefaultObservedModule(module), type);
assertThat(observedType.shouldBeObserved()).isFalse();
}
}