Skip to content

Commit 752b5d7

Browse files
committed
GH-332 - 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.
1 parent 1482ba0 commit 752b5d7

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

spring-modulith-observability/src/main/java/org/springframework/modulith/observability/DefaultObservedModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public ObservedModuleType getObservedModuleType(Class<?> type, ApplicationModule
140140
.map(SpringBean::toArchitecturallyEvidentType)
141141
.findFirst()
142142
.map(it -> new ObservedModuleType(modules, this, it))
143-
.filter(ObservedModuleType::shouldBeTraced)
143+
.filter(ObservedModuleType::shouldBeObserved)
144144
.orElse(null);
145145
}
146146

spring-modulith-observability/src/main/java/org/springframework/modulith/observability/ObservedModuleType.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.springframework.aop.TargetClassAware;
2525
import org.springframework.aop.framework.Advised;
26+
import org.springframework.context.annotation.Configuration;
2627
import org.springframework.modulith.core.ApplicationModules;
2728
import org.springframework.modulith.core.ArchitecturallyEvidentType;
2829
import org.springframework.modulith.core.ArchitecturallyEvidentType.ReferenceMethod;
@@ -34,7 +35,7 @@
3435
*
3536
* @author Oliver Drotbohm
3637
*/
37-
public class ObservedModuleType {
38+
class ObservedModuleType {
3839

3940
private static Collection<Class<?>> IGNORED_TYPES = List.of(Advised.class, TargetClassAware.class);
4041

@@ -62,16 +63,20 @@ public class ObservedModuleType {
6263
}
6364

6465
/**
65-
* Returns whether the type should be traced at all. Can be skipped for types not exposed by the module unless they
66+
* Returns whether the type should be observed at all. Can be skipped for types not exposed by the module unless they
6667
* listen to events of other modules.
6768
*
6869
* @return
6970
*/
70-
public boolean shouldBeTraced() {
71+
public boolean shouldBeObserved() {
7172

72-
boolean isApiType = module.exposes(type.getType());
73+
if (type.getType().isMetaAnnotatedWith(Configuration.class)) {
74+
return false;
75+
}
7376

74-
return type.isController() || listensToOtherModulesEvents() || isApiType;
77+
return type.isController()
78+
|| listensToOtherModulesEvents()
79+
|| module.exposes(type.getType());
7580
}
7681

7782
/**
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 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+
package example.sample;
17+
18+
import org.springframework.context.annotation.Configuration;
19+
20+
/**
21+
* @author Oliver Drotbohm
22+
*/
23+
@Configuration
24+
public class SampleConfiguration {}

spring-modulith-observability/src/test/java/org/springframework/modulith/observability/ObservedModuleTypeUnitTests.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.assertj.core.api.Assertions.*;
1919

2020
import example.sample.SampleComponent;
21+
import example.sample.SampleConfiguration;
2122

2223
import org.junit.jupiter.api.Test;
2324
import org.springframework.aop.framework.Advised;
@@ -55,6 +56,15 @@ void onlyExposesUserMethodsAsToBeIntercepted() {
5556

5657
@Test // GH-106
5758
void considersExposedTypeAsToBeIntercepted() {
58-
assertThat(observedType.shouldBeTraced()).isTrue();
59+
assertThat(observedType.shouldBeObserved()).isTrue();
60+
}
61+
62+
@Test // GH-332
63+
void doesNotObserveConfigurationClasses() {
64+
65+
var type = module.getArchitecturallyEvidentType(SampleConfiguration.class);
66+
var observedType = new ObservedModuleType(modules, new DefaultObservedModule(module), type);
67+
68+
assertThat(observedType.shouldBeObserved()).isFalse();
5969
}
6070
}

0 commit comments

Comments
 (0)