pub struct CounterSet { /* private fields */ }Expand description
A grouped set of related sharded counters.
Use CounterSet when one hot-path operation commonly updates several
counters together, such as request count, bytes processed, and cache hits.
The counters share the same shard row, so updates by the same thread touch a
compact row while rows for different shards remain padded apart to avoid
false sharing.
CounterSet still supports individual counter updates by index. Resolve
those indexes once during construction and keep direct handles/indexes on the
hot path.
use fast_telemetry::CounterSet;
const REQUESTS: usize = 0;
const BYTES: usize = 1;
const HITS: usize = 2;
let counters = CounterSet::new(4, 3);
counters.inc(REQUESTS);
counters.add(BYTES, 4096);
counters.inc(HITS);
assert_eq!(counters.sum(REQUESTS), 1);
assert_eq!(counters.sum(BYTES), 4096);
assert_eq!(counters.sum(HITS), 1);Implementations§
Source§impl CounterSet
impl CounterSet
Sourcepub fn new(shards: usize, counters: usize) -> Self
pub fn new(shards: usize, counters: usize) -> Self
Creates a grouped counter set with counters counters per shard.
Shards are padded by row instead of by cell, so related counters updated by the same thread sit contiguously while adjacent shard rows remain separated enough to avoid false sharing.
Sourcepub fn inc(&self, counter_idx: usize)
pub fn inc(&self, counter_idx: usize)
Increments one counter in the current thread’s shard row.
Sourcepub fn add(&self, counter_idx: usize, value: isize)
pub fn add(&self, counter_idx: usize, value: isize)
Adds a value to one counter in the current thread’s shard row.
Sourcepub fn add_all(&self, value: isize)
pub fn add_all(&self, value: isize)
Adds the same value to all counters in the current thread’s shard row.
Sourcepub fn add_indices(&self, indexes: &[usize], value: isize)
pub fn add_indices(&self, indexes: &[usize], value: isize)
Adds the same value to selected counters in the current thread’s shard row.
Sourcepub fn add_index_values(&self, updates: &[(usize, isize)])
pub fn add_index_values(&self, updates: &[(usize, isize)])
Adds one value per selected counter in the current thread’s shard row.
Sourcepub fn add_all_indexed(&self, value: isize)
pub fn add_all_indexed(&self, value: isize)
Adds the same value to all counters in the current thread’s shard row.
Sourcepub fn add_values(&self, values: &[isize])
pub fn add_values(&self, values: &[isize])
Adds one value per counter in the current thread’s shard row.
Sourcepub fn sum(&self, counter_idx: usize) -> isize
pub fn sum(&self, counter_idx: usize) -> isize
Returns the sum for one counter across all shards.
Sourcepub fn sum_and_reset(&self, counter_idx: usize) -> isize
pub fn sum_and_reset(&self, counter_idx: usize) -> isize
Resets one counter to zero and returns its previous sum.
§Eventual Consistency
Writes that occur concurrently with sum_and_reset() may be attributed
to the next window rather than the current one. No counts are lost; they
simply shift to the next export window.
Sourcepub fn snapshot_and_reset(&self) -> Vec<isize>
pub fn snapshot_and_reset(&self) -> Vec<isize>
Returns one value per counter in index order, then resets all counters.
§Eventual Consistency
Writes that occur concurrently with snapshot_and_reset() may be
attributed to the next window rather than the current one. No counts are
lost; they simply shift to the next export window.
Auto Trait Implementations§
impl Freeze for CounterSet
impl RefUnwindSafe for CounterSet
impl Send for CounterSet
impl Sync for CounterSet
impl Unpin for CounterSet
impl UnsafeUnpin for CounterSet
impl UnwindSafe for CounterSet
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request