Class FlowMetricsFactory

java.lang.Object
cloud.opencode.base.flow.observability.FlowMetricsFactory

public final class FlowMetricsFactory extends Object
Factory for FlowMetrics that auto-detects whether the optional opencode-base-observability module is available on the runtime classpath. Returns BaseObservabilityFlowMetrics when present, NoOpFlowMetrics otherwise. FlowMetrics 工厂:自动探测可选模块 opencode-base-observability 是否在运行时类路径,命中返回 BaseObservabilityFlowMetrics,否则返回 NoOpFlowMetrics。

Detection uses Class.forName(String) so the engine module compiles and runs even when the observability module is excluded.

探测使用 Class.forName(String),因此即便排除 observability 依赖, flow 模块仍可编译运行。

Sample wiring:

  FlowMetrics metrics = FlowMetricsFactory.create();
  DefaultFlowInstanceService inst = new DefaultFlowInstanceService(
          repo, resolver, expr, metrics);
  DefaultTaskService task = new DefaultTaskService(
          repo, resolver, expr, metrics);
Since:
JDK 25, opencode-base-flow V1.0.5
Author:
Leon Soo
  • Method Details

    • create

      public static FlowMetrics create()
      Returns a FlowMetrics bound to a freshly created MetricRegistry when opencode-base-observability is on the classpath, or a NoOpFlowMetrics otherwise. 当类路径有 opencode-base-observability 时返回绑定新建 MetricRegistry 的 FlowMetrics,否则返回 NoOpFlowMetrics。

      R1 S-6 hardening: narrow the catch from Throwable to the specific signals that "observability module absent" can produce (ClassNotFoundException / NoClassDefFoundError); log (rather than swallow) a SecurityException; use MethodHandles instead of reflective Method.invoke so a signature mismatch in MetricRegistry.create() fails fast at lookup time rather than at the next invocation.

      R1 S-6 加固:把 Throwable 缩窄为 ClassNotFoundException / NoClassDefFoundError;SecurityException 改 WARN 后再退化;用 MethodHandles 替换 Method.invoke,签名错误 在 lookup 阶段直接 fail-fast。

      Returns:
      a new FlowMetrics instance — 新的 FlowMetrics 实例
      Since:
      JDK 25, opencode-base-flow V1.0.5
    • fromRegistry

      public static FlowMetrics fromRegistry(Object registry)
      Returns a FlowMetrics bound to the supplied registry. The registry parameter is typed as Object so this method can be called from hosts that have not added the observability module to their compile classpath (e.g. via reflection); a runtime ClassCastException is raised when the type is wrong. 返回绑定到给定 registry 的 FlowMetrics。registry 用 Object 类型以允许未把 observability 模块加入编译期类路径的宿主 通过反射调用;类型不匹配时运行期抛 ClassCastException。
      Parameters:
      registry - an instance of MetricRegistry — MetricRegistry 实例
      Returns:
      a new FlowMetrics instance — 新的 FlowMetrics 实例
      Throws:
      ClassCastException - when registry is not a MetricRegistry — 当 registry 不是 MetricRegistry 时
      Since:
      JDK 25, opencode-base-flow V1.0.5