pub struct CounterTracker { /* private fields */ }Expand description
One cumulative counter’s baseline and the rules for reading the next value.
§Monotonic time
The tracker stores the Instant of every reading and derives the interval
with Instant::saturating_duration_since. A wall-clock jump therefore
cannot shorten, lengthen, or negate an interval, which is exactly what §8.1
requires. Callers must pass the snapshot’s monotonic captured_at and
never an Instant reconstructed from a SystemTime.
§Example
use core::time::Duration;
use std::time::Instant;
use monitrs_core::rates::{CounterTracker, CounterWidth};
let mut rx = CounterTracker::new(CounterWidth::Bits64);
let start = Instant::now();
// The first reading establishes a baseline; there is nothing to divide yet.
assert!(rx.rate(1_000, start).is_warming_up());
// 2 000 bytes over half a second is 4 000 B/s, not 2 000 B/s.
let state = rx.rate(3_000, start + Duration::from_millis(500));
let rate = state.fresh().copied().expect("second sample is measurable");
assert_eq!(rate.per_second(), 4_000.0);Implementations§
Source§impl CounterTracker
impl CounterTracker
Sourcepub const fn new(width: CounterWidth) -> Self
pub const fn new(width: CounterWidth) -> Self
Builds a tracker with no baseline, for a counter of the given width.
Sourcepub const fn width(&self) -> CounterWidth
pub const fn width(&self) -> CounterWidth
The width this tracker was told to assume.
Sourcepub const fn is_warming_up(&self) -> bool
pub const fn is_warming_up(&self) -> bool
Whether the next reading will be the first, and so warming up (§8.2).
Sourcepub const fn last_value(&self) -> Option<u64>
pub const fn last_value(&self) -> Option<u64>
The last accepted reading, or None while warming up.
Sourcepub const fn last_observed_at(&self) -> Option<Instant>
pub const fn last_observed_at(&self) -> Option<Instant>
When the last reading was accepted, or None while warming up.
Sourcepub fn forget_baseline(&mut self)
pub fn forget_baseline(&mut self)
Drops the baseline so the next reading warms up again.
Collectors call this when the thing behind the counter changed identity — a renamed interface, a re-created device node, a reused PID — because a delta across such a change describes two different counters (§8.2).
Sourcepub fn observe(&mut self, value: u64, at: Instant) -> CounterDelta
pub fn observe(&mut self, value: u64, at: Instant) -> CounterDelta
Folds one cumulative reading in and classifies the movement.
at must be monotonic; see the type-level note on time.
Trait Implementations§
Source§impl Clone for CounterTracker
impl Clone for CounterTracker
Source§fn clone(&self) -> CounterTracker
fn clone(&self) -> CounterTracker
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl Copy for CounterTracker
Source§impl Debug for CounterTracker
impl Debug for CounterTracker
Source§impl DeltaTracker for CounterTracker
impl DeltaTracker for CounterTracker
Source§type Config = CounterWidth
type Config = CounterWidth
Source§fn with_config(config: Self::Config) -> Self
fn with_config(config: Self::Config) -> Self
Source§fn observe_reading(
&mut self,
reading: Self::Reading,
at: Instant,
) -> MetricState<Self::Value>
fn observe_reading( &mut self, reading: Self::Reading, at: Instant, ) -> MetricState<Self::Value>
at.Source§fn last_observed_at(&self) -> Option<Instant>
fn last_observed_at(&self) -> Option<Instant>
None while warming up.