CounterRateStat
Rate derived from a monotonic counter stream.
Each update is interpreted as an absolute counter sample (for example requests_total). The stat advances a high-water-mark counter; the running total is the sum of all forward increments. The rate at read time is totalDelta / (readTs - startTs).
Ordering is by counter value, not timestamp. Two writers submitting samples with overlapping timestamps work fine as long as their counter values are globally monotonic (e.g. each pulled from a shared AtomicLong). An explicit reset (counter decrease with treatDecreaseAsReset) re-anchors the start window to the post-reset timestamp.
Use cases: scraping monotonic counters (Prometheus *_total metrics, OS counter readings, replicated request counts). Pair with a Prometheus-style scraper that periodically samples an external counter.
Memory: O(1); total delta + start timestamp + (lastCounter, lastTs).
Update: O(1) per observation under the per-stat lock.
Concurrency: Body locked under any concurrent Concurrency level (no-op under Concurrency.None); safe under any number of concurrent writers without external synchronisation. Forward-progressing samples advance the high-water mark; decreases either reset (when treatDecreaseAsReset) or drop silently. The start timestamp converges to the earliest observed predecessor via CAS-loop-min.
Constructors
CounterRateStat
Properties
concurrency
Thread-safety mode fixed at construction. See Concurrency for the shared contract and the implementation's KDoc for its cell and lock strategy.
treatDecreaseAsReset
When true (default), a counter decrease is interpreted as a reset and the new sample value is counted as post-reset progress. Set to false to drop decreases entirely; the right choice when the underlying counter never resets and multiple writers may submit samples out of value order.
Functions
create
Create an independent accumulator at the configured baseline. A null concurrency preserves this accumulator's mode; a non-null value overrides it. Modality subtypes narrow the return type to preserve the input shape.
merge
Fold a compatible snapshot values into this accumulator at timestampNanos. Implementations document whether the combine is exact or approximate and which state the snapshot omits. The source accumulator is unaffected.
Time-dependent statistics, such as windows, place the snapshot at timestampNanos, which must share the clock the caller uses for updates and reads. Time-independent statistics ignore it.
read
Return the current result evaluated at timestampNanos. Time-independent statistics ignore the timestamp. Reading does not add an observation.
Snapshot consistency depends on the implementation's synchronisation. Locks protect coupled state under Concurrency.Strict and Concurrency.HighWrite; independently updated cells and JVM striped adders can still yield a best-effort snapshot. Concurrency.Relaxed permits drift across coupled cells.
reset
Restore the configured baseline, including any prior seed. Preserves the configuration and concurrency.