Skip to content

Commit

Permalink
Flush metrics every 8192 increments
Browse files Browse the repository at this point in the history
  • Loading branch information
akoshelev committed Dec 26, 2024
1 parent 3413c3c commit d8b599d
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion ipa-metrics/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,20 @@ thread_local! {
pub(crate) static METRICS_CTX: RefCell<MetricsContext> = const { RefCell::new(MetricsContext::new()) }
}

thread_local! {
pub(crate) static INCREMENTS: RefCell<usize> = const { RefCell::new(0) }
}

// const INCREMENTS_BEFORE_FLUSH: usize = 1024;

#[macro_export]
macro_rules! counter {
($metric:expr, $val:expr $(, $l:expr => $v:expr)*) => {{
let name = $crate::metric_name!($metric $(, $l => $v)*);
$crate::MetricsCurrentThreadContext::store_mut(|store| store.counter(&name).inc($val))
let r = $crate::MetricsCurrentThreadContext::store_mut(|store| store.counter(&name).inc($val));
$crate::MetricsCurrentThreadContext::maybe_flush();

r
}};
($metric:expr $(, $l:expr => $v:expr)*) => {{
$crate::metric_name!($metric $(, $l => $v)*)
Expand Down Expand Up @@ -44,6 +53,19 @@ impl CurrentThreadContext {
pub fn is_connected() -> bool {
METRICS_CTX.with_borrow(|ctx| ctx.tx.is_some())
}

pub fn maybe_flush() {
const INCREMENTS_BEFORE_FLUSH: usize = 8192;

let new_val = INCREMENTS.with_borrow_mut(|v| {
*v += 1;
*v
});

if new_val % INCREMENTS_BEFORE_FLUSH == 0 && Self::is_connected() {
Self::flush();
}
}
}

/// This context is used inside thread-local storage,
Expand Down

0 comments on commit d8b599d

Please sign in to comment.