#![allow(clippy::arithmetic_side_effects)]
pub mod counter;
pub mod datapoint;
pub mod metrics;
pub mod poh_timing_point;
pub use crate::metrics::{flush, query, set_host_id, set_panic_hook, submit};
use std::sync::{
atomic::{AtomicU64, Ordering},
Arc,
};
#[derive(Default)]
pub struct MovingStat {
value: AtomicU64,
}
impl MovingStat {
pub fn update_stat(&self, old_value: &MovingStat, new_value: u64) {
let old = old_value.value.swap(new_value, Ordering::Acquire);
self.value
.fetch_add(new_value.saturating_sub(old), Ordering::Release);
}
pub fn load_and_reset(&self) -> u64 {
self.value.swap(0, Ordering::Acquire)
}
}
#[allow(clippy::redundant_allocation)]
pub struct TokenCounter(Arc<&'static str>);
impl TokenCounter {
pub fn new(name: &'static str) -> Self {
Self(Arc::new(name))
}
pub fn create_token(&self) -> CounterToken {
datapoint_info!(*self.0, ("count", Arc::strong_count(&self.0), i64));
CounterToken(self.0.clone())
}
}
#[allow(clippy::redundant_allocation)]
pub struct CounterToken(Arc<&'static str>);
impl Clone for CounterToken {
fn clone(&self) -> Self {
datapoint_info!(*self.0, ("count", Arc::strong_count(&self.0), i64));
CounterToken(self.0.clone())
}
}
impl Drop for CounterToken {
fn drop(&mut self) {
datapoint_info!(
*self.0,
("count", Arc::strong_count(&self.0).saturating_sub(2), i64)
);
}
}
impl Drop for TokenCounter {
fn drop(&mut self) {
datapoint_info!(
*self.0,
("count", Arc::strong_count(&self.0).saturating_sub(2), i64)
);
}
}