Skip to main content

MeasurementHistogram

Struct MeasurementHistogram 

Source
pub struct MeasurementHistogram<const N: usize> { /* private fields */ }
Expand description

Canonical shared arithmetic for local diagnostic summaries and distributions.

Re-exported so callers can name returned summaries without selecting a separate Metrics dependency. This is the shared type, with no local wrapper or arithmetic. Fixed-size histogram of observations in one consumer-selected unit.

N strictly increasing inclusive upper bounds define N disjoint buckets. The first bucket includes zero; later buckets exclude the preceding bound. Values above the last bound go into a separate overflow bucket. With no bounds, every observation goes into overflow. A final bound of u64::MAX is valid and leaves overflow empty.

Each observation updates one bucket and the accompanying MeasurementSummary. Bucket counts saturate independently at u64::MAX. Treat a count at that value as unavailable for exact arithmetic, even when reached exactly. Unsaturated bucket counts remain useful after the summary’s total saturates. Saturation can prevent bucket counts from summing to the summary’s sample count. Buckets describe ranges, not exact percentiles.

Storage is fixed arrays, one overflow count and one summary, with no heap allocation. Recording searches at most N bounds. Consumers choose bounds, units, sample admission, identity and reset boundaries. Bounds are immutable after construction; cumulative reporting and persistence remain consumer-owned.

use ic_metrics::MeasurementHistogram;

let mut histogram = MeasurementHistogram::new([10, 100])?;
for value in [0, 10, 11, 101] {
    histogram.record(value);
}
assert_eq!(histogram.bucket_counts(), &[2, 1]);
assert_eq!(histogram.overflow(), 1);
assert_eq!(histogram.summary().samples(), 4);

Implementations§

Source§

impl<const N: usize> MeasurementHistogram<N>

Source

pub const fn new( upper_bounds: [u64; N], ) -> Result<MeasurementHistogram<N>, HistogramBoundsError>

Construct an empty histogram with inclusive upper bounds.

Bounds and observations must use the same unit. A bound of zero and an empty bounds array are valid.

§Errors

Returns HistogramBoundsError for the first duplicate or descending bound. The error identifies the right-hand bound in that pair.

Source

pub const fn record(&mut self, value: u64)

Record one completed observation, including zero.

Updates the summary and exactly one disjoint bucket. Every observation must use the same unit as the bounds and earlier observations.

Source

pub const fn upper_bounds(&self) -> &[u64; N]

Immutable inclusive upper bounds corresponding to Self::bucket_counts.

Source

pub const fn bucket_counts(&self) -> &[u64; N]

Disjoint bucket counts, each saturating independently at u64::MAX.

These exclude overflow and are not cumulative counts.

Source

pub const fn overflow(&self) -> u64

Count above the last bound, saturating independently at u64::MAX.

With no bounds, this counts all observations.

Source

pub const fn summary(&self) -> MeasurementSummary

Summary of all observations, including overflow.

Trait Implementations§

Source§

impl<const N: usize> Clone for MeasurementHistogram<N>

Source§

fn clone(&self) -> MeasurementHistogram<N>

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<const N: usize> Copy for MeasurementHistogram<N>

Source§

impl<const N: usize> Debug for MeasurementHistogram<N>

Source§

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

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

impl<const N: usize> Eq for MeasurementHistogram<N>

Source§

impl<const N: usize> PartialEq for MeasurementHistogram<N>

Source§

fn eq(&self, other: &MeasurementHistogram<N>) -> bool

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

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

Inequality operator !=. Read more
Source§

impl<const N: usize> StructuralPartialEq for MeasurementHistogram<N>

Auto Trait Implementations§

§

impl<const N: usize> Freeze for MeasurementHistogram<N>
where [u64; N]: Freeze,

§

impl<const N: usize> RefUnwindSafe for MeasurementHistogram<N>
where [u64; N]: RefUnwindSafe,

§

impl<const N: usize> Send for MeasurementHistogram<N>
where [u64; N]: Send,

§

impl<const N: usize> Sync for MeasurementHistogram<N>
where [u64; N]: Sync,

§

impl<const N: usize> Unpin for MeasurementHistogram<N>
where [u64; N]: Unpin,

§

impl<const N: usize> UnsafeUnpin for MeasurementHistogram<N>
where [u64; N]: UnsafeUnpin,

§

impl<const N: usize> UnwindSafe for MeasurementHistogram<N>
where [u64; N]: UnwindSafe,

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> Same for T

Source§

type Output = T

Should always be Self
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.