Class MetricsListener

java.lang.Object
cloud.opencode.base.rules.metric.MetricsListener
All Implemented Interfaces:
RuleListener

public final class MetricsListener extends Object implements 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 Details

    • MetricsListener

      public MetricsListener(RuleMetrics metrics)
      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

      public RuleMetrics getMetrics()
      Returns the underlying RuleMetrics instance 返回底层的RuleMetrics实例
      Returns:
      the metrics collector | 指标收集器
    • beforeEvaluate

      public void beforeEvaluate(Rule rule, RuleContext context)
      Records the start time before rule evaluation 在规则评估前记录开始时间
      Specified by:
      beforeEvaluate in interface RuleListener
      Parameters:
      rule - the rule being evaluated | 正在评估的规则
      context - the rule context | 规则上下文
    • afterEvaluate

      public void afterEvaluate(Rule rule, RuleContext context, boolean satisfied)
      Computes duration and records the evaluation metric 计算持续时间并记录评估指标
      Specified by:
      afterEvaluate in interface RuleListener
      Parameters:
      rule - the rule that was evaluated | 已评估的规则
      context - the rule context | 规则上下文
      satisfied - whether the condition was satisfied | 条件是否满足
    • onFailure

      public void onFailure(Rule rule, RuleContext context, Exception exception)
      Records a rule failure 记录规则失败
      Specified by:
      onFailure in interface RuleListener
      Parameters:
      rule - the rule that failed | 失败的规则
      context - the rule context | 规则上下文
      exception - the exception thrown | 抛出的异常
    • onFinish

      public void onFinish(RuleContext context, int firedCount, long elapsedMillis)
      Cleans up ThreadLocal state when execution finishes 执行完成时清理ThreadLocal状态
      Specified by:
      onFinish in interface RuleListener
      Parameters:
      context - the rule context | 规则上下文
      firedCount - the number of fired rules | 触发的规则数
      elapsedMillis - the elapsed time in milliseconds | 耗时(毫秒)