pub struct Counter { /* private fields */ }Expand description
A sharded atomic counter.
Shards cache-line aligned AtomicIsize values across a vector for faster updates in high contention scenarios.
Implementations§
Source§impl Counter
impl Counter
Sourcepub fn new(count: usize) -> Self
pub fn new(count: usize) -> Self
Creates a new Counter with at least count cells.
The count is rounded up to the next power of two for fast modulo.
Sourcepub fn add_with_ordering(&self, value: isize, ordering: Ordering)
pub fn add_with_ordering(&self, value: isize, ordering: Ordering)
Adds a value to the counter with the specified ordering.
Sourcepub fn sum(&self) -> isize
pub fn sum(&self) -> isize
Returns the sum of all shards using relaxed ordering.
§Eventual Consistency
Due to sharding, this may be slightly inaccurate under heavy concurrent modification - writes to already-summed shards won’t be reflected until the next call. The total is eventually consistent.
Sourcepub fn sum_with_ordering(&self, ordering: Ordering) -> isize
pub fn sum_with_ordering(&self, ordering: Ordering) -> isize
Returns the sum of all shards with the specified ordering.
Sourcepub fn swap(&self) -> isize
pub fn swap(&self) -> isize
Resets all shards to zero and returns the previous sum.
Useful for delta-style metrics export.
§Eventual Consistency
Writes that occur concurrently with swap() may be attributed to the
next window rather than the current one. This is because shards are
swapped sequentially - a write landing on an already-swapped shard
will be picked up by the next swap() call. No counts are lost; they
simply shift to the next export window. For telemetry purposes with
multi-second export intervals, this timing skew is negligible.
Trait Implementations§
Source§impl ClickHouseExport for Counter
impl ClickHouseExport for Counter
fn export_clickhouse( &self, batch: &mut ClickHouseMetricBatch, name: &str, description: &str, time_unix_nano: u64, )
Source§impl DogStatsDExport for Counter
impl DogStatsDExport for Counter
Source§impl OtlpExport for Counter
impl OtlpExport for Counter
Auto Trait Implementations§
impl Freeze for Counter
impl RefUnwindSafe for Counter
impl Send for Counter
impl Sync for Counter
impl Unpin for Counter
impl UnsafeUnpin for Counter
impl UnwindSafe for Counter
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