2021-02-16 14:48:20 -07:00
|
|
|
#![allow(clippy::integer_arithmetic)]
|
2019-02-18 23:26:22 -07:00
|
|
|
pub mod counter;
|
2019-09-07 12:48:45 -07:00
|
|
|
pub mod datapoint;
|
2018-11-16 08:45:59 -08:00
|
|
|
mod metrics;
|
2019-08-30 15:33:30 -07:00
|
|
|
pub use crate::metrics::{flush, query, set_host_id, set_panic_hook, submit};
|
2021-09-17 22:40:14 +03:00
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
/// A helper that sends the count of created tokens as a datapoint.
|
|
|
|
#[allow(clippy::redundant_allocation)]
|
|
|
|
pub struct TokenCounter(Arc<&'static str>);
|
|
|
|
|
|
|
|
impl TokenCounter {
|
|
|
|
/// Creates a new counter with the specified metrics `name`.
|
|
|
|
pub fn new(name: &'static str) -> Self {
|
|
|
|
Self(Arc::new(name))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new token for this counter. The metric's value will be equal
|
|
|
|
/// to the number of `CounterToken`s.
|
|
|
|
pub fn create_token(&self) -> CounterToken {
|
|
|
|
// new_count = strong_count
|
|
|
|
// - 1 (in TokenCounter)
|
|
|
|
// + 1 (token that's being created)
|
|
|
|
datapoint_info!(*self.0, ("count", Arc::strong_count(&self.0), i64));
|
|
|
|
CounterToken(self.0.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A token for `TokenCounter`.
|
|
|
|
#[allow(clippy::redundant_allocation)]
|
|
|
|
pub struct CounterToken(Arc<&'static str>);
|
|
|
|
|
|
|
|
impl Clone for CounterToken {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
// new_count = strong_count
|
|
|
|
// - 1 (in TokenCounter)
|
|
|
|
// + 1 (token that's being created)
|
|
|
|
datapoint_info!(*self.0, ("count", Arc::strong_count(&self.0), i64));
|
|
|
|
CounterToken(self.0.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for CounterToken {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
// new_count = strong_count
|
|
|
|
// - 1 (in TokenCounter, if it still exists)
|
|
|
|
// - 1 (token that's being dropped)
|
|
|
|
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)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|