Class MetricsListener
java.lang.Object
cloud.opencode.base.rules.metric.MetricsListener
- All Implemented Interfaces:
RuleListener
Metrics Listener - RuleListener That Collects Execution Metrics
指标监听器 - 收集执行指标的规则监听器
Implements RuleListener to automatically collect per-rule timing,
fire, and failure metrics during rule engine execution.
实现RuleListener以在规则引擎执行期间自动收集每条规则的时间、触发和失败指标。
Thread Safety | 线程安全: Uses ThreadLocal for start times and
delegates to thread-safe RuleMetrics for accumulation.
- Since:
- JDK 25, opencode-base-rules V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a MetricsListener with an internal RuleMetrics instance 使用内部RuleMetrics实例创建MetricsListenerMetricsListener(RuleMetrics metrics) Creates a MetricsListener with the given RuleMetrics instance 使用给定的RuleMetrics实例创建MetricsListener -
Method Summary
Modifier and TypeMethodDescriptionvoidafterEvaluate(Rule rule, RuleContext context, boolean satisfied) Computes duration and records the evaluation metric 计算持续时间并记录评估指标voidbeforeEvaluate(Rule rule, RuleContext context) Records the start time before rule evaluation 在规则评估前记录开始时间Returns the underlying RuleMetrics instance 返回底层的RuleMetrics实例voidonFailure(Rule rule, RuleContext context, Exception exception) Records a rule failure 记录规则失败voidonFinish(RuleContext context, int firedCount, long elapsedMillis) Cleans up ThreadLocal state when execution finishes 执行完成时清理ThreadLocal状态Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface RuleListener
afterExecute, beforeExecute, onStart
-
Constructor Details
-
MetricsListener
Creates a MetricsListener with the given RuleMetrics instance 使用给定的RuleMetrics实例创建MetricsListener- Parameters:
metrics- the metrics collector | 指标收集器
-
MetricsListener
public MetricsListener()Creates a MetricsListener with an internal RuleMetrics instance 使用内部RuleMetrics实例创建MetricsListener
-
-
Method Details
-
getMetrics
Returns the underlying RuleMetrics instance 返回底层的RuleMetrics实例- Returns:
- the metrics collector | 指标收集器
-
beforeEvaluate
Records the start time before rule evaluation 在规则评估前记录开始时间- Specified by:
beforeEvaluatein interfaceRuleListener- Parameters:
rule- the rule being evaluated | 正在评估的规则context- the rule context | 规则上下文
-
afterEvaluate
Computes duration and records the evaluation metric 计算持续时间并记录评估指标- Specified by:
afterEvaluatein interfaceRuleListener- Parameters:
rule- the rule that was evaluated | 已评估的规则context- the rule context | 规则上下文satisfied- whether the condition was satisfied | 条件是否满足
-
onFailure
Records a rule failure 记录规则失败- Specified by:
onFailurein interfaceRuleListener- Parameters:
rule- the rule that failed | 失败的规则context- the rule context | 规则上下文exception- the exception thrown | 抛出的异常
-
onFinish
Cleans up ThreadLocal state when execution finishes 执行完成时清理ThreadLocal状态- Specified by:
onFinishin interfaceRuleListener- Parameters:
context- the rule context | 规则上下文firedCount- the number of fired rules | 触发的规则数elapsedMillis- the elapsed time in milliseconds | 耗时(毫秒)
-