Class MetricRegistry
java.lang.Object
cloud.opencode.base.observability.metric.MetricRegistry
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 Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all registered metrics.Registers or returns an existing counter.static MetricRegistrycreate()Creates a registry with the default max metrics limit (10,000).static MetricRegistrycreate(int maxMetrics) Creates a registry with the specified max metrics limit.Optional<?> Finds the first metric matching the given name.Registers or returns an existing gauge.Registers or returns an existing histogram.booleanRemoves the metric with the given id.intsize()Returns the number of registered metrics.snapshot()Returns a snapshot of all registered metrics.Registers or returns an existing timer.
-
Method Details
-
create
Creates a registry with the default max metrics limit (10,000). 使用默认最大指标限制(10,000)创建注册表。- Returns:
- a new MetricRegistry | 新的 MetricRegistry
-
create
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
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
Registers or returns an existing gauge. 注册或返回已有的仪表盘。- Parameters:
name- the metric name | 指标名称supplier- the value supplier, must not be null | 值供应者,不能为 nulltags- the optional tags | 可选标签- Returns:
- the Gauge instance | 仪表盘实例
- Throws:
ObservabilityException- if supplier is null, a different type is registered, or limit exceeded 如果 supplier 为 null、已注册不同类型或超出限制
-
timer
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
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
-
snapshot
Returns a snapshot of all registered metrics. 返回所有已注册指标的快照。- Returns:
- a list of MetricSnapshot | MetricSnapshot 列表
-
remove
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 | 数量
-