Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions crates/core/src/logging/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use sink::log_level_filter;
pub(crate) use format::format_event_for_test;

static LOGGER_LIFECYCLE_LOCK: Mutex<()> = Mutex::new(());
static DEFAULT_LOGGING_RUNTIME: Mutex<Option<LoggingRuntime>> = Mutex::new(None);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

fn lock_logger_lifecycle() -> MutexGuard<'static, ()> {
LOGGER_LIFECYCLE_LOCK
Expand All @@ -44,6 +45,14 @@ fn log_crate_proxy_is_installed() -> bool {
std::ptr::addr_eq(log::logger(), spdlog::log_crate_proxy() as &dyn log::Log)
}

fn log_crate_proxy_has_receiver() -> bool {
let _lifecycle = lock_logger_lifecycle();
let receiver = spdlog::log_crate_proxy().swap_logger(None);
let has_receiver = receiver.is_some();
spdlog::log_crate_proxy().set_logger(receiver);
has_receiver
}

Comment thread
willkill07 marked this conversation as resolved.
Resolved
fn install_log_crate_proxy() -> Result<()> {
match spdlog::init_log_crate_proxy() {
Ok(()) => Ok(()),
Expand Down Expand Up @@ -170,6 +179,50 @@ pub fn init_logging(config: &LoggingConfig) -> Result<LoggingRuntime> {
LoggingRuntime::configure(config.clone())
}

/// Installs and retains the default process-wide logging runtime for a language binding.
///
/// Configuration is resolved from the supported logging environment variables, with built-in
/// defaults when none are present. Repeated initialization in the same linked runtime is a no-op.
#[doc(hidden)]
pub fn initialize_default_logging() -> Result<()> {
let mut runtime = DEFAULT_LOGGING_RUNTIME.lock().map_err(|error| {
FlowError::Internal(format!("default logging runtime lock poisoned: {error}"))
})?;
if runtime.is_none() {
let config = LoggingConfig::from_environment()?;
let uses_default_config = config.is_none();
if uses_default_config && log_crate_proxy_is_installed() && log_crate_proxy_has_receiver() {
return Ok(());
}
match LoggingRuntime::configure(config.unwrap_or_default()) {
Ok(configured) => *runtime = Some(configured),
// Language bindings initialize logging automatically. When Relay was not explicitly
// configured, defer to an application logger that already owns the process facade.
Err(FlowError::AlreadyExists(_)) if uses_default_config => {}
Err(error) => return Err(error),
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
Ok(())
}

/// Shuts down and releases the default process-wide logging runtime for a language binding.
///
/// Repeated shutdown in the same linked runtime is a no-op. The runtime is removed from shared
/// state before its sinks are drained so shutdown does not hold the default-runtime lock.
#[doc(hidden)]
pub fn shutdown_default_logging() -> Result<()> {
let runtime = DEFAULT_LOGGING_RUNTIME
.lock()
.map_err(|error| {
FlowError::Internal(format!("default logging runtime lock poisoned: {error}"))
})?
.take();
if let Some(runtime) = runtime {
runtime.shutdown();
}
Ok(())
}

#[cfg(test)]
#[path = "../../tests/coverage/logging_tests.rs"]
mod tests;
114 changes: 113 additions & 1 deletion crates/core/tests/coverage/logging_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::logging::{
FileLogRotationConfig, FileLogSinkConfig, LogFormat, LogLevel, LogSinkConfig, LoggingConfig,
LoggingRuntime, MAX_FILE_SINK_QUEUE_ENTRIES, MAX_FILE_SINK_RETAINED_FILES, build_logger,
format_event_for_test, init_logging,
format_event_for_test, init_logging, initialize_default_logging, shutdown_default_logging,
};
use opentelemetry::trace::{Span as _, Tracer as _, TracerProvider as _};
use opentelemetry_sdk::error::OTelSdkResult;
Expand Down Expand Up @@ -192,6 +192,104 @@ queue_capacity = 16
assert_eq!(record["fields"]["source"], "toml");
}

#[test]
fn default_logging_runtime_initializes_once_and_shuts_down_idempotently() {
let temp = tempfile::tempdir().unwrap();
let config_path = temp.path().join("logging.toml");
let log_path = temp.path().join("relay.log.jsonl");
std::fs::write(
&config_path,
format!(
r#"
[logging]
level = "info"
stderr_format = "human"
flush_interval_millis = 0

[[logging.sinks]]
path = {}
level = "info"
format = "jsonl"
queue_capacity = 16
"#,
toml_basic_string(log_path.to_string_lossy().as_ref())
),
)
.unwrap();
let _environment = LoggingEnvScope::set(&[
("NEMO_RELAY_LOG", None),
("NEMO_RELAY_LOG_STDERR_FORMAT", None),
("NEMO_RELAY_LOG_CONFIG_PATH", Some(config_path.as_os_str())),
]);

shutdown_default_logging().unwrap();
initialize_default_logging().unwrap();
initialize_default_logging().unwrap();
shutdown_default_logging().unwrap();
shutdown_default_logging().unwrap();

let records = std::fs::read_to_string(log_path)
.unwrap()
.lines()
.map(|line| serde_json::from_str::<Value>(line).expect("valid JSONL lifecycle record"))
.collect::<Vec<_>>();
assert_eq!(
records
.iter()
.filter(|record| record["event"] == "logging_initialized")
.count(),
1
);
assert_eq!(
records
.iter()
.filter(|record| record["event"] == "logging_shutdown_started")
.count(),
1
);
Comment thread
willkill07 marked this conversation as resolved.
Resolved
}

#[test]
fn implicit_default_logging_preserves_an_existing_relay_logger() {
let _environment = LoggingEnvScope::set(&[
("NEMO_RELAY_LOG", None),
("NEMO_RELAY_LOG_STDERR_FORMAT", None),
("NEMO_RELAY_LOG_CONFIG_PATH", None),
]);
shutdown_default_logging().unwrap();
let host_runtime = init_logging(&default_config()).unwrap();

initialize_default_logging().unwrap();
shutdown_default_logging().unwrap();

let receiver = spdlog::log_crate_proxy().swap_logger(None);
assert!(
receiver
.as_ref()
.is_some_and(|receiver| Arc::ptr_eq(receiver, &host_runtime.logger)),
"implicit binding startup must preserve the host Relay logger"
);
spdlog::log_crate_proxy().set_logger(receiver);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
drop(host_runtime);
Comment thread
willkill07 marked this conversation as resolved.
Resolved
}

#[test]
fn default_logging_runtime_rejects_invalid_environment() {
let _environment = LoggingEnvScope::set(&[
("NEMO_RELAY_LOG", Some(OsStr::new(""))),
("NEMO_RELAY_LOG_STDERR_FORMAT", None),
("NEMO_RELAY_LOG_CONFIG_PATH", None),
]);

shutdown_default_logging().unwrap();
let error = initialize_default_logging().unwrap_err().to_string();

assert!(
error.contains("NEMO_RELAY_LOG must not be empty"),
"{error}"
);
}

#[test]
fn logging_config_from_environment_resolves_direct_settings() {
let _environment = LoggingEnvScope::set(&[
Expand Down Expand Up @@ -1581,6 +1679,17 @@ fn configure_rejects_preinstalled_foreign_logger() {
.contains("process-global log facade is already initialized by another logger"),
"{error}"
);
initialize_default_logging()
.expect("unconfigured default logging should defer to the foreign logger");
unsafe { std::env::set_var("NEMO_RELAY_LOG", "info") };
let error = initialize_default_logging()
.expect_err("explicit default logging should reject the foreign logger");
assert!(
error
.to_string()
.contains("process-global log facade is already initialized by another logger"),
"{error}"
);
return;
}

Expand All @@ -1589,6 +1698,9 @@ fn configure_rejects_preinstalled_foreign_logger() {
let output = std::process::Command::new(std::env::current_exe().unwrap())
.args(["--exact", test_name, "--nocapture"])
.env(FOREIGN_LOGGER_CHILD_ENV, "1")
.env_remove("NEMO_RELAY_LOG")
.env_remove("NEMO_RELAY_LOG_STDERR_FORMAT")
.env_remove("NEMO_RELAY_LOG_CONFIG_PATH")
.output()
.expect("foreign logger child test should start");

Expand Down
17 changes: 17 additions & 0 deletions crates/ffi/nemo_relay.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,23 @@ typedef char *(*NemoRelayToolExecInterceptCb)(void *user_data,
*/
typedef char *(*NemoRelayToolExecCb)(void *user_data, const char *args_json);

/**
* Initializes the Go binding runtime and installs default operational logging.
*
* Logging configuration is resolved from `NEMO_RELAY_LOG`,
* `NEMO_RELAY_LOG_STDERR_FORMAT`, or `NEMO_RELAY_LOG_CONFIG_PATH`, with built-in defaults when
* none are set. Repeated initialization is a no-op.
*/
NemoRelayStatus nemo_relay_initialize_default_logging(void);

/**
* Shuts down and releases the default operational logging runtime.
*
* Pending file-sink records are drained before this function returns. Repeated shutdown is a
* no-op.
*/
NemoRelayStatus nemo_relay_shutdown_default_logging(void);

/**
* Run the registered tool request intercept chain on the given arguments.
*
Expand Down
29 changes: 29 additions & 0 deletions crates/ffi/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,35 @@ fn tokio_runtime() -> &'static Runtime {
})
}

/// Initializes the Go binding runtime and installs default operational logging.
///
/// Logging configuration is resolved from `NEMO_RELAY_LOG`,
/// `NEMO_RELAY_LOG_STDERR_FORMAT`, or `NEMO_RELAY_LOG_CONFIG_PATH`, with built-in defaults when
/// none are set. Repeated initialization is a no-op.
#[unsafe(no_mangle)]
pub extern "C" fn nemo_relay_initialize_default_logging() -> NemoRelayStatus {
clear_last_error();
let result = nemo_relay::shared_runtime::initialize_shared_runtime_binding("go")
.and_then(|()| nemo_relay::logging::initialize_default_logging());
match result {
Ok(()) => NemoRelayStatus::Ok,
Err(error) => status_from_error(&error),
}
}

/// Shuts down and releases the default operational logging runtime.
///
/// Pending file-sink records are drained before this function returns. Repeated shutdown is a
/// no-op.
#[unsafe(no_mangle)]
pub extern "C" fn nemo_relay_shutdown_default_logging() -> NemoRelayStatus {
clear_last_error();
match nemo_relay::logging::shutdown_default_logging() {
Ok(()) => NemoRelayStatus::Ok,
Err(error) => status_from_error(&error),
}
}

fn block_on_sync_ffi<T, F>(future: F) -> FlowResult<T>
where
T: Send,
Expand Down
13 changes: 13 additions & 0 deletions crates/ffi/tests/unit/api_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,19 @@ unsafe fn fresh_scope_stack() -> *mut FfiScopeStack {
stack
}

#[test]
fn default_logging_shutdown_is_idempotent() {
let _guard = lock_unpoisoned(&TEST_MUTEX);
assert_status!(
api::nemo_relay_shutdown_default_logging(),
NemoRelayStatus::Ok
);
assert_status!(
api::nemo_relay_shutdown_default_logging(),
NemoRelayStatus::Ok
);
}

#[test]
fn propagation_context_json_round_trips_through_the_ffi() {
let _guard = lock_unpoisoned(&TEST_MUTEX);
Expand Down
32 changes: 30 additions & 2 deletions crates/node/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::pin::Pin;
use std::ptr;
use std::sync::Arc;
use std::sync::Mutex as StdMutex;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
use std::task::{Context, Poll};

use chrono::{DateTime, Utc};
Expand Down Expand Up @@ -88,6 +88,29 @@ use crate::promise_call::with_publication_callback_context;
use crate::stream::LlmStream;
use crate::types::{LlmHandle, ScopeHandle, ScopeStack, ScopeType, ToolHandle};

static NODE_ENVIRONMENT_COUNT: AtomicUsize = AtomicUsize::new(0);
static NODE_ENVIRONMENT_LIFECYCLE_LOCK: StdMutex<()> = StdMutex::new(());

fn register_node_environment() -> FlowResult<()> {
let _guard = NODE_ENVIRONMENT_LIFECYCLE_LOCK
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
nemo_relay::logging::initialize_default_logging()?;
NODE_ENVIRONMENT_COUNT.fetch_add(1, Ordering::AcqRel);
Ok(())
}

fn cleanup_node_environment() {
let _guard = NODE_ENVIRONMENT_LIFECYCLE_LOCK
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
if NODE_ENVIRONMENT_COUNT.fetch_sub(1, Ordering::AcqRel) == 1
&& let Err(error) = nemo_relay::logging::shutdown_default_logging()
{
eprintln!("nemo-relay: operational logging shutdown failed: {error}");
}
}

fn effective_scope_context(
env: &Env,
) -> napi::Result<(
Expand Down Expand Up @@ -127,7 +150,12 @@ fn init() {

#[cfg(not(test))]
#[napi_derive::module_exports]
fn install_well_known_symbol_methods(exports: JsObject, env: Env) -> napi::Result<()> {
fn install_well_known_symbol_methods(exports: JsObject, mut env: Env) -> napi::Result<()> {
register_node_environment().map_err(to_napi_err)?;
if let Err(error) = env.add_env_cleanup_hook((), |_| cleanup_node_environment()) {
cleanup_node_environment();
return Err(error);
}
let activation: JsFunction = exports.get_named_property("DynamicPluginActivation")?;
let activation = activation.coerce_to_object()?;
let mut prototype: JsObject = activation.get_named_property("prototype")?;
Expand Down
Loading
Loading