Skip to main content

Sensor

Struct Sensor 

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

A lock-free exponential moving average (EMA) gauge.

Useful for tracking smoothed rates or throughput where you want to dampen spikes. The smoothing factor alpha controls responsiveness:

  • alpha close to 1.0 → responds quickly to new values (noisy)
  • alpha close to 0.0 → responds slowly (smooth)

A typical value for per-second throughput is 0.05.

§Example

use prometheus_extensions::Sensor;

let sensor = Sensor::new(0.05);
for _ in 0..100 {
    sensor.measure(1000.0);
}
// After many identical measurements, EMA converges to the input value.
assert!((sensor.get() - 1000.0).abs() < 10.0);

Implementations§

Source§

impl Sensor

Source

pub fn new(alpha: f64) -> Self

Create a new sensor with the given smoothing factor alpha.

§Panics

Panics if alpha is not in the range (0.0, 1.0].

Source

pub fn measure(&self, value: f64)

Feed a new measurement into the EMA.

Source

pub fn get(&self) -> f64

Read the current smoothed value.

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.