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.
Properties
The thread-safety contract this stat was constructed with. Each stat picks the cell-encoding and lock strategy that honours this contract for its mathematical structure:
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
Spawn a fresh accumulator with the same configuration. Optionally override the Concurrency; useful for materialising a wire spec at a different concurrency level than the source.
Fold another accumulator's snapshot into this one. The unit of merge is the immutable Result; not a live Stat; which is what lets the merge cross a process boundary. Many workers track slices of the same stream, call read periodically, ship snapshots to a coordinator, and the coordinator merges them in.
Materialise the current state as an immutable Result. Reads never mutate, so the caller can read as often as it likes without affecting the stream.
Reset the stat to its prior-seeded baseline. Equivalent to constructing a fresh stat with the same configuration, but in place; keeps the same Concurrency and any per-stat tunables.
Record an observation with the given weight, stamped at the current time.
Record an observation at timestampNanos with the given weight. Stats that consume time (rates, decay, windowing) use this as the ordering signal; pass a monotonic stamp when feeding from a replay log.
CounterRateStat
concurrency
The thread-safety contract this stat was constructed with. Each stat picks the cell-encoding and lock strategy that honours this contract for its mathematical structure:
Concurrency.None: single-threaded; no synchronisation. Cheapest path.
Concurrency.Relaxed: lock-free best-effort. Multi-cell stats (Welford-style MeanStat, VarianceStat, MomentsStat) may drift under contention but never throw.
Concurrency.Strict: serialised when needed for full correctness across coupled cells. Sketches always self-serialise; Welford stats lock per update.
Concurrency.HighWrite: optimised for many concurrent writers; JVM uses striped adders for naively additive stats.
Picked at construction; immutable after.
create
Spawn a fresh accumulator with the same configuration. Optionally override the Concurrency; useful for materialising a wire spec at a different concurrency level than the source.
The returned stat is independent: its state starts at the configured baseline, not at the source's current state. Each modality subtype narrows the return type so chaining doesn't lose the modality.
merge
Fold another accumulator's snapshot into this one. The unit of merge is the immutable Result; not a live Stat; which is what lets the merge cross a process boundary. Many workers track slices of the same stream, call read periodically, ship snapshots to a coordinator, and the coordinator merges them in.
Most stat families implement merge exactly (Chan-style parallel formulas for Welford, cell-wise additions for histograms, cell-wise max for HLL). SGD-based regressors merge approximately; they have no second-moment information for the principled combine. Each stat's KDoc documents its merge semantics.
read
Materialise the current state as an immutable Result. Reads never mutate, so the caller can read as often as it likes without affecting the stream.
Snapshot consistency depends on the configured Concurrency. Under Concurrency.Strict / Concurrency.HighWrite a read locks against writers so coupled cells stay consistent. Under Concurrency.Relaxed the cells race and the snapshot may drift by ULPs of the workload under heavy contention; the drift is bounded and the read never throws.
timestampNanos is the read timestamp. Stats that don't care about time silently drop it; stats that do (rates, decay families, recency, windowed wrappers) use it as the ordering signal.
reset
Reset the stat to its prior-seeded baseline. Equivalent to constructing a fresh stat with the same configuration, but in place; keeps the same Concurrency and any per-stat tunables.
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.