From 876709fbe9e724f8a27f7875faa8a103fd1d6046 Mon Sep 17 00:00:00 2001 From: sqwishy Date: Fri, 12 Aug 2022 01:44:41 -0700 Subject: [PATCH] make initialize_tracing sync and infallable (#395) also remove tiny bit of raceyness from worker tests, there's a bit of sync around Once but it's short so it should be fine --- backend/src/lib.rs | 6 ++---- backend/src/main.rs | 2 +- backend/src/tracing_init.rs | 7 +++---- backend/src/worker.rs | 16 +++------------- 4 files changed, 9 insertions(+), 22 deletions(-) diff --git a/backend/src/lib.rs b/backend/src/lib.rs index bcb3ae0975..db8bc3d651 100644 --- a/backend/src/lib.rs +++ b/backend/src/lib.rs @@ -59,6 +59,8 @@ use crate::{ utils::rd_string, }; +pub use crate::tracing_init::initialize_tracing; + const GIT_VERSION: &str = git_version!(args = ["--tag", "--always"], fallback = "unknown-version"); pub const DEFAULT_NUM_WORKERS: usize = 3; pub const DEFAULT_TIMEOUT: i32 = 300; @@ -85,10 +87,6 @@ pub async fn connect_db() -> anyhow::Result { Ok(db::connect(&database_url, max_connections).await?) } -pub async fn initialize_tracing() -> anyhow::Result<()> { - tracing_init::initialize_tracing().await -} - struct BaseUrl(String); struct CloudHosted(bool); diff --git a/backend/src/main.rs b/backend/src/main.rs index bef233fd05..eabaee2c6f 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -14,7 +14,7 @@ use dotenv::dotenv; async fn main() -> anyhow::Result<()> { dotenv().ok(); - windmill::initialize_tracing().await?; + windmill::initialize_tracing(); let db = windmill::connect_db().await?; diff --git a/backend/src/tracing_init.rs b/backend/src/tracing_init.rs index c5ec916fff..82fedea45a 100644 --- a/backend/src/tracing_init.rs +++ b/backend/src/tracing_init.rs @@ -58,7 +58,7 @@ fn filter_metadata(meta: &Metadata) -> bool { meta.target().starts_with("windmill") } -pub async fn initialize_tracing() -> anyhow::Result<()> { +pub fn initialize_tracing() { let tokio_console = std::env::var("TOKIO_CONSOLE") .map(|x| x == "true") .unwrap_or(false); @@ -70,8 +70,8 @@ pub async fn initialize_tracing() -> anyhow::Result<()> { let nenv_filter = if tokio_console { env_filter - .add_directive("runtime=trace".parse()?) - .add_directive("tokio=trace".parse()?) + .add_directive("runtime=trace".parse().unwrap()) + .add_directive("tokio=trace".parse().unwrap()) } else { env_filter }; @@ -93,5 +93,4 @@ pub async fn initialize_tracing() -> anyhow::Result<()> { .with(compact_layer().with_filter(filter_fn(filter_metadata))) .init(), } - Ok(()) } diff --git a/backend/src/worker.rs b/backend/src/worker.rs index 785695f4cb..f5bb70704e 100644 --- a/backend/src/worker.rs +++ b/backend/src/worker.rs @@ -1202,20 +1202,10 @@ mod tests { use super::*; async fn initialize_tracing() { - use std::sync::atomic::Ordering::SeqCst; + use std::sync::Once; - // TODO a bit of a race condition here. - // Only one test calls tracing_init::initialize_tracing() (good) but it may not have - // finished & returned while other tests return from this function and start doing things. - - static IS_INIT: AtomicBool = AtomicBool::new(false); - - if IS_INIT - .compare_exchange(false, true, SeqCst, SeqCst) - .is_ok() - { - crate::tracing_init::initialize_tracing().await.unwrap(); - } + static ONCE: Once = Once::new(); + ONCE.call_once(|| crate::tracing_init::initialize_tracing()); } /// it's important this is unique between tests as there is one prometheus registry and