use std::cell::{Cell, RefCell};
use chrono::{DateTime, Utc};
use super::types::BufferedEmit;
tokio::task_local! {
pub(super) static EMIT_BUFFER: RefCell<Vec<BufferedEmit>>;
}
thread_local! {
pub(super) static EMIT_TS: Cell<Option<DateTime<Utc>>> = const { Cell::new(None) };
pub(super) static REPLAYING: Cell<bool> = const { Cell::new(false) };
}
pub(super) fn env_flag_default_on(key: &str) -> bool {
match std::env::var(key).as_deref() {
Ok("0") | Ok("false") | Ok("FALSE") | Ok("no") | Ok("NO") => false,
_ => true,
}
}
pub fn request_enabled() -> bool {
env_flag_default_on("SPECTRA_REQUEST_BUFFER")
}
pub fn job_enabled() -> bool {
env_flag_default_on("SPECTRA_JOB_BUFFER")
}
pub fn aggregate_enabled() -> bool {
env_flag_default_on("SPECTRA_COUNTER_AGGREGATE")
}
pub fn current_emit_ts() -> chrono::DateTime<Utc> {
EMIT_TS.with(|c| c.get()).unwrap_or_else(Utc::now)
}
pub fn is_active() -> bool {
EMIT_BUFFER.try_with(|_| ()).is_ok()
}
pub fn is_replaying() -> bool {
REPLAYING.with(|r| r.get())
}