Skip to content

Commit f3d5e3b

Browse files
committed
GH-928 - Polishing.
License headers, formatting, null checks.
1 parent 738ad3f commit f3d5e3b

12 files changed

+319
-89
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1+
/*
2+
* Copyright 2024 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+
*/
116
package org.springframework.modulith.observability;
217

3-
import java.lang.reflect.Method;
4-
518
import io.micrometer.common.KeyValues;
619

720
import org.springframework.modulith.observability.ModulithObservations.HighKeys;
@@ -11,40 +24,62 @@
1124
* Default implementation of {@link ModulithObservationConvention}.
1225
*
1326
* @author Marcin Grzejszczak
27+
* @author Oliver Drotbohm
1428
* @since 1.4
1529
*/
16-
public class DefaultModulithObservationConvention implements ModulithObservationConvention {
30+
class DefaultModulithObservationConvention implements ModulithObservationConvention {
1731

32+
/*
33+
* (non-Javadoc)
34+
* @see io.micrometer.observation.ObservationConvention#getLowCardinalityKeyValues(io.micrometer.observation.Observation.Context)
35+
*/
1836
@Override
1937
public KeyValues getLowCardinalityKeyValues(ModulithContext context) {
20-
KeyValues keyValues = KeyValues.of(LowKeys.MODULE_KEY.withValue(context.getModule().getIdentifier().toString()));
21-
if (isEventListener(context)) {
22-
return keyValues.and(LowKeys.INVOCATION_TYPE.withValue("event-listener"));
23-
}
24-
return keyValues;
25-
}
2638

27-
private boolean isEventListener(ModulithContext context) {
28-
try {
29-
return context.getModule().isEventListenerInvocation(context.getInvocation());
30-
} catch (Exception e) {
31-
return false;
32-
}
39+
var keyValues = KeyValues.of(LowKeys.MODULE_KEY.withValue(context.getModule().getIdentifier().toString()));
40+
41+
return isEventListener(context)
42+
? keyValues.and(LowKeys.INVOCATION_TYPE.withValue("event-listener"))
43+
: keyValues;
3344
}
3445

46+
/*
47+
*
48+
* (non-Javadoc)
49+
* @see io.micrometer.observation.ObservationConvention#getHighCardinalityKeyValues(io.micrometer.observation.Observation.Context)
50+
*/
3551
@Override
3652
public KeyValues getHighCardinalityKeyValues(ModulithContext context) {
37-
Method method = context.getInvocation().getMethod();
53+
54+
var method = context.getInvocation().getMethod();
55+
3856
return KeyValues.of(HighKeys.MODULE_METHOD.withValue(method.getName()));
3957
}
4058

59+
/*
60+
* (non-Javadoc)
61+
* @see io.micrometer.observation.ObservationConvention#getName()
62+
*/
4163
@Override
4264
public String getName() {
4365
return "module.requests";
4466
}
4567

68+
/*
69+
* (non-Javadoc)
70+
* @see io.micrometer.observation.ObservationConvention#getContextualName(io.micrometer.observation.Observation.Context)
71+
*/
4672
@Override
4773
public String getContextualName(ModulithContext context) {
4874
return "[" + context.getApplicationName() + "] " + context.getModule().getDisplayName();
4975
}
76+
77+
private boolean isEventListener(ModulithContext context) {
78+
79+
try {
80+
return context.getModule().isEventListenerInvocation(context.getInvocation());
81+
} catch (Exception e) {
82+
return false;
83+
}
84+
}
5085
}

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

-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.springframework.modulith.core.ApplicationModule;
2626
import org.springframework.modulith.core.ApplicationModuleIdentifier;
2727
import org.springframework.modulith.core.ApplicationModules;
28-
import org.springframework.modulith.core.ArchitecturallyEvidentType;
2928
import org.springframework.modulith.core.ArchitecturallyEvidentType.ReferenceMethod;
3029
import org.springframework.modulith.core.FormattableType;
3130
import org.springframework.modulith.core.SpringBean;
@@ -163,7 +162,6 @@ private Method findModuleLocalMethod(MethodInvocation invocation) {
163162
var targetClass = advised.getTargetClass();
164163

165164
if (module.contains(targetClass)) {
166-
167165
return AopUtils.getMostSpecificMethod(method, targetClass);
168166
}
169167

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

+22-7
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616
package org.springframework.modulith.observability;
1717

18-
import io.micrometer.common.KeyValue;
1918
import io.micrometer.observation.Observation;
19+
import io.micrometer.observation.Observation.Scope;
2020
import io.micrometer.observation.ObservationRegistry;
2121

2222
import java.util.HashMap;
@@ -33,16 +33,22 @@
3333
import org.springframework.modulith.observability.ModulithObservations.LowKeys;
3434
import org.springframework.util.Assert;
3535

36+
/**
37+
* {@link MethodInterceptor} to create {@link Observation}s.
38+
*
39+
* @author Marcin Grzejszczak
40+
* @author Oliver Drotbohm
41+
*/
3642
class ModuleEntryInterceptor implements MethodInterceptor {
3743

38-
private static Logger LOGGER = LoggerFactory.getLogger(ModuleEntryInterceptor.class);
44+
private static final Logger LOGGER = LoggerFactory.getLogger(ModuleEntryInterceptor.class);
3945
private static Map<ApplicationModuleIdentifier, ModuleEntryInterceptor> CACHE = new HashMap<>();
4046

4147
private static final ModulithObservationConvention DEFAULT = new DefaultModulithObservationConvention();
4248

4349
private final ObservedModule module;
4450
private final ObservationRegistry observationRegistry;
45-
@Nullable private final ModulithObservationConvention customModulithObservationConvention;
51+
private final @Nullable ModulithObservationConvention customModulithObservationConvention;
4652
private final Environment environment;
4753

4854
/**
@@ -103,7 +109,8 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
103109
String currentModule = null;
104110

105111
if (currentObservation != null) {
106-
KeyValue moduleKey = currentObservation.getContextView().getLowCardinalityKeyValue(LowKeys.MODULE_KEY.asString());
112+
113+
var moduleKey = currentObservation.getContextView().getLowCardinalityKeyValue(LowKeys.MODULE_KEY.asString());
107114
currentModule = moduleKey != null ? moduleKey.getValue() : null;
108115
}
109116

@@ -116,18 +123,26 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
116123

117124
LOGGER.trace("Entering {} via {}.", module.getDisplayName(), invokedMethod);
118125

119-
ModulithContext modulithContext = new ModulithContext(module, invocation, environment);
126+
var modulithContext = new ModulithContext(module, invocation, environment);
120127
var observation = Observation.createNotStarted(customModulithObservationConvention, DEFAULT,
121128
() -> modulithContext, observationRegistry);
122-
try (Observation.Scope scope = observation.start().openScope()) {
123-
Object proceed = invocation.proceed();
129+
130+
try (Scope scope = observation.start().openScope()) {
131+
132+
var proceed = invocation.proceed();
124133
observation.event(ModulithObservations.Events.EVENT_PUBLICATION_SUCCESS);
134+
125135
return proceed;
136+
126137
} catch (Exception ex) {
138+
127139
observation.error(ex);
128140
observation.event(ModulithObservations.Events.EVENT_PUBLICATION_FAILURE);
141+
129142
throw ex;
143+
130144
} finally {
145+
131146
LOGGER.trace("Leaving {}", module.getDisplayName());
132147
observation.stop();
133148
}

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ public class ModuleEventListener implements ApplicationListener<ApplicationEvent
3939
private final Supplier<MeterRegistry> meterRegistry;
4040

4141
/**
42-
* Creates a new {@link ModuleEventListener} for the given {@link ApplicationModulesRuntime} and {@link ObservationRegistry} and {@link MeterRegistry}.
42+
* Creates a new {@link ModuleEventListener} for the given {@link ApplicationModulesRuntime} and
43+
* {@link ObservationRegistry} and {@link MeterRegistry}.
4344
*
4445
* @param runtime must not be {@literal null}.
4546
* @param observationRegistrySupplier must not be {@literal null}.
47+
* @param meterRegistrySupplier must not be {@literal null}.
4648
*/
47-
public ModuleEventListener(ApplicationModulesRuntime runtime, Supplier<ObservationRegistry> observationRegistrySupplier,
48-
Supplier<MeterRegistry> meterRegistrySupplier) {
49+
public ModuleEventListener(ApplicationModulesRuntime runtime,
50+
Supplier<ObservationRegistry> observationRegistrySupplier, Supplier<MeterRegistry> meterRegistrySupplier) {
4951

5052
Assert.notNull(runtime, "ApplicationModulesRuntime must not be null!");
5153
Assert.notNull(observationRegistrySupplier, "ObservationRegistry must not be null!");
@@ -82,8 +84,10 @@ public void onApplicationEvent(ApplicationEvent event) {
8284
return;
8385
}
8486

85-
MeterRegistry registry = meterRegistry.get();
87+
var registry = meterRegistry.get();
88+
8689
if (registry != null) {
90+
8791
Counter.builder(ModulithMetrics.EVENTS.getName()) //
8892
.tags(ModulithMetrics.LowKeys.EVENT_TYPE.name().toLowerCase(), event.getClass().getSimpleName()) //
8993
.tags(ModulithMetrics.LowKeys.MODULE_NAME.name().toLowerCase(), moduleByType.getDisplayName()) //
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2024 the original author or authors.
2+
* Copyright 2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,30 +16,46 @@
1616
package org.springframework.modulith.observability;
1717

1818
import io.micrometer.observation.Observation;
19+
import io.micrometer.observation.Observation.ContextView;
1920
import io.micrometer.observation.ObservationFilter;
2021

2122
/**
22-
* Ensures that {@link ModulithObservations.LowKeys#MODULE_KEY} gets propagated from parent
23-
* to child.
24-
*
23+
* Ensures that {@link ModulithObservations.LowKeys#MODULE_KEY} gets propagated from parent to child.
24+
*
2525
* @author Marcin Grzejszczak
26+
* @author Oliver Drotbohm
2627
* @since 1.4
2728
*/
2829
public class ModulePassingObservationFilter implements ObservationFilter {
2930

30-
@Override
31+
/*
32+
* (non-Javadoc)
33+
* @see io.micrometer.observation.ObservationFilter#map(io.micrometer.observation.Observation.Context)
34+
*/
35+
@Override
3136
public Observation.Context map(Observation.Context context) {
37+
3238
if (isModuleKeyValueAbsentInCurrent(context) && isModuleKeyValuePresentInParent(context)) {
33-
return context.addLowCardinalityKeyValue(ModulithObservations.LowKeys.MODULE_KEY.withValue(context.getParentObservation().getContextView().getLowCardinalityKeyValue(ModulithObservations.LowKeys.MODULE_KEY.asString()).getValue()));
39+
40+
var moduleKey = ModulithObservations.LowKeys.MODULE_KEY;
41+
42+
return context.addLowCardinalityKeyValue(moduleKey.withValue(context.getParentObservation().getContextView()
43+
.getLowCardinalityKeyValue(moduleKey.asString()).getValue()));
3444
}
45+
3546
return context;
3647
}
3748

38-
private static boolean isModuleKeyValueAbsentInCurrent(Observation.ContextView context) {
49+
private static boolean isModuleKeyValueAbsentInCurrent(ContextView context) {
3950
return context.getLowCardinalityKeyValue(ModulithObservations.LowKeys.MODULE_KEY.asString()) == null;
4051
}
4152

42-
private static boolean isModuleKeyValuePresentInParent(Observation.ContextView context) {
43-
return context.getParentObservation() != null && context.getParentObservation().getContextView().getLowCardinalityKeyValue(ModulithObservations.LowKeys.MODULE_KEY.asString()) != null;
53+
private static boolean isModuleKeyValuePresentInParent(ContextView context) {
54+
55+
var parentObservation = context.getParentObservation();
56+
57+
return parentObservation != null
58+
&& parentObservation.getContextView()
59+
.getLowCardinalityKeyValue(ModulithObservations.LowKeys.MODULE_KEY.asString()) != null;
4460
}
45-
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,59 @@
1+
/*
2+
* Copyright 2024 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+
*/
116
package org.springframework.modulith.observability;
217

3-
import io.micrometer.observation.Observation;
4-
import org.aopalliance.intercept.MethodInvocation;
18+
import io.micrometer.observation.Observation.Context;
519

20+
import org.aopalliance.intercept.MethodInvocation;
621
import org.springframework.core.env.Environment;
22+
import org.springframework.util.Assert;
723

824
/**
925
* A {@link Observation.Context} for Modulithic applications.
1026
*
1127
* @author Marcin Grzejsczak
28+
* @author Oliver Drotbohm
1229
* @since 1.4
1330
*/
14-
public class ModulithContext extends Observation.Context {
31+
public class ModulithContext extends Context {
1532

16-
private final ObservedModule module;
33+
private final ObservedModule module;
34+
private final MethodInvocation invocation;
35+
private final String applicationName;
1736

18-
private final MethodInvocation invocation;
37+
public ModulithContext(ObservedModule module, MethodInvocation invocation, Environment environment) {
1938

20-
private final String applicationName;
39+
Assert.notNull(module, "ObservedModule must not be null!");
40+
Assert.notNull(invocation, "MethodInvocation must not be null!");
41+
Assert.notNull(environment, "Environment must not be null!");
2142

22-
public ModulithContext(ObservedModule module, MethodInvocation invocation, Environment environment) {
23-
this.module = module;
24-
this.invocation = invocation;
25-
this.applicationName = environment.getProperty("spring.application.name");
26-
}
43+
this.module = module;
44+
this.invocation = invocation;
45+
this.applicationName = environment.getProperty("spring.application.name");
46+
}
2747

28-
public ObservedModule getModule() {
29-
return module;
30-
}
48+
public ObservedModule getModule() {
49+
return module;
50+
}
3151

32-
public MethodInvocation getInvocation() {
33-
return invocation;
34-
}
52+
public MethodInvocation getInvocation() {
53+
return invocation;
54+
}
3555

36-
public String getApplicationName() {
37-
return applicationName;
38-
}
56+
public String getApplicationName() {
57+
return applicationName;
58+
}
3959
}

0 commit comments

Comments
 (0)