Class MetricRegistry

java.lang.Object
cloud.opencode.base.observability.metric.MetricRegistry

public final class MetricRegistry extends Object
MetricRegistry - Central registry for creating and managing metrics MetricRegistry - 创建和管理指标的中央注册表

Provides factory methods for counters, gauges, timers, and histograms. All registrations are idempotent — registering the same MetricId returns the existing metric. Registering a different type for an existing MetricId throws an exception.

提供计数器、仪表盘、计时器和直方图的工厂方法。 所有注册都是幂等的 — 注册相同的 MetricId 返回已有指标。 为已有 MetricId 注册不同类型将抛出异常。

Thread safety | 线程安全: All operations are thread-safe via ConcurrentHashMap.

所有操作通过 ConcurrentHashMap 实现线程安全。

Since:
JDK 25, opencode-base-observability V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • create

      public static MetricRegistry create()
      Creates a registry with the default max metrics limit (10,000). 使用默认最大指标限制(10,000)创建注册表。
      Returns:
      a new MetricRegistry | 新的 MetricRegistry
    • create

      public static MetricRegistry create(int maxMetrics)
      Creates a registry with the specified max metrics limit. 使用指定的最大指标限制创建注册表。
      Parameters:
      maxMetrics - the maximum number of metrics | 最大指标数量
      Returns:
      a new MetricRegistry | 新的 MetricRegistry
      Throws:
      ObservabilityException - if maxMetrics is not positive | 如果 maxMetrics 不为正
    • counter

      public Counter counter(String name, Tag... tags)
      Registers or returns an existing counter. 注册或返回已有的计数器。
      Parameters:
      name - the metric name | 指标名称
      tags - the optional tags | 可选标签
      Returns:
      the Counter instance | 计数器实例
      Throws:
      ObservabilityException - if a different metric type is already registered, or limit exceeded 如果已注册了不同的指标类型,或超出限制
    • gauge

      public Gauge gauge(String name, Supplier<Double> supplier, Tag... tags)
      Registers or returns an existing gauge. 注册或返回已有的仪表盘。
      Parameters:
      name - the metric name | 指标名称
      supplier - the value supplier, must not be null | 值供应者,不能为 null
      tags - the optional tags | 可选标签
      Returns:
      the Gauge instance | 仪表盘实例
      Throws:
      ObservabilityException - if supplier is null, a different type is registered, or limit exceeded 如果 supplier 为 null、已注册不同类型或超出限制
    • timer

      public Timer timer(String name, Tag... tags)
      Registers or returns an existing timer. 注册或返回已有的计时器。
      Parameters:
      name - the metric name | 指标名称
      tags - the optional tags | 可选标签
      Returns:
      the Timer instance | 计时器实例
      Throws:
      ObservabilityException - if a different metric type is already registered, or limit exceeded 如果已注册了不同的指标类型,或超出限制
    • histogram

      public Histogram histogram(String name, Tag... tags)
      Registers or returns an existing histogram. 注册或返回已有的直方图。
      Parameters:
      name - the metric name | 指标名称
      tags - the optional tags | 可选标签
      Returns:
      the Histogram instance | 直方图实例
      Throws:
      ObservabilityException - if a different metric type is already registered, or limit exceeded 如果已注册了不同的指标类型,或超出限制
    • find

      public Optional<?> find(String name)
      Finds the first metric matching the given name. 查找第一个匹配给定名称的指标。
      Parameters:
      name - the metric name to search for | 要搜索的指标名称
      Returns:
      an Optional containing the metric, or empty | 包含指标的 Optional,或空
    • snapshot

      public List<MetricSnapshot> snapshot()
      Returns a snapshot of all registered metrics. 返回所有已注册指标的快照。
      Returns:
      a list of MetricSnapshot | MetricSnapshot 列表
    • remove

      public boolean remove(MetricId id)
      Removes the metric with the given id. 移除给定 ID 的指标。
      Parameters:
      id - the MetricId to remove | 要移除的 MetricId
      Returns:
      true if the metric was removed | 如果指标被移除则返回 true
    • clear

      public void clear()
      Removes all registered metrics. 移除所有已注册的指标。
    • size

      public int size()
      Returns the number of registered metrics. 返回已注册指标的数量。
      Returns:
      the count | 数量