3535import org .apache .shenyu .plugin .base .cache .BaseDataCache ;
3636import org .apache .shenyu .plugin .base .cache .MatchDataCache ;
3737import org .apache .shenyu .plugin .base .condition .strategy .MatchStrategyFactory ;
38+ import org .apache .shenyu .plugin .base .match .SelectorRuleMatchObservation ;
39+ import org .apache .shenyu .plugin .base .match .SelectorRuleMatchObservation .CacheResolution ;
40+ import org .apache .shenyu .plugin .base .match .SelectorRuleMatchObservation .Decision ;
41+ import org .apache .shenyu .plugin .base .match .SelectorRuleMatchObserver ;
42+ import org .apache .shenyu .plugin .base .match .SelectorRuleMatchObserverHolder ;
3843import org .slf4j .Logger ;
3944import org .slf4j .LoggerFactory ;
4045import org .springframework .web .server .ServerWebExchange ;
@@ -87,54 +92,86 @@ public Mono<Void> execute(final ServerWebExchange exchange, final ShenyuPluginCh
8792 PluginData pluginData = BaseDataCache .getInstance ().obtainPluginData (pluginName );
8893 // early exit
8994 if (Objects .isNull (pluginData ) || !pluginData .getEnabled ()) {
95+ reportDecision (Objects .isNull (pluginData ) ? Decision .NO_PLUGIN_DATA : Decision .PLUGIN_DISABLED ,
96+ null , null , false , CacheResolution .NOT_CONSULTED , CacheResolution .NOT_CONSULTED );
9097 return chain .execute (exchange );
9198 }
9299 final String path = getRawPath (exchange );
93100 List <SelectorData > selectors = BaseDataCache .getInstance ().obtainSelectorData (pluginName );
94101 if (CollectionUtils .isEmpty (selectors )) {
102+ reportDecision (Decision .NO_SELECTOR , null , null , false ,
103+ CacheResolution .NOT_CONSULTED , CacheResolution .NOT_CONSULTED );
95104 return handleSelectorIfNull (pluginName , exchange , chain );
96105 }
97106 SelectorData selectorData = obtainSelectorDataCacheIfEnabled (path );
107+ CacheResolution selectorCache = Objects .nonNull (selectorData ) ? CacheResolution .POSITIVE_HIT
108+ : cacheResolution (selectorMatchConfig .getCache ().getEnabled ());
98109 // handle Selector
99110 if (Objects .nonNull (selectorData ) && StringUtils .isBlank (selectorData .getId ())) {
111+ reportDecision (Decision .NO_SELECTOR , null , null , false ,
112+ CacheResolution .NEGATIVE_HIT , CacheResolution .NOT_CONSULTED );
100113 return handleSelectorIfNull (pluginName , exchange , chain );
101114 }
102115 if (Objects .isNull (selectorData )) {
103116 selectorData = defaultMatchSelector (exchange , selectors , path );
104117 if (Objects .isNull (selectorData )) {
118+ reportDecision (Decision .SELECTOR_UNMATCHED , null , null , false ,
119+ selectorCache , CacheResolution .NOT_CONSULTED );
105120 return handleSelectorIfNull (pluginName , exchange , chain );
106121 }
107122 }
108123 printLog (selectorData , pluginName );
109124 if (!selectorData .getContinued ()) {
110125 // if continued, not match rules
126+ reportDecision (Decision .SELECTOR_NOT_CONTINUED , selectorData , null , true ,
127+ selectorCache , CacheResolution .NOT_CONSULTED );
111128 return doExecute (exchange , chain , selectorData , defaultRuleData (selectorData ));
112129 }
130+ return matchRuleAndExecute (exchange , chain , pluginName , selectors , path , selectorData , selectorCache );
131+ }
132+
133+ private Mono <Void > matchRuleAndExecute (final ServerWebExchange exchange , final ShenyuPluginChain chain ,
134+ final String pluginName , final List <SelectorData > selectors ,
135+ final String path , final SelectorData selectorData ,
136+ final CacheResolution selectorCache ) {
113137 List <RuleData > rules = BaseDataCache .getInstance ().obtainRuleData (selectorData .getId ());
114138 if (CollectionUtils .isEmpty (rules )) {
139+ reportDecision (Decision .NO_RULE , selectorData , null , false ,
140+ selectorCache , CacheResolution .NOT_CONSULTED );
115141 return handleRuleIfNull (pluginName , exchange , chain );
116142 }
117143 if (selectorData .getType () == SelectorTypeEnum .FULL_FLOW .getCode ()) {
118144 RuleData rule = lastEnabledRule (rules );
119145 if (Objects .isNull (rule )) {
146+ reportDecision (Decision .NO_RULE , selectorData , null , false ,
147+ selectorCache , CacheResolution .NOT_CONSULTED );
120148 return handleRuleIfNull (pluginName , exchange , chain );
121149 }
122150 printLog (rule , pluginName );
151+ reportDecision (Decision .FULL_FLOW_RULE , selectorData , rule , false ,
152+ selectorCache , CacheResolution .NOT_CONSULTED );
123153 return doExecute (exchange , chain , selectorData , rule );
124154 }
125155 // consult the bounded per-path match cache first when it is enabled.
126156 // on a cache miss, match the current rule list with the default strategy.
127157 RuleData ruleData = obtainRuleDataCacheIfEnabled (path );
158+ CacheResolution ruleCache = Objects .nonNull (ruleData ) ? CacheResolution .POSITIVE_HIT
159+ : cacheResolution (ruleMatchConfig .getCache ().getEnabled ());
128160 if (Objects .nonNull (ruleData ) && Objects .isNull (ruleData .getId ())) {
161+ reportDecision (Decision .NO_RULE , selectorData , null , false ,
162+ selectorCache , CacheResolution .NEGATIVE_HIT );
129163 return handleRuleIfNull (pluginName , exchange , chain );
130164 }
131165 if (Objects .isNull (ruleData )) {
132166 ruleData = defaultMatchRule (exchange , selectors , rules , path );
133167 if (Objects .isNull (ruleData )) {
168+ reportDecision (Decision .RULE_UNMATCHED , selectorData , null , false ,
169+ selectorCache , ruleCache );
134170 return handleRuleIfNull (pluginName , exchange , chain );
135171 }
136172 }
137173 printLog (ruleData , pluginName );
174+ reportDecision (Decision .SELECTED , selectorData , ruleData , false , selectorCache , ruleCache );
138175 return doExecute (exchange , chain , selectorData , ruleData );
139176 }
140177
@@ -147,6 +184,41 @@ private static RuleData lastEnabledRule(final List<RuleData> rules) {
147184 }
148185 return null ;
149186 }
187+
188+ private static CacheResolution cacheResolution (final boolean cacheEnabled ) {
189+ return cacheEnabled ? CacheResolution .EVALUATED : CacheResolution .NOT_CONSULTED ;
190+ }
191+
192+ /**
193+ * Report one matching decision to the registered observer, if any.
194+ *
195+ * <p>The summary is built only when an observer is registered, so the un-instrumented
196+ * path performs a single null read and allocates nothing. The report never re-runs a
197+ * predicate: it only names the branch just taken and the ids already in hand.</p>
198+ *
199+ * @param decision the decision reason
200+ * @param selector the selected selector, or null
201+ * @param rule the selected rule, or null
202+ * @param defaultRule whether the rule is the synthetic default rule
203+ * @param selectorCache how the selector stage resolved its cache
204+ * @param ruleCache how the rule stage resolved its cache
205+ */
206+ private void reportDecision (final Decision decision , final SelectorData selector , final RuleData rule ,
207+ final boolean defaultRule , final CacheResolution selectorCache ,
208+ final CacheResolution ruleCache ) {
209+ SelectorRuleMatchObserver observer = SelectorRuleMatchObserverHolder .get ();
210+ if (Objects .isNull (observer )) {
211+ return ;
212+ }
213+ try {
214+ final String ruleId = defaultRule ? Constants .DEFAULT_RULE : (Objects .isNull (rule ) ? null : rule .getId ());
215+ observer .onMatchDecision (SelectorRuleMatchObservation .of (named (), decision ,
216+ Objects .isNull (selector ) ? null : selector .getId (), ruleId ,
217+ defaultRule , selectorCache , ruleCache ));
218+ } catch (RuntimeException ignored ) {
219+ // a broken observer must never influence the request outcome
220+ }
221+ }
150222
151223 protected String getRawPath (final ServerWebExchange exchange ) {
152224 return RequestUrlUtils .getRewrittenRawPath (exchange );
0 commit comments