CounterSeries

Struct CounterSeries 

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

CounterSeries implements a time series stored in a sequence of a fixed number of buckets each with a fixed (and equal) duration.

The buckets are implemented as a ring buffer held in memory. The counter can be incremented or updated to a new value, but only for the bucket representing the current point in time.

As time elapses, the current bucket changes based on the bucket duration, with older buckets being zeroed out. No background maintenance tasks are required to manage this rotation, as the counter series maintains book keeping to fixup the structure prior to accessing the buckets.

The value tracked in each bucket is a u64, meaning that we cannot track negative numbers. If you try to delta outside the valid range, the resulting value is saturated to the bounds of a u64; it will never be less than zero and never wrap around due to overflow.

Implementations§

Source§

impl CounterSeries

Source

pub fn with_config(config: CounterSeriesConfig) -> Self

Create a new instance. All buckets will be initialized to zero.

Source

pub fn with_initial_value(config: CounterSeriesConfig, value: u64) -> Self

Create a new instance with a pre-set initial value. Useful when setting up the initial state for observation based tracking

Source

pub fn increment(&mut self, to_add: u64)

Increment the counter for the current time window by the specified value.

Source

pub fn delta(&mut self, delta: i64)

Adjust the counter for the current time window by the specified value

Source

pub fn observe(&mut self, current_value: u64)

Record an observation; assigns current_value to the current bucket

Source

pub fn sum(&mut self) -> u64

Returns the total tracked over the entire series duration

Source

pub fn sum_over(&mut self, duration: Duration) -> u64

Returns the total tracked over a specific time duration. Rounds up to the next bucket for spans smaller than the bucket size.

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.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more