Skip to main content

MeasurementCounter

Struct MeasurementCounter 

Source
pub struct MeasurementCounter { /* private fields */ }
Expand description

Shared, inert measurement arithmetic; registration identity remains timer-owned. A scalar total in one consumer-selected unit with tracked actual overflow.

Use for diagnostic event counts, item counts or quantities such as bytes. Each addition increases the total without retaining a sample count, maximum, latest value or distribution. Zero is an exact total, not an absent metric; consumers own whether a counter exists and which events may increment it.

Totals clip at u128::MAX with a sticky flag for actual overflow. An exactly reached cap remains exact. Inputs must already be exact: adding a clamped quantity cannot restore its lost quality. Consumers own units, identities, reset boundaries and reporting. No allocation, platform reads or logging. Diagnostic totals do not replace authoritative budgets, balances or debt.

use ic_metrics::MeasurementCounter;
let mut retries = MeasurementCounter::ZERO;
retries.add(1);
retries.add(1);
assert_eq!(retries.exact_total()?, 2);
let mut bytes = MeasurementCounter::ZERO;
let before = bytes;
bytes.add(32_768);
bytes.add(0);
assert_eq!(bytes.delta_since(before)?, 32_768);

Implementations§

Source§

impl MeasurementCounter

Source

pub const ZERO: MeasurementCounter

Zero total with no actual overflow; zero additions remain exact.

Source

pub const fn from_parts( total: u128, total_overflowed: bool, ) -> Result<MeasurementCounter, MeasurementStateError>

Restore a total and its actual-overflow flag after numeric admission.

A cap with a false flag remains exact. The caller owns provenance, units and identity; missing or previously discarded quality cannot be inferred.

§Errors

Returns MeasurementStateError::InvalidOverflowFlag when a true flag accompanies a total below its clipped cap.

Source

pub const fn add(&mut self, amount: u128)

Add an admitted exact quantity, including zero, in the existing unit.

Use one for an event; larger quantities can account for a whole batch. This never wraps or clears an existing overflow flag.

Source

pub const fn merge(&mut self, other: MeasurementCounter)

Add a counter’s total while preserving any source overflow.

Consumers establish matching units and deliberate attribution. This is addition, not deduplication: merging the same batch twice counts it twice.

Source

pub const fn total(self) -> u128

Total clipped at u128::MAX; check Self::total_overflowed.

Source

pub const fn total_overflowed(self) -> bool

Whether addition or merging has actually overflowed the total.

Source

pub const fn exact_total(self) -> Result<u128, MeasurementCounterError>

Exact total, including zero and an exactly reached cap.

Use a checked conversion when a consumer report requires a narrower type; a narrowing failure is distinct from this accumulator’s overflow.

§Errors

Returns MeasurementCounterError::TotalOverflow on actual overflow.

Source

pub const fn delta_since( self, previous: MeasurementCounter, ) -> Result<u128, MeasurementCounterError>

Exact difference from an earlier snapshot of this counter, including zero.

Consumers must first establish matching units, counter/window identity and append-only continuity. Numeric success cannot detect a reset followed by larger values, changed attribution or an unrelated counter.

§Errors

Rejects actual overflow in either snapshot before a decreasing total.

Trait Implementations§

Source§

impl Clone for MeasurementCounter

Source§

fn clone(&self) -> MeasurementCounter

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for MeasurementCounter

Source§

impl Debug for MeasurementCounter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for MeasurementCounter

Source§

fn default() -> MeasurementCounter

Returns the “default value” for a type. Read more
Source§

impl Eq for MeasurementCounter

Source§

impl PartialEq for MeasurementCounter

Source§

fn eq(&self, other: &MeasurementCounter) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for MeasurementCounter

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.