use once_cell::sync::OnceCell;
use parking_lot::RwLock;
#[derive(Clone, Default)]
pub struct MoniOFGlobalConfig {
pub log_each_db_event: bool,
pub slow_db_threshold_ms: Option<u64>,
pub low_db_threshold_ms: Option<u64>,
pub slack_webhook: Option<String>,
}
static GLOBAL: OnceCell<RwLock<MoniOFGlobalConfig>> = OnceCell::new();
pub fn initiate(cfg: MoniOFGlobalConfig) {
use tracing_subscriber::{fmt, EnvFilter, prelude::*};
let base = EnvFilter::from_default_env();
let filter = base
.add_directive("moniof=debug".parse().unwrap_or_else(|_| "debug".parse().unwrap()))
.add_directive("moniof::mongo=debug".parse().unwrap_or_else(|_| "debug".parse().unwrap()))
.add_directive("moniof::sql=debug".parse().unwrap_or_else(|_| "debug".parse().unwrap()))
.add_directive("moniof::of=debug".parse().unwrap_or_else(|_| "debug".parse().unwrap()))
.add_directive("sqlx=info".parse().unwrap_or_else(|_| "info".parse().unwrap()));
let fmt_layer = fmt::layer().with_target(true);
#[cfg(feature = "sqlx")]
{
use crate::instrumentation::sql_events::MOFSqlEvents;
let subscriber = tracing_subscriber::registry()
.with(filter)
.with(fmt_layer)
.with(MOFSqlEvents::new());
let _ = subscriber.try_init();
}
#[cfg(not(feature = "sqlx"))]
{
let subscriber = tracing_subscriber::registry()
.with(filter)
.with(fmt_layer);
let _ = subscriber.try_init();
}
let cell = GLOBAL.get_or_init(|| RwLock::new(MoniOFGlobalConfig::default()));
*cell.write() = cfg;
tracing::info!(target = "moniof", "moniof global initiated (SQL logging enabled)");
}
pub fn global() -> MoniOFGlobalConfig {
GLOBAL
.get()
.map(|g| g.read().clone())
.unwrap_or_default()
}