MetricHistogram

Struct MetricHistogram 

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

Fixed-bucket latency histogram.

Tracks the distribution of values (typically durations in microseconds) across predefined buckets. No allocator needed — the bucket array is inline. Uses 10 buckets: 100us, 500us, 1ms, 5ms, 10ms, 50ms, 100ms, 500ms, 1s, +Inf.

Buckets are cumulative: each bucket count includes all observations that also fall into lower buckets. This matches the Prometheus histogram convention.

§Examples

use grafos_observe::MetricHistogram;

let h = MetricHistogram::new();
h.observe(50);     // 50us — lands in the <=100us bucket
h.observe(2_000);  // 2ms  — lands in the <=5ms bucket

assert_eq!(h.count(), 2);
assert_eq!(h.sum(), 2050);
assert_eq!(h.bucket_count(0), 1); // <=100us: only the 50us observation
assert_eq!(h.bucket_count(3), 2); // <=5ms: both observations

Implementations§

Source§

impl MetricHistogram

Source

pub const NUM_BUCKETS: usize = 10usize

Number of histogram buckets.

Source

pub const BUCKET_BOUNDS: [u64; 10]

Bucket boundaries in microseconds.

Source

pub const fn new() -> Self

Create a new histogram with the default bucket boundaries.

Source

pub fn observe(&self, value_us: u64)

Record an observation (value in microseconds).

Increments the count for every bucket whose bound is >= the value (cumulative histogram, matching Prometheus convention).

Source

pub fn bucket_count(&self, index: usize) -> u64

Read the cumulative count for a specific bucket index.

Source

pub fn bucket_bound(&self, index: usize) -> u64

Read the bucket upper bound for a specific bucket index (in microseconds).

Source

pub fn count(&self) -> u64

Total number of observations.

Source

pub fn sum(&self) -> u64

Sum of all observed values (in microseconds).

Trait Implementations§

Source§

impl Default for MetricHistogram

Source§

fn default() -> Self

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

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.