From 64edb978cd85bfff5ea835cb6320f5f4dca4414c Mon Sep 17 00:00:00 2001 From: Douwe Osinga Date: Mon, 18 May 2026 09:58:05 -0400 Subject: [PATCH] Flush OTLP traces reliably on exit with configurable timeout (#9228) Signed-off-by: Douwe Osinga Co-authored-by: Douwe Osinga --- crates/goose-cli/src/main.rs | 1 - crates/goose-server/src/commands/agent.rs | 1 - crates/goose/src/otel/otlp.rs | 19 +++++++++++++++---- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/crates/goose-cli/src/main.rs b/crates/goose-cli/src/main.rs index f3d2fffab5..afecc21c82 100644 --- a/crates/goose-cli/src/main.rs +++ b/crates/goose-cli/src/main.rs @@ -23,7 +23,6 @@ async fn run() -> Result<()> { #[cfg(feature = "otel")] if goose::otel::otlp::is_otlp_initialized() { - tokio::time::sleep(tokio::time::Duration::from_millis(100)).await; goose::otel::otlp::shutdown_otlp(); } diff --git a/crates/goose-server/src/commands/agent.rs b/crates/goose-server/src/commands/agent.rs index 369efe629b..ef9be3ac70 100644 --- a/crates/goose-server/src/commands/agent.rs +++ b/crates/goose-server/src/commands/agent.rs @@ -158,7 +158,6 @@ pub async fn run() -> Result<()> { #[cfg(feature = "otel")] if goose::otel::otlp::is_otlp_initialized() { - tokio::time::sleep(tokio::time::Duration::from_millis(100)).await; goose::otel::otlp::shutdown_otlp(); } diff --git a/crates/goose/src/otel/otlp.rs b/crates/goose/src/otel/otlp.rs index 67d6c8bbcf..234617941e 100644 --- a/crates/goose/src/otel/otlp.rs +++ b/crates/goose/src/otel/otlp.rs @@ -359,28 +359,39 @@ fn create_otlp_logs_filter() -> FilterFn) -> bool> { }) } -/// Shutdown OTLP providers gracefully pub fn shutdown_otlp() { + let timeout = std::time::Duration::from_millis( + crate::config::Config::global() + .get_param::("otel_shutdown_timeout_ms") + .unwrap_or(5000), + ); + if let Some(provider) = TRACER_PROVIDER .lock() .unwrap_or_else(|e| e.into_inner()) .take() { - let _ = provider.shutdown(); + if let Err(e) = provider.shutdown_with_timeout(timeout) { + tracing::warn!("OTLP tracer provider shutdown error: {e}"); + } } if let Some(provider) = METER_PROVIDER .lock() .unwrap_or_else(|e| e.into_inner()) .take() { - let _ = provider.shutdown(); + if let Err(e) = provider.shutdown_with_timeout(timeout) { + tracing::warn!("OTLP meter provider shutdown error: {e}"); + } } if let Some(provider) = LOGGER_PROVIDER .lock() .unwrap_or_else(|e| e.into_inner()) .take() { - let _ = provider.shutdown(); + if let Err(e) = provider.shutdown_with_timeout(timeout) { + tracing::warn!("OTLP logger provider shutdown error: {e}"); + } } }