1515 */
1616package org .springframework .modulith .observability ;
1717
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 ;
2121
2222import java .util .HashMap ;
2323import java .util .Map ;
24+ import java .util .Objects ;
2425
2526import org .aopalliance .intercept .MethodInterceptor ;
2627import org .aopalliance .intercept .MethodInvocation ;
2728import org .slf4j .Logger ;
2829import 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 ;
2934import org .springframework .util .Assert ;
3035
3136class ModuleEntryInterceptor implements MethodInterceptor {
3237
3338 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 ();
3642
3743 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+ }
3959
4060 /**
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}.
4263 *
4364 * @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}.
4568 */
46- private ModuleEntryInterceptor (ObservedModule module , Tracer tracer ) {
69+ private ModuleEntryInterceptor (ObservedModule module , ObservationRegistry observationRegistry ,
70+ ModulithObservationConvention custom , Environment environment ) {
4771
4872 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!" );
5074
5175 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 );
5384 }
5485
55- public static ModuleEntryInterceptor of (ObservedModule module , Tracer tracer ) {
86+ public static ModuleEntryInterceptor of (ObservedModule module , ObservationRegistry observationRegistry ,
87+ ModulithObservationConvention custom , Environment environment ) {
5688
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 );
5991 });
6092 }
6193
@@ -66,34 +98,38 @@ public static ModuleEntryInterceptor of(ObservedModule module, Tracer tracer) {
6698 @ Override
6799 public Object invoke (MethodInvocation invocation ) throws Throwable {
68100
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+ }
73109
74- if (currentSpan != null && moduleName .equals (currentModule )) {
110+ if (currentObservation != null && Objects .equals (moduleIdentifier .toString (), currentModule )) {
111+ // Same module
75112 return invocation .proceed ();
76113 }
77114
78115 var invokedMethod = module .getInvokedMethod (invocation );
79116
80117 LOGGER .trace ("Entering {} via {}." , module .getDisplayName (), invokedMethod );
81118
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 ;
93130 } finally {
94-
95131 LOGGER .trace ("Leaving {}" , module .getDisplayName ());
96- span . end ();
132+ observation . stop ();
97133 }
98134 }
99135}
0 commit comments