15
15
*/
16
16
package org .springframework .modulith .observability ;
17
17
18
- import io .micrometer .tracing . BaggageInScope ;
19
- import io .micrometer .tracing . Tracer ;
20
- import io .micrometer .tracing . Tracer . SpanInScope ;
18
+ import io .micrometer .common . KeyValue ;
19
+ import io .micrometer .observation . Observation ;
20
+ import io .micrometer .observation . ObservationRegistry ;
21
21
22
22
import java .util .HashMap ;
23
23
import java .util .Map ;
24
+ import java .util .Objects ;
24
25
25
26
import org .aopalliance .intercept .MethodInterceptor ;
26
27
import org .aopalliance .intercept .MethodInvocation ;
27
28
import org .slf4j .Logger ;
28
29
import org .slf4j .LoggerFactory ;
30
+ import org .springframework .core .env .Environment ;
31
+ import org .springframework .lang .Nullable ;
32
+ import org .springframework .modulith .core .ApplicationModuleIdentifier ;
33
+ import org .springframework .modulith .observability .ModulithObservations .LowKeys ;
29
34
import org .springframework .util .Assert ;
30
35
31
36
class ModuleEntryInterceptor implements MethodInterceptor {
32
37
33
38
private static Logger LOGGER = LoggerFactory .getLogger (ModuleEntryInterceptor .class );
34
- private static Map <String , ModuleEntryInterceptor > CACHE = new HashMap <>();
35
- private static final String MODULE_KEY = ModuleTracingBeanPostProcessor .MODULE_BAGGAGE_KEY ;
39
+ private static Map <ApplicationModuleIdentifier , ModuleEntryInterceptor > CACHE = new HashMap <>();
40
+
41
+ private static final ModulithObservationConvention DEFAULT = new DefaultModulithObservationConvention ();
36
42
37
43
private final ObservedModule module ;
38
- private final Tracer tracer ;
44
+ private final ObservationRegistry observationRegistry ;
45
+ @ Nullable private final ModulithObservationConvention customModulithObservationConvention ;
46
+ private final Environment environment ;
47
+
48
+ /**
49
+ * Creates a new {@link ModuleEntryInterceptor} for the given {@link ObservedModule} and {@link ObservationRegistry}.
50
+ *
51
+ * @param module must not be {@literal null}.
52
+ * @param observationRegistry must not be {@literal null}.
53
+ * @param environment must not be {@literal null}.
54
+ */
55
+ private ModuleEntryInterceptor (ObservedModule module , ObservationRegistry observationRegistry ,
56
+ Environment environment ) {
57
+ this (module , observationRegistry , null , environment );
58
+ }
39
59
40
60
/**
41
- * Creates a new {@link ModuleEntryInterceptor} for the given {@link ObservedModule} and {@link Tracer}.
61
+ * Creates a new {@link ModuleEntryInterceptor} for the given {@link ObservedModule}, {@link ObservationRegistry} and
62
+ * {@link ModulithObservationConvention}.
42
63
*
43
64
* @param module must not be {@literal null}.
44
- * @param tracer must not be {@literal null}.
65
+ * @param observationRegistry must not be {@literal null}.
66
+ * @param custom must not be {@literal null}.
67
+ * @param environment must not be {@literal null}.
45
68
*/
46
- private ModuleEntryInterceptor (ObservedModule module , Tracer tracer ) {
69
+ private ModuleEntryInterceptor (ObservedModule module , ObservationRegistry observationRegistry ,
70
+ ModulithObservationConvention custom , Environment environment ) {
47
71
48
72
Assert .notNull (module , "ObservedModule must not be null!" );
49
- Assert .notNull (tracer , "Tracer must not be null!" );
73
+ Assert .notNull (observationRegistry , "ObservationRegistry must not be null!" );
50
74
51
75
this .module = module ;
52
- this .tracer = tracer ;
76
+ this .observationRegistry = observationRegistry ;
77
+ this .customModulithObservationConvention = custom ;
78
+ this .environment = environment ;
79
+ }
80
+
81
+ public static ModuleEntryInterceptor of (ObservedModule module , ObservationRegistry observationRegistry ,
82
+ Environment environment ) {
83
+ return of (module , observationRegistry , null , environment );
53
84
}
54
85
55
- public static ModuleEntryInterceptor of (ObservedModule module , Tracer tracer ) {
86
+ public static ModuleEntryInterceptor of (ObservedModule module , ObservationRegistry observationRegistry ,
87
+ ModulithObservationConvention custom , Environment environment ) {
56
88
57
- return CACHE .computeIfAbsent (module .getName (), __ -> {
58
- return new ModuleEntryInterceptor (module , tracer );
89
+ return CACHE .computeIfAbsent (module .getIdentifier (), __ -> {
90
+ return new ModuleEntryInterceptor (module , observationRegistry , custom , environment );
59
91
});
60
92
}
61
93
@@ -66,34 +98,38 @@ public static ModuleEntryInterceptor of(ObservedModule module, Tracer tracer) {
66
98
@ Override
67
99
public Object invoke (MethodInvocation invocation ) throws Throwable {
68
100
69
- var moduleName = module .getName ();
70
- var currentSpan = tracer .currentSpan ();
71
- var currentBaggage = tracer .getBaggage (MODULE_KEY );
72
- var currentModule = currentBaggage != null ? currentBaggage .get () : null ;
101
+ var moduleIdentifier = module .getIdentifier ();
102
+ var currentObservation = observationRegistry .getCurrentObservation ();
103
+ String currentModule = null ;
104
+
105
+ if (currentObservation != null ) {
106
+ KeyValue moduleKey = currentObservation .getContextView ().getLowCardinalityKeyValue (LowKeys .MODULE_KEY .asString ());
107
+ currentModule = moduleKey != null ? moduleKey .getValue () : null ;
108
+ }
73
109
74
- if (currentSpan != null && moduleName .equals (currentModule )) {
110
+ if (currentObservation != null && Objects .equals (moduleIdentifier .toString (), currentModule )) {
111
+ // Same module
75
112
return invocation .proceed ();
76
113
}
77
114
78
115
var invokedMethod = module .getInvokedMethod (invocation );
79
116
80
117
LOGGER .trace ("Entering {} via {}." , module .getDisplayName (), invokedMethod );
81
118
82
- var span = tracer . spanBuilder ()
83
- . name ( moduleName )
84
- . tag ( "module.method" , invokedMethod )
85
- . tag ( MODULE_KEY , moduleName )
86
- . start ();
87
-
88
- try ( SpanInScope ws = tracer . withSpan ( span ) ;
89
- BaggageInScope baggage = tracer . createBaggageInScope ( MODULE_KEY , moduleName ) ) {
90
-
91
- return invocation . proceed ( );
92
-
119
+ ModulithContext modulithContext = new ModulithContext ( module , invocation , environment );
120
+ var observation = Observation . createNotStarted ( customModulithObservationConvention , DEFAULT ,
121
+ () -> modulithContext , observationRegistry );
122
+ try ( Observation . Scope scope = observation . start (). openScope ()) {
123
+ Object proceed = invocation . proceed ();
124
+ observation . event ( ModulithObservations . Events . EVENT_PUBLICATION_SUCCESS );
125
+ return proceed ;
126
+ } catch ( Exception ex ) {
127
+ observation . error ( ex );
128
+ observation . event ( ModulithObservations . Events . EVENT_PUBLICATION_FAILURE );
129
+ throw ex ;
93
130
} finally {
94
-
95
131
LOGGER .trace ("Leaving {}" , module .getDisplayName ());
96
- span . end ();
132
+ observation . stop ();
97
133
}
98
134
}
99
135
}
0 commit comments