kumulant

CounterRateStat

class CounterRateStat(val concurrency: Concurrency = Concurrency.None, val treatDecreaseAsReset: Boolean = StatDefaults.COUNTER_RATE_TREAT_DECREASE_AS_RESET) : SeriesStat<RateResult> (source)

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

constructor(concurrency: Concurrency = Concurrency.None, treatDecreaseAsReset: Boolean = StatDefaults.COUNTER_RATE_TREAT_DECREASE_AS_RESET)(source)

Properties

concurrency

open override val concurrency: Concurrency(source)

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

open override fun create(concurrency: Concurrency? = null): CounterRateStat(source)

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

open override fun merge(values: RateResult, timestampNanos: Long = currentTimeNanos())(source)

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

open override fun read(timestampNanos: Long = currentTimeNanos()): RateResult(source)

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

open override fun reset()(source)

Restore the configured baseline, including any prior seed. Preserves the configuration and concurrency.

Link copied to clipboard
open fun update(value: Double, weight: Double = 1.0)

Record value with weight at the current monotonic time.

open override fun update(value: Double, timestampNanos: Long, weight: Double = 1.0)

Record value with weight at timestampNanos.

Link copied to clipboard
open fun validateMerge(values: RateResult)

Validate values for merge without changing accumulator state. Containers validate every child before merging any of them. Successful validation checks compatibility; it does not make a concurrent merge transactional.

update

open override fun update(value: Double, timestampNanos: Long, weight: Double = 1.0)(source)

Record value with weight at timestampNanos.