Skip to main content

Reporter

Struct Reporter 

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

Records telemetry events, ships the ones worth their bytes, and counts them all.

A reporter keeps a level threshold and forwards only events at or above it, while counting every event it sees - dropped or not - so the aggregate picture survives even when the link is too costly to ship detail. Call adapt_to as the link cost changes to raise or lower the bar, and ship a snapshot of the counters periodically instead of the full stream.

§Examples

use pamoja_telemetry::{Event, Level, LinkCost, Reporter};

let mut reporter = Reporter::new(Level::Trace);

// On a metered link, routine debug events are dropped but a warning still ships.
reporter.adapt_to(LinkCost::Metered);
assert!(reporter.record(Event::debug("loop.tick")).is_none());
assert!(reporter.record(Event::warn("battery.low")).is_some());

// Both events were still counted.
assert_eq!(reporter.total(), 2);
assert_eq!(reporter.dropped(), 1);

Implementations§

Source§

impl Reporter

Source

pub fn new(threshold: Level) -> Self

Creates a reporter that ships events at or above threshold.

§Arguments
  • threshold - the minimum level to ship.
§Returns

A reporter with empty counters.

Source

pub fn threshold(&self) -> Level

Returns the current ship threshold.

§Returns

The minimum level currently being shipped.

Source

pub fn set_threshold(&mut self, threshold: Level)

Sets the ship threshold directly.

§Arguments
  • threshold - the new minimum level to ship.
Source

pub fn adapt_to(&mut self, cost: LinkCost)

Raises or lowers the threshold to match the current link cost.

§Arguments
  • cost - how costly the link currently is.
Source

pub fn record(&mut self, event: Event) -> Option<Event>

Records an event, returning it to ship if it clears the threshold.

The event is counted whether or not it is shipped, so the aggregate counts stay complete even while detail is held back.

§Arguments
  • event - the event to record.
§Returns

Some(event) if it should be shipped, or None if it was dropped by the threshold.

Source

pub fn count(&self, level: Level) -> u32

Returns how many events have been seen at level, shipped or not.

§Arguments
  • level - the level to count.
§Returns

The number of events recorded at that level.

Source

pub fn total(&self) -> u32

Returns the total number of events seen across all levels.

§Returns

The total count.

Source

pub fn emitted(&self) -> u32

Returns how many events passed the threshold and were shipped.

§Returns

The emitted count.

Source

pub fn dropped(&self) -> u32

Returns how many events were dropped by the threshold.

§Returns

The dropped count.

Source

pub fn snapshot(&self) -> Snapshot

Returns a snapshot of the counters to ship in place of the raw stream.

§Returns

A Snapshot of the per-level counts and the emitted and dropped totals.

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.