diff --git a/vmm/common/Cargo.toml b/vmm/common/Cargo.toml index 674d8ee7..35f9949d 100644 --- a/vmm/common/Cargo.toml +++ b/vmm/common/Cargo.toml @@ -18,6 +18,8 @@ ttrpc = { version = "0.7", features = ["async"] } protobuf = "3.2" async-trait = "0.1" regex = "1.5.6" +futures = { version = "0.3.21" } +signal-hook-tokio = { version = "0.3.1", features = ["futures-v0_3"] } tracing = "0.1.40" tracing-opentelemetry = "0.21.0" @@ -26,8 +28,6 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } opentelemetry = { version = "0.20.0", features = ["rt-tokio"] } opentelemetry-otlp = "0.13.0" -signal-hook = "0.3.1" - [build-dependencies] ttrpc-codegen = { git = "https://github.com/kuasar-io/ttrpc-rust.git", branch = "v0.7.1-kuasar" } tonic-build = "0.7.2" diff --git a/vmm/common/src/signal.rs b/vmm/common/src/signal.rs index 4d56be3d..7ec0f4e8 100644 --- a/vmm/common/src/signal.rs +++ b/vmm/common/src/signal.rs @@ -14,16 +14,19 @@ See the License for the specific language governing permissions and limitations under the License. */ -use nix::libc::{SIGINT, SIGTERM, SIGUSR1}; -use signal_hook::iterator::Signals; +use futures::StreamExt; +use nix::libc; +use signal_hook_tokio::Signals; use crate::trace; pub async fn handle_signals(log_level: &str, otlp_service_name: &str) { - let mut signals = Signals::new([SIGTERM, SIGINT, SIGUSR1]).expect("new signal failed"); + let mut signals = Signals::new([libc::SIGUSR1]) + .expect("new signal failed") + .fuse(); - for sig in signals.forever() { - if sig == SIGUSR1 { + while let Some(sig) = signals.next().await { + if sig == libc::SIGUSR1 { trace::set_enabled(!trace::is_enabled()); let _ = trace::setup_tracing(log_level, otlp_service_name); } diff --git a/vmm/common/src/trace.rs b/vmm/common/src/trace.rs index 329283f7..4c200049 100644 --- a/vmm/common/src/trace.rs +++ b/vmm/common/src/trace.rs @@ -30,15 +30,15 @@ use tracing_subscriber::{ }; lazy_static! { - static ref ENABLED: AtomicBool = AtomicBool::new(false); + static ref TRACE_ENABLED: AtomicBool = AtomicBool::new(false); } pub fn is_enabled() -> bool { - ENABLED.load(Ordering::Relaxed) + TRACE_ENABLED.load(Ordering::Relaxed) } pub fn set_enabled(enabled: bool) { - ENABLED.store(enabled, Ordering::Relaxed); + TRACE_ENABLED.store(enabled, Ordering::Relaxed); } pub fn setup_tracing(log_level: &str, otlp_service_name: &str) -> anyhow::Result<()> { @@ -46,12 +46,11 @@ pub fn setup_tracing(log_level: &str, otlp_service_name: &str) -> anyhow::Result .map_err(|e| anyhow!("failed to init logger filter: {}", e))?; let mut layers = vec![tracing_subscriber::fmt::layer().boxed()]; + // TODO: shutdown tracer provider when is_enabled is false if is_enabled() { let tracer = init_otlp_tracer(otlp_service_name) .map_err(|e| anyhow!("failed to init otlp tracer: {}", e))?; layers.push(tracing_opentelemetry::layer().with_tracer(tracer).boxed()); - } else { - shutdown_tracing(); } Registry::default() @@ -79,6 +78,8 @@ pub fn init_otlp_tracer(otlp_service_name: &str) -> anyhow::Result { Ok(tracer) } +// TODO: may hang indefinitely, use it again when https://github.com/open-telemetry/opentelemetry-rust/issues/868 is resolved +#[allow(dead_code)] pub fn shutdown_tracing() { global::shutdown_tracer_provider(); } diff --git a/vmm/sandbox/Cargo.lock b/vmm/sandbox/Cargo.lock index 782786c3..1f1b35ad 100644 --- a/vmm/sandbox/Cargo.lock +++ b/vmm/sandbox/Cargo.lock @@ -3032,6 +3032,7 @@ dependencies = [ "anyhow", "async-trait", "containerd-sandbox", + "futures", "lazy_static", "log", "nix 0.24.3", @@ -3040,7 +3041,7 @@ dependencies = [ "protobuf 3.2.0", "regex", "serde", - "signal-hook", + "signal-hook-tokio", "tonic-build", "tracing", "tracing-opentelemetry", diff --git a/vmm/task/Cargo.lock b/vmm/task/Cargo.lock index 9184a301..573d0a22 100644 --- a/vmm/task/Cargo.lock +++ b/vmm/task/Cargo.lock @@ -2488,6 +2488,7 @@ dependencies = [ "anyhow", "async-trait", "containerd-sandbox", + "futures", "lazy_static", "log", "nix 0.24.3", @@ -2496,7 +2497,7 @@ dependencies = [ "protobuf 3.2.0", "regex", "serde", - "signal-hook", + "signal-hook-tokio", "tonic-build", "tracing", "tracing-opentelemetry", diff --git a/vmm/task/src/main.rs b/vmm/task/src/main.rs index 01dab31a..f032bf8b 100644 --- a/vmm/task/src/main.rs +++ b/vmm/task/src/main.rs @@ -183,12 +183,11 @@ fn init_logger(log_level: &str) -> anyhow::Result<()> { .add_directive(format!("vmm_task={}", log_level).parse()?); let mut layers = vec![tracing_subscriber::fmt::layer().boxed()]; + // TODO: shutdown tracer provider when is_enabled is false if trace::is_enabled() { let tracer = trace::init_otlp_tracer("kuasar-vmm-task-service") .map_err(|e| anyhow!("failed to init otlp tracer: {}", e))?; layers.push(tracing_opentelemetry::layer().with_tracer(tracer).boxed()); - } else { - trace::shutdown_tracing(); } Registry::default() @@ -254,8 +253,6 @@ async fn handle_signals(signals: Signals, log_level: &str) { while let Some(sig) = signals.next().await { match sig { libc::SIGTERM | libc::SIGINT => { - trace::set_enabled(false); - trace::shutdown_tracing(); debug!("received {}", sig); } libc::SIGCHLD => loop {