use std::cell::Cell;
use std::sync::Once;
thread_local! {
static RETRY_EVENTS: Cell<usize> = const { Cell::new(0) };
}
struct Counter;
impl tracing::Subscriber for Counter {
fn enabled(&self, meta: &tracing::Metadata<'_>) -> bool {
meta.target() == "mettle::retry"
}
fn event(&self, _event: &tracing::Event<'_>) {
RETRY_EVENTS.with(|c| c.set(c.get() + 1));
}
fn new_span(&self, _: &tracing::span::Attributes<'_>) -> tracing::span::Id {
tracing::span::Id::from_u64(1)
}
fn record(&self, _: &tracing::span::Id, _: &tracing::span::Record<'_>) {}
fn record_follows_from(&self, _: &tracing::span::Id, _: &tracing::span::Id) {}
fn enter(&self, _: &tracing::span::Id) {}
fn exit(&self, _: &tracing::span::Id) {}
}
pub(crate) fn count_retry_events() -> RetryEventCount {
static INIT: Once = Once::new();
INIT.call_once(|| {
let _ = tracing::subscriber::set_global_default(Counter);
});
tracing::callsite::rebuild_interest_cache();
RETRY_EVENTS.with(|c| c.set(0));
RetryEventCount
}
pub(crate) struct RetryEventCount;
impl RetryEventCount {
pub(crate) fn get(&self) -> usize {
RETRY_EVENTS.with(|c| c.get())
}
}