Interface Counter


public interface Counter
Counter - A monotonically increasing counter metric Counter - 单调递增的计数器指标

Counters track cumulative values that only increase (or reset). Useful for counting requests, errors, or completed tasks.

计数器跟踪仅增加(或重置)的累计值。 适用于计数请求、错误或已完成的任务。

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

    Modifier and Type
    Method
    Description
    long
    Returns the current count.
    id()
    Returns the metric identifier.
    void
    Increments the counter by one.
    void
    increment(long amount)
    Increments the counter by the given amount.
    void
    Resets the counter to zero.
  • Method Details

    • increment

      void increment()
      Increments the counter by one. 将计数器加一。
    • increment

      void increment(long amount)
      Increments the counter by the given amount. 将计数器增加指定的数量。
      Parameters:
      amount - the amount to add, must be non-negative | 增加的数量,不能为负
      Throws:
      ObservabilityException - if amount is negative | 如果数量为负
    • count

      long count()
      Returns the current count. 返回当前计数值。
      Returns:
      the current count | 当前计数
    • reset

      void reset()
      Resets the counter to zero. 将计数器重置为零。

      Concurrency note | 并发注意事项: Reset is NOT atomic with respect to concurrent increment() calls. An increment racing with reset may be lost. This is acceptable for observability counters where approximate values are sufficient.

      重置操作与并发 increment() 调用不是原子的。 与重置竞争的增量可能会丢失。对于近似值足够的可观测性计数器,这是可接受的。

    • id

      MetricId id()
      Returns the metric identifier. 返回指标标识符。
      Returns:
      the MetricId | 指标 ID