diff --git a/crates/containerd-shim-wasm/Cargo.toml b/crates/containerd-shim-wasm/Cargo.toml index 05d366ebd..5c9c4ec29 100644 --- a/crates/containerd-shim-wasm/Cargo.toml +++ b/crates/containerd-shim-wasm/Cargo.toml @@ -36,7 +36,9 @@ wasmparser = "0.207.0" tokio-stream = { version = "0.1" } prost-types = "0.12" # should match version in containerd-shim sha256 = { workspace = true } -tracing = { workspace = true } + +# tracing +tracing = { workspace = true, optional = true } [target.'cfg(unix)'.dependencies] caps = "0.5" @@ -61,3 +63,4 @@ rand= "0.8" [features] testing = ["dep:containerd-shim-wasm-test-modules", "dep:env_logger", "dep:tempfile", "dep:oci-tar-builder"] +tracing = ["dep:tracing"] diff --git a/crates/containerd-shim-wasm/src/sandbox/cli.rs b/crates/containerd-shim-wasm/src/sandbox/cli.rs index 0fa51eb13..cf2342940 100644 --- a/crates/containerd-shim-wasm/src/sandbox/cli.rs +++ b/crates/containerd-shim-wasm/src/sandbox/cli.rs @@ -3,7 +3,6 @@ use std::sync::mpsc::channel; use std::sync::Arc; use containerd_shim::{parse, run, Config}; -use tracing::{instrument, Span}; use ttrpc::Server; use crate::sandbox::manager::Shim; @@ -37,7 +36,7 @@ macro_rules! revision { }; } -#[instrument(skip_all, parent = Span::current(), level= "Info")] +#[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] pub fn shim_main<'a, I>( name: &str, version: &str, diff --git a/crates/containerd-shim-wasm/src/sandbox/containerd/client.rs b/crates/containerd-shim-wasm/src/sandbox/containerd/client.rs index a6d74e5bf..c1a8942c4 100644 --- a/crates/containerd-shim-wasm/src/sandbox/containerd/client.rs +++ b/crates/containerd-shim-wasm/src/sandbox/containerd/client.rs @@ -24,7 +24,6 @@ use tokio::runtime::Runtime; use tokio::sync::mpsc; use tokio_stream::wrappers::ReceiverStream; use tonic::{Code, Request}; -use tracing::{instrument, Span}; use super::lease::LeaseGuard; use crate::container::Engine; @@ -54,7 +53,7 @@ pub(crate) struct WriteContent { // sync wrapper implementation from https://tokio.rs/tokio/topics/bridging impl Client { // wrapper around connection that will establish a connection and create a client - #[instrument(skip_all, parent = Span::current(), level = "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] pub fn connect( address: impl AsRef + ToString, namespace: impl ToString, @@ -76,7 +75,7 @@ impl Client { } // wrapper around read that will read the entire content file - #[instrument(skip_all, parent = Span::current(), level = "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn read_content(&self, digest: impl ToString) -> Result> { self.rt.block_on(async { let req = ReadContentRequest { @@ -98,7 +97,7 @@ impl Client { // used in tests to clean up content #[allow(dead_code)] - #[instrument(skip_all, parent = Span::current(), level = "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn delete_content(&self, digest: impl ToString) -> Result<()> { self.rt.block_on(async { let req = DeleteContentRequest { @@ -114,7 +113,7 @@ impl Client { } // wrapper around lease that will create a lease and return a guard that will delete the lease when dropped - #[instrument(skip_all, parent = Span::current(), level = "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn lease(&self, reference: String) -> Result { self.rt.block_on(async { let mut lease_labels = HashMap::new(); @@ -146,7 +145,7 @@ impl Client { }) } - #[instrument(skip_all, parent = Span::current(), level = "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn save_content( &self, data: Vec, @@ -272,7 +271,7 @@ impl Client { }) } - #[instrument(skip_all, parent = Span::current(), level = "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn get_info(&self, content_digest: &str) -> Result { self.rt.block_on(async { let req = InfoRequest { @@ -295,7 +294,7 @@ impl Client { }) } - #[instrument(skip_all, parent = Span::current(), level = "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn update_info(&self, info: Info) -> Result { self.rt.block_on(async { let req = UpdateRequest { @@ -321,7 +320,7 @@ impl Client { }) } - #[instrument(skip_all, parent = Span::current(), level = "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn get_image(&self, image_name: impl ToString) -> Result { self.rt.block_on(async { let name = image_name.to_string(); @@ -343,7 +342,7 @@ impl Client { }) } - #[instrument(skip_all, parent = Span::current(), level = "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn extract_image_content_sha(&self, image: &Image) -> Result { let digest = image .target @@ -359,7 +358,7 @@ impl Client { Ok(digest) } - #[instrument(skip_all, parent = Span::current(), level = "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn get_container(&self, container_name: impl ToString) -> Result { self.rt.block_on(async { let id = container_name.to_string(); @@ -381,7 +380,7 @@ impl Client { }) } - #[instrument(skip_all, parent = Span::current(), level = "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn get_image_manifest_and_digest(&self, image_name: &str) -> Result<(ImageManifest, String)> { let image = self.get_image(image_name)?; let image_digest = self.extract_image_content_sha(&image)?; @@ -392,7 +391,7 @@ impl Client { // load module will query the containerd store to find an image that has an OS of type 'wasm' // If found it continues to parse the manifest and return the layers that contains the WASM modules // and possibly other configuration layers. - #[instrument(skip_all, parent = Span::current(), level = "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] pub fn load_modules( &self, containerd_id: impl ToString, @@ -523,7 +522,7 @@ impl Client { Ok((layers, platform)) } - #[instrument(skip_all, parent = Span::current(), level = "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn read_wasm_layer( &self, original_config: &oci_spec::image::Descriptor, diff --git a/crates/containerd-shim-wasm/src/sandbox/instance.rs b/crates/containerd-shim-wasm/src/sandbox/instance.rs index 32498be54..7321ba190 100644 --- a/crates/containerd-shim-wasm/src/sandbox/instance.rs +++ b/crates/containerd-shim-wasm/src/sandbox/instance.rs @@ -4,7 +4,6 @@ use std::path::{Path, PathBuf}; use std::time::Duration; use chrono::{DateTime, Utc}; -use tracing::{instrument, Span}; use super::error::Error; use super::sync::WaitableCell; @@ -137,7 +136,7 @@ pub trait Instance: 'static { /// Waits for the instance to finish and retunrs its exit code /// This is a blocking call. - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn wait(&self) -> (u32, DateTime) { self.wait_timeout(None).unwrap() } diff --git a/crates/containerd-shim-wasm/src/sandbox/instance_utils.rs b/crates/containerd-shim-wasm/src/sandbox/instance_utils.rs index 633bed6b4..1a5617dd2 100644 --- a/crates/containerd-shim-wasm/src/sandbox/instance_utils.rs +++ b/crates/containerd-shim-wasm/src/sandbox/instance_utils.rs @@ -5,14 +5,13 @@ use std::path::{Path, PathBuf}; use anyhow::{bail, Context, Result}; use serde::{Deserialize, Serialize}; -use tracing::{instrument, Span}; use super::Error; /// Return the root path for the instance. /// /// The root path is the path to the directory containing the container's state. -#[instrument(skip_all, parent = Span::current(), level= "Info")] +#[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] pub fn get_instance_root>( root_path: P, instance_id: &str, @@ -27,7 +26,7 @@ pub fn get_instance_root>( /// Checks if the container exists. /// /// The root path is the path to the directory containing the container's state. -#[instrument(skip_all, parent = Span::current(), level= "Info")] +#[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] pub fn instance_exists>(root_path: P, container_id: &str) -> Result { let instance_root = construct_instance_root(root_path, container_id)?; Ok(instance_root.exists()) @@ -38,7 +37,7 @@ struct Options { root: Option, } -#[instrument(skip_all, parent = Span::current(), level= "Info")] +#[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] pub fn determine_rootdir( bundle: impl AsRef, namespace: &str, @@ -57,7 +56,7 @@ pub fn determine_rootdir( Ok(path) } -#[instrument(skip_all, parent = Span::current(), level= "Info")] +#[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn construct_instance_root>(root_path: P, container_id: &str) -> Result { let root_path = root_path.as_ref().canonicalize().with_context(|| { format!( diff --git a/crates/containerd-shim-wasm/src/sandbox/shim/cli.rs b/crates/containerd-shim-wasm/src/sandbox/shim/cli.rs index 0486cb4d8..706e0b677 100644 --- a/crates/containerd-shim-wasm/src/sandbox/shim/cli.rs +++ b/crates/containerd-shim-wasm/src/sandbox/shim/cli.rs @@ -9,7 +9,6 @@ use containerd_shim::util::write_address; use containerd_shim::{self as shim, api, ExitSignal}; use oci_spec::runtime::Spec; use shim::Flags; -use tracing::{instrument, Span}; use crate::sandbox::instance::Instance; use crate::sandbox::shim::events::{RemoteEventSender, ToTimestamp}; @@ -46,7 +45,7 @@ where { type T = Local; - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn new(_runtime_id: &str, args: &Flags, _config: &mut shim::Config) -> Self { Cli { engine: Default::default(), @@ -57,7 +56,7 @@ where } } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn start_shim(&mut self, opts: containerd_shim::StartOpts) -> shim::Result { let dir = current_dir().map_err(|err| ShimError::Other(err.to_string()))?; let spec = Spec::load(dir.join("config.json")).map_err(|err| { @@ -81,12 +80,12 @@ where Ok(address) } - #[instrument(skip_all, parent = Span::current(), level = "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn wait(&mut self) { self.exit.wait(); } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn create_task_service(&self, publisher: RemotePublisher) -> Self::T { let events = RemoteEventSender::new(&self.namespace, publisher); let exit = self.exit.clone(); @@ -100,7 +99,7 @@ where ) } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn delete_shim(&mut self) -> shim::Result { Ok(api::DeleteResponse { exit_status: 137, diff --git a/crates/containerd-shim-wasm/src/sandbox/shim/instance_data.rs b/crates/containerd-shim-wasm/src/sandbox/shim/instance_data.rs index bf02618e1..537fda6b9 100644 --- a/crates/containerd-shim-wasm/src/sandbox/shim/instance_data.rs +++ b/crates/containerd-shim-wasm/src/sandbox/shim/instance_data.rs @@ -2,7 +2,6 @@ use std::sync::{Arc, OnceLock, RwLock}; use std::time::Duration; use chrono::{DateTime, Utc}; -use tracing::{instrument, Span}; use crate::sandbox::instance::Nop; use crate::sandbox::shim::instance_option::InstanceOption; @@ -17,7 +16,7 @@ pub(super) struct InstanceData { } impl InstanceData { - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] pub fn new_instance(id: impl AsRef, cfg: InstanceConfig) -> Result { let id = id.as_ref().to_string(); let instance = InstanceOption::Instance(T::new(id, Some(&cfg))?); @@ -29,7 +28,7 @@ impl InstanceData { }) } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] pub fn new_base(id: impl AsRef, cfg: InstanceConfig) -> Result { let id = id.as_ref().to_string(); let instance = InstanceOption::Nop(Nop::new(id, None)?); @@ -41,17 +40,17 @@ impl InstanceData { }) } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] pub fn pid(&self) -> Option { self.pid.get().copied() } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] pub fn config(&self) -> &InstanceConfig { &self.cfg } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] pub fn start(&self) -> Result { let mut s = self.state.write().unwrap(); s.start()?; @@ -71,7 +70,7 @@ impl InstanceData { res } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] pub fn kill(&self, signal: u32) -> Result<()> { let mut s = self.state.write().unwrap(); s.kill()?; @@ -79,7 +78,7 @@ impl InstanceData { self.instance.kill(signal) } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] pub fn delete(&self) -> Result<()> { let mut s = self.state.write().unwrap(); s.delete()?; @@ -94,7 +93,7 @@ impl InstanceData { res } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] pub fn wait(&self) -> (u32, DateTime) { let res = self.instance.wait(); let mut s = self.state.write().unwrap(); @@ -102,7 +101,7 @@ impl InstanceData { res } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] pub fn wait_timeout(&self, t: impl Into>) -> Option<(u32, DateTime)> { let res = self.instance.wait_timeout(t); if res.is_some() { diff --git a/crates/containerd-shim-wasm/src/sandbox/shim/instance_option.rs b/crates/containerd-shim-wasm/src/sandbox/shim/instance_option.rs index c5f0ec093..da430fa0e 100644 --- a/crates/containerd-shim-wasm/src/sandbox/shim/instance_option.rs +++ b/crates/containerd-shim-wasm/src/sandbox/shim/instance_option.rs @@ -1,7 +1,6 @@ use std::time::Duration; use chrono::{DateTime, Utc}; -use tracing::{instrument, Span}; use crate::sandbox::instance::Nop; use crate::sandbox::{Instance, InstanceConfig, Result}; @@ -14,13 +13,13 @@ pub(super) enum InstanceOption { impl Instance for InstanceOption { type Engine = (); - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn new(_id: String, _cfg: Option<&InstanceConfig>) -> Result { // this is never called unimplemented!(); } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn start(&self) -> Result { match self { Self::Instance(i) => i.start(), @@ -28,7 +27,7 @@ impl Instance for InstanceOption { } } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn kill(&self, signal: u32) -> Result<()> { match self { Self::Instance(i) => i.kill(signal), @@ -36,7 +35,7 @@ impl Instance for InstanceOption { } } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn delete(&self) -> Result<()> { match self { Self::Instance(i) => i.delete(), @@ -44,7 +43,7 @@ impl Instance for InstanceOption { } } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn wait_timeout(&self, t: impl Into>) -> Option<(u32, DateTime)> { match self { Self::Instance(i) => i.wait_timeout(t), diff --git a/crates/containerd-shim-wasm/src/sandbox/shim/local.rs b/crates/containerd-shim-wasm/src/sandbox/shim/local.rs index 62cd529a8..71905afe1 100644 --- a/crates/containerd-shim-wasm/src/sandbox/shim/local.rs +++ b/crates/containerd-shim-wasm/src/sandbox/shim/local.rs @@ -21,7 +21,6 @@ use containerd_shim::util::IntoOption; use containerd_shim::{DeleteResponse, ExitSignal, TtrpcContext, TtrpcResult}; use log::debug; use oci_spec::runtime::Spec; -use tracing::{instrument, Span}; use crate::sandbox::instance::{Instance, InstanceConfig}; use crate::sandbox::shim::events::{EventSender, RemoteEventSender, ToTimestamp}; @@ -47,7 +46,7 @@ pub struct Local impl Local { /// Creates a new local task service. - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] pub fn new( engine: T::Engine, events: E, @@ -68,23 +67,23 @@ impl Local { } } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] pub(super) fn get_instance(&self, id: &str) -> Result>> { let instance = self.instances.read().unwrap().get(id).cloned(); instance.ok_or_else(|| Error::NotFound(id.to_string())) } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn has_instance(&self, id: &str) -> bool { self.instances.read().unwrap().contains_key(id) } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn is_empty(&self) -> bool { self.instances.read().unwrap().is_empty() } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn instance_config(&self) -> InstanceConfig { InstanceConfig::new( self.engine.clone(), @@ -94,7 +93,7 @@ impl Local { } } -#[instrument(skip_all, parent = Span::current(), level= "Info")] +#[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn is_cri_container(spec: &Spec) -> bool { spec.annotations() .as_ref() @@ -103,7 +102,7 @@ fn is_cri_container(spec: &Spec) -> bool { // These are the same functions as in Task, but without the TtrcpContext, which is useful for testing impl Local { - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn task_create(&self, req: CreateTaskRequest) -> Result { if !req.checkpoint().is_empty() || !req.parent_checkpoint().is_empty() { return Err(ShimError::Unimplemented("checkpoint is not supported".to_string()).into()); @@ -195,7 +194,7 @@ impl Local { }) } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn task_start(&self, req: StartRequest) -> Result { if req.exec_id().is_empty().not() { return Err(ShimError::Unimplemented("exec is not supported".to_string()).into()); @@ -238,7 +237,7 @@ impl Local { }) } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn task_kill(&self, req: KillRequest) -> Result { if !req.exec_id().is_empty() { return Err(Error::InvalidArgument("exec is not supported".to_string())); @@ -247,7 +246,7 @@ impl Local { Ok(Empty::new()) } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn task_delete(&self, req: DeleteRequest) -> Result { if !req.exec_id().is_empty() { return Err(Error::InvalidArgument("exec is not supported".to_string())); @@ -279,7 +278,7 @@ impl Local { }) } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn task_wait(&self, req: WaitRequest) -> Result { if !req.exec_id().is_empty() { return Err(Error::InvalidArgument("exec is not supported".to_string())); @@ -296,7 +295,7 @@ impl Local { }) } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn task_state(&self, req: StateRequest) -> Result { if !req.exec_id().is_empty() { return Err(Error::InvalidArgument("exec is not supported".to_string())); @@ -328,7 +327,7 @@ impl Local { }) } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn task_stats(&self, req: StatsRequest) -> Result { let i = self.get_instance(req.id())?; let pid = i @@ -346,7 +345,7 @@ impl Local { impl SandboxService for Local { type Instance = T; - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn new( namespace: String, containerd_address: String, @@ -361,37 +360,37 @@ impl SandboxService for Local { } impl Task for Local { - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn create(&self, _: &TtrpcContext, req: CreateTaskRequest) -> TtrpcResult { debug!("create: {:?}", req); Ok(self.task_create(req)?) } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn start(&self, _: &TtrpcContext, req: StartRequest) -> TtrpcResult { debug!("start: {:?}", req); Ok(self.task_start(req)?) } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn kill(&self, _: &TtrpcContext, req: KillRequest) -> TtrpcResult { debug!("kill: {:?}", req); Ok(self.task_kill(req)?) } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn delete(&self, _: &TtrpcContext, req: DeleteRequest) -> TtrpcResult { debug!("delete: {:?}", req); Ok(self.task_delete(req)?) } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn wait(&self, _: &TtrpcContext, req: WaitRequest) -> TtrpcResult { debug!("wait: {:?}", req); Ok(self.task_wait(req)?) } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn connect(&self, _: &TtrpcContext, req: ConnectRequest) -> TtrpcResult { debug!("connect: {:?}", req); let i = self.get_instance(req.id())?; @@ -404,13 +403,13 @@ impl Task for Local { }) } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn state(&self, _: &TtrpcContext, req: StateRequest) -> TtrpcResult { debug!("state: {:?}", req); Ok(self.task_state(req)?) } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn shutdown(&self, _: &TtrpcContext, _: ShutdownRequest) -> TtrpcResult { debug!("shutdown"); if self.is_empty() { @@ -419,7 +418,7 @@ impl Task for Local { Ok(Empty::new()) } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn stats(&self, _ctx: &TtrpcContext, req: StatsRequest) -> TtrpcResult { debug!("stats: {:?}", req); Ok(self.task_stats(req)?) diff --git a/crates/containerd-shim-wasm/src/sandbox/shim/task_state.rs b/crates/containerd-shim-wasm/src/sandbox/shim/task_state.rs index bc0608471..03e6bbf9f 100644 --- a/crates/containerd-shim-wasm/src/sandbox/shim/task_state.rs +++ b/crates/containerd-shim-wasm/src/sandbox/shim/task_state.rs @@ -1,5 +1,3 @@ -use tracing::{instrument, Span}; - use crate::sandbox::Error::FailedPrecondition; use crate::sandbox::Result; @@ -13,7 +11,7 @@ pub(super) enum TaskState { } impl TaskState { - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] pub fn start(&mut self) -> Result<()> { *self = match self { Self::Created => Ok(Self::Starting), @@ -22,7 +20,7 @@ impl TaskState { Ok(()) } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] pub fn kill(&mut self) -> Result<()> { *self = match self { Self::Started => Ok(Self::Started), @@ -31,7 +29,7 @@ impl TaskState { Ok(()) } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] pub fn delete(&mut self) -> Result<()> { *self = match self { Self::Created | Self::Exited => Ok(Self::Deleting), @@ -40,7 +38,7 @@ impl TaskState { Ok(()) } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] pub fn started(&mut self) -> Result<()> { *self = match self { Self::Starting => Ok(Self::Started), @@ -49,7 +47,7 @@ impl TaskState { Ok(()) } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] pub fn stop(&mut self) -> Result<()> { *self = match self { Self::Started | Self::Starting => Ok(Self::Exited), diff --git a/crates/containerd-shim-wasm/src/sys/unix/container/executor.rs b/crates/containerd-shim-wasm/src/sys/unix/container/executor.rs index 987f71875..036d82f51 100644 --- a/crates/containerd-shim-wasm/src/sys/unix/container/executor.rs +++ b/crates/containerd-shim-wasm/src/sys/unix/container/executor.rs @@ -12,7 +12,6 @@ use libcontainer::workload::{ }; use oci_spec::image::Platform; use oci_spec::runtime::Spec; -use tracing::{instrument, Span}; use crate::container::{Engine, PathResolve, RuntimeContext, Source, Stdio, WasiContext}; use crate::sandbox::oci::WasmLayer; @@ -34,7 +33,7 @@ pub(crate) struct Executor { } impl LibcontainerExecutor for Executor { - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn validate(&self, spec: &Spec) -> Result<(), ExecutorValidationError> { // We can handle linux container. We delegate wasm container to the engine. match self.inner(spec) { @@ -43,7 +42,7 @@ impl LibcontainerExecutor for Executor { } } - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn exec(&self, spec: &Spec) -> Result<(), LibcontainerExecutorError> { // If it looks like a linux container, run it as a linux container. // Otherwise, run it as a wasm container diff --git a/crates/containerd-shim-wasm/src/sys/unix/container/instance.rs b/crates/containerd-shim-wasm/src/sys/unix/container/instance.rs index 7cc76444e..12b77f4bb 100644 --- a/crates/containerd-shim-wasm/src/sys/unix/container/instance.rs +++ b/crates/containerd-shim-wasm/src/sys/unix/container/instance.rs @@ -13,7 +13,6 @@ use nix::errno::Errno; use nix::sys::wait::{waitid, Id as WaitID, WaitPidFlag, WaitStatus}; use nix::unistd::Pid; use oci_spec::image::Platform; -use tracing::{instrument, Span}; use crate::container::Engine; use crate::sandbox::instance_utils::{determine_rootdir, get_instance_root, instance_exists}; @@ -35,7 +34,7 @@ pub struct Instance { impl SandboxInstance for Instance { type Engine = E; - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn new(id: String, cfg: Option<&InstanceConfig>) -> Result { let cfg = cfg.context("missing configuration")?; let engine = cfg.get_engine(); @@ -71,7 +70,7 @@ impl SandboxInstance for Instance { /// Start the instance /// The returned value should be a unique ID (such as a PID) for the instance. /// Nothing internally should be using this ID, but it is returned to containerd where a user may want to use it. - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn start(&self) -> Result { log::info!("starting instance: {}", self.id); // make sure we have an exit code by the time we finish (even if there's a panic) @@ -108,7 +107,7 @@ impl SandboxInstance for Instance { } /// Send a signal to the instance - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn kill(&self, signal: u32) -> Result<(), SandboxError> { log::info!("sending signal {signal} to instance: {}", self.id); let signal = Signal::try_from(signal as i32).map_err(|err| { @@ -125,7 +124,7 @@ impl SandboxInstance for Instance { /// Delete any reference to the instance /// This is called after the instance has exited. - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn delete(&self) -> Result<(), SandboxError> { log::info!("deleting instance: {}", self.id); match instance_exists(&self.rootdir, &self.id) { @@ -151,7 +150,7 @@ impl SandboxInstance for Instance { /// Waits for the instance to finish and retunrs its exit code /// Returns None if the timeout is reached before the instance has finished. /// This is a blocking call. - #[instrument(skip_all, parent = Span::current(), level= "Info")] + #[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] fn wait_timeout(&self, t: impl Into>) -> Option<(u32, DateTime)> { self.exit_code.wait_timeout(t).copied() } diff --git a/crates/containerd-shim-wasm/src/sys/unix/metrics.rs b/crates/containerd-shim-wasm/src/sys/unix/metrics.rs index 332da0909..08a182042 100644 --- a/crates/containerd-shim-wasm/src/sys/unix/metrics.rs +++ b/crates/containerd-shim-wasm/src/sys/unix/metrics.rs @@ -2,9 +2,8 @@ use anyhow::Result; use containerd_shim::cgroup::collect_metrics; use containerd_shim::util::convert_to_any; use protobuf::well_known_types::any::Any; -use tracing::{instrument, Span}; -#[instrument(skip_all, parent = Span::current(), level= "Info")] +#[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] pub fn get_metrics(pid: u32) -> Result { let metrics = collect_metrics(pid)?; diff --git a/crates/containerd-shim-wasm/src/sys/unix/networking.rs b/crates/containerd-shim-wasm/src/sys/unix/networking.rs index 1070618bd..b54f853f7 100644 --- a/crates/containerd-shim-wasm/src/sys/unix/networking.rs +++ b/crates/containerd-shim-wasm/src/sys/unix/networking.rs @@ -5,9 +5,8 @@ use containerd_shim::error::Error as ShimError; use containerd_shim::{self as shim}; use nix::sched::{setns, unshare, CloneFlags}; use oci_spec::runtime; -use tracing::{instrument, Span}; -#[instrument(skip_all, parent = Span::current(), level= "Info")] +#[cfg_attr(feature = "tracing", tracing::instrument(parent = tracing::Span::current(), skip_all, level = "Info"))] pub fn setup_namespaces(spec: &runtime::Spec) -> Result<()> { let namespaces = spec .linux() diff --git a/crates/containerd-shim-wasmtime/Cargo.toml b/crates/containerd-shim-wasmtime/Cargo.toml index 05a8e7fa1..4742f6f45 100644 --- a/crates/containerd-shim-wasmtime/Cargo.toml +++ b/crates/containerd-shim-wasmtime/Cargo.toml @@ -6,7 +6,7 @@ edition.workspace = true [dependencies] anyhow = { workspace = true } containerd-shim = { workspace = true } -containerd-shim-wasm = { workspace = true } +containerd-shim-wasm = { workspace = true, features = ["tracing"] } log = { workspace = true } oci-spec = { workspace = true, features = ["runtime"] } ttrpc = { workspace = true }