diff --git a/src/agent/Cargo.lock b/src/agent/Cargo.lock index 633159b913..fa6320fde4 100644 --- a/src/agent/Cargo.lock +++ b/src/agent/Cargo.lock @@ -30,6 +30,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi", +] + [[package]] name = "anyhow" version = "1.0.40" @@ -80,6 +89,12 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +[[package]] +name = "bumpalo" +version = "3.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63396b8a4b9de3f4fdfb320ab6080762242f66a8ef174c49d8e19b674db4cdbe" + [[package]] name = "byteorder" version = "1.4.3" @@ -475,6 +490,15 @@ version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" +[[package]] +name = "js-sys" +version = "0.3.51" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83bdfbace3a0e81a4253f73b49e960b053e396a11012cbd49b9b74d6a2b67062" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "kata-agent" version = "0.1.0" @@ -493,6 +517,7 @@ dependencies = [ "netlink-sys", "nix 0.17.0", "oci", + "opentelemetry", "procfs", "prometheus", "protobuf", @@ -509,6 +534,9 @@ dependencies = [ "tempfile", "tokio 1.6.0", "tokio-vsock", + "tracing", + "tracing-opentelemetry", + "tracing-subscriber", "ttrpc", ] @@ -573,6 +601,15 @@ dependencies = [ "slog-scope", ] +[[package]] +name = "matchers" +version = "0.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f099785f7595cc4b4553a174ce30dd7589ef93391ff414dbb67f62392b9e0ce1" +dependencies = [ + "regex-automata", +] + [[package]] name = "memchr" version = "2.4.0" @@ -792,6 +829,23 @@ version = "1.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3" +[[package]] +name = "opentelemetry" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "492848ff47f11b7f9de0443b404e2c5775f695e1af6b7076ca25f999581d547a" +dependencies = [ + "async-trait", + "crossbeam-channel", + "futures", + "js-sys", + "lazy_static", + "percent-encoding", + "pin-project", + "rand", + "thiserror", +] + [[package]] name = "parking_lot" version = "0.11.1" @@ -842,6 +896,12 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "percent-encoding" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" + [[package]] name = "petgraph" version = "0.4.13" @@ -851,6 +911,26 @@ dependencies = [ "fixedbitset", ] +[[package]] +name = "pin-project" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7509cc106041c40a4518d2af7a61530e1eed0e6285296a3d8c5472806ccc4a4" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48c950132583b500556b1efd71d45b319029f2b71518d979fcc208e16b42426f" +dependencies = [ + "proc-macro2 1.0.26", + "quote 1.0.9", + "syn 1.0.72", +] + [[package]] name = "pin-project-lite" version = "0.1.12" @@ -1101,6 +1181,16 @@ dependencies = [ "regex-syntax", ] +[[package]] +name = "regex-automata" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae1ded71d66a4a97f5e961fd0cb25a5f366a42a41570d16a763a69c092c26ae4" +dependencies = [ + "byteorder", + "regex-syntax", +] + [[package]] name = "regex-syntax" version = "0.6.25" @@ -1256,6 +1346,15 @@ dependencies = [ "syn 1.0.72", ] +[[package]] +name = "sharded-slab" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79c719719ee05df97490f80a45acfc99e5a30ce98a1e4fb67aee422745ae14e3" +dependencies = [ + "lazy_static", +] + [[package]] name = "signal-hook-registry" version = "1.3.0" @@ -1489,6 +1588,94 @@ dependencies = [ "vsock", ] +[[package]] +name = "tracing" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09adeb8c97449311ccd28a427f96fb563e7fd31aabf994189879d9da2394b89d" +dependencies = [ + "cfg-if 1.0.0", + "pin-project-lite 0.2.6", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c42e6fa53307c8a17e4ccd4dc81cf5ec38db9209f59b222210375b54ee40d1e2" +dependencies = [ + "proc-macro2 1.0.26", + "quote 1.0.9", + "syn 1.0.72", +] + +[[package]] +name = "tracing-core" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9ff14f98b1a4b289c6248a023c1c2fa1491062964e9fed67ab29c4e4da4a052" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "tracing-log" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6923477a48e41c1951f1999ef8bb5a3023eb723ceadafe78ffb65dc366761e3" +dependencies = [ + "lazy_static", + "log", + "tracing-core", +] + +[[package]] +name = "tracing-opentelemetry" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2f4cb277b92a8ba1170b3b911056428ce2ef9993351baf5965bb0359a2e5963" +dependencies = [ + "opentelemetry", + "tracing", + "tracing-core", + "tracing-log", + "tracing-subscriber", +] + +[[package]] +name = "tracing-serde" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb65ea441fbb84f9f6748fd496cf7f63ec9af5bca94dd86456978d055e8eb28b" +dependencies = [ + "serde", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa5553bf0883ba7c9cbe493b085c29926bd41b66afc31ff72cf17ff4fb60dcd5" +dependencies = [ + "ansi_term", + "chrono", + "lazy_static", + "matchers", + "regex", + "serde", + "serde_json", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", + "tracing-serde", +] + [[package]] name = "ttrpc" version = "0.5.1" @@ -1575,6 +1762,60 @@ version = "0.10.2+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" +[[package]] +name = "wasm-bindgen" +version = "0.2.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d54ee1d4ed486f78874278e63e4069fc1ab9f6a18ca492076ffb90c5eb2997fd" +dependencies = [ + "cfg-if 1.0.0", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b33f6a0694ccfea53d94db8b2ed1c3a8a4c86dd936b13b9f0a15ec4a451b900" +dependencies = [ + "bumpalo", + "lazy_static", + "log", + "proc-macro2 1.0.26", + "quote 1.0.9", + "syn 1.0.72", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "088169ca61430fe1e58b8096c24975251700e7b1f6fd91cc9d59b04fb9b18bd4" +dependencies = [ + "quote 1.0.9", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be2241542ff3d9f241f5e2cb6dd09b37efe786df8851c54957683a49f0987a97" +dependencies = [ + "proc-macro2 1.0.26", + "quote 1.0.9", + "syn 1.0.72", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7cff876b8f18eed75a66cf49b65e7f967cb354a7aa16003fb55dbfd25b44b4f" + [[package]] name = "which" version = "2.0.1" diff --git a/src/agent/Cargo.toml b/src/agent/Cargo.toml index 47df6eaee0..98aecf7917 100644 --- a/src/agent/Cargo.toml +++ b/src/agent/Cargo.toml @@ -45,6 +45,12 @@ procfs = "0.7.9" anyhow = "1.0.32" cgroups = { package = "cgroups-rs", version = "0.2.5" } +# Tracing +tracing = "0.1.26" +tracing-subscriber = "0.2.18" +tracing-opentelemetry = "0.13.0" +opentelemetry = "0.14.0" + [dev-dependencies] tempfile = "3.1.0" diff --git a/src/agent/src/config.rs b/src/agent/src/config.rs index bc415cdc15..2bb70dfb80 100644 --- a/src/agent/src/config.rs +++ b/src/agent/src/config.rs @@ -6,6 +6,7 @@ use anyhow::{anyhow, Result}; use std::env; use std::fs; use std::time; +use tracing::instrument; const DEBUG_CONSOLE_FLAG: &str = "agent.debug_console"; const DEV_MODE_FLAG: &str = "agent.devmode"; @@ -101,6 +102,7 @@ impl AgentConfig { } } + #[instrument] pub fn parse_cmdline(&mut self, file: &str) -> Result<()> { let cmdline = fs::read_to_string(file)?; let params: Vec<&str> = cmdline.split_ascii_whitespace().collect(); @@ -171,6 +173,7 @@ impl AgentConfig { } } +#[instrument] fn get_vsock_port(p: &str) -> Result { let fields: Vec<&str> = p.split('=').collect(); if fields.len() != 2 { @@ -185,6 +188,7 @@ fn get_vsock_port(p: &str) -> Result { // // Note: Logrus names are used for compatability with the previous // golang-based agent. +#[instrument] fn logrus_to_slog_level(logrus_level: &str) -> Result { let level = match logrus_level { // Note: different semantics to logrus: log, but don't panic. @@ -207,6 +211,7 @@ fn logrus_to_slog_level(logrus_level: &str) -> Result { Ok(level) } +#[instrument] fn get_log_level(param: &str) -> Result { let fields: Vec<&str> = param.split('=').collect(); @@ -221,6 +226,7 @@ fn get_log_level(param: &str) -> Result { } } +#[instrument] fn get_hotplug_timeout(param: &str) -> Result { let fields: Vec<&str> = param.split('=').collect(); @@ -241,6 +247,7 @@ fn get_hotplug_timeout(param: &str) -> Result { Ok(time::Duration::from_secs(value.unwrap())) } +#[instrument] fn get_bool_value(param: &str) -> Result { let fields: Vec<&str> = param.split('=').collect(); @@ -265,6 +272,7 @@ fn get_bool_value(param: &str) -> Result { // - A value can contain any number of equal signs. // - We could/should maybe check if the name is pure whitespace // since this is considered to be invalid. +#[instrument] fn get_string_value(param: &str) -> Result { let fields: Vec<&str> = param.split('=').collect(); @@ -285,6 +293,7 @@ fn get_string_value(param: &str) -> Result { Ok(value) } +#[instrument] fn get_container_pipe_size(param: &str) -> Result { let fields: Vec<&str> = param.split('=').collect(); diff --git a/src/agent/src/device.rs b/src/agent/src/device.rs index 29efb07997..5af1f649c0 100644 --- a/src/agent/src/device.rs +++ b/src/agent/src/device.rs @@ -22,6 +22,7 @@ use crate::uevent::{wait_for_uevent, Uevent, UeventMatcher}; use anyhow::{anyhow, Result}; use oci::{LinuxDeviceCgroup, LinuxResources, Spec}; use protocols::agent::Device; +use tracing::instrument; // Convenience macro to obtain the scope logger macro_rules! sl { @@ -32,17 +33,21 @@ macro_rules! sl { const VM_ROOTFS: &str = "/"; +#[derive(Debug)] struct DevIndexEntry { idx: usize, residx: Vec, } +#[derive(Debug)] struct DevIndex(HashMap); +#[instrument] pub fn rescan_pci_bus() -> Result<()> { online_device(SYSFS_PCI_BUS_RESCAN_FILE) } +#[instrument] pub fn online_device(path: &str) -> Result<()> { fs::write(path, "1")?; Ok(()) @@ -51,6 +56,7 @@ pub fn online_device(path: &str) -> Result<()> { // pcipath_to_sysfs fetches the sysfs path for a PCI path, relative to // the sysfs path for the PCI host bridge, based on the PCI path // provided. +#[instrument] fn pcipath_to_sysfs(root_bus_sysfs: &str, pcipath: &pci::Path) -> Result { let mut bus = "0000:00".to_string(); let mut relpath = String::new(); @@ -109,6 +115,7 @@ impl UeventMatcher for ScsiBlockMatcher { } } +#[instrument] pub async fn get_scsi_device_name( sandbox: &Arc>, scsi_addr: &str, @@ -141,6 +148,7 @@ impl UeventMatcher for VirtioBlkPciMatcher { } } +#[instrument] pub async fn get_virtio_blk_pci_device_name( sandbox: &Arc>, pcipath: &pci::Path, @@ -177,6 +185,7 @@ impl UeventMatcher for PmemBlockMatcher { } } +#[instrument] pub async fn wait_for_pmem_device(sandbox: &Arc>, devpath: &str) -> Result<()> { let devname = match devpath.strip_prefix("/dev/") { Some(dev) => dev, @@ -201,6 +210,7 @@ pub async fn wait_for_pmem_device(sandbox: &Arc>, devpath: &str) } /// Scan SCSI bus for the given SCSI address(SCSI-Id and LUN) +#[instrument] fn scan_scsi_bus(scsi_addr: &str) -> Result<()> { let tokens: Vec<&str> = scsi_addr.split(':').collect(); if tokens.len() != 2 { @@ -235,6 +245,7 @@ fn scan_scsi_bus(scsi_addr: &str) -> Result<()> { // the same device in the list of devices provided through the OCI spec. // This is needed to update information about minor/major numbers that cannot // be predicted from the caller. +#[instrument] fn update_spec_device_list(device: &Device, spec: &mut Spec, devidx: &DevIndex) -> Result<()> { let major_id: c_uint; let minor_id: c_uint; @@ -311,6 +322,7 @@ fn update_spec_device_list(device: &Device, spec: &mut Spec, devidx: &DevIndex) // device.Id should be the predicted device name (vda, vdb, ...) // device.VmPath already provides a way to send it in +#[instrument] async fn virtiommio_blk_device_handler( device: &Device, spec: &mut Spec, @@ -325,6 +337,7 @@ async fn virtiommio_blk_device_handler( } // device.Id should be a PCI path string +#[instrument] async fn virtio_blk_device_handler( device: &Device, spec: &mut Spec, @@ -340,6 +353,7 @@ async fn virtio_blk_device_handler( } // device.Id should be the SCSI address of the disk in the format "scsiID:lunID" +#[instrument] async fn virtio_scsi_device_handler( device: &Device, spec: &mut Spec, @@ -351,6 +365,7 @@ async fn virtio_scsi_device_handler( update_spec_device_list(&dev, spec, devidx) } +#[instrument] async fn virtio_nvdimm_device_handler( device: &Device, spec: &mut Spec, @@ -389,6 +404,7 @@ impl DevIndex { } } +#[instrument] pub async fn add_devices( devices: &[Device], spec: &mut Spec, @@ -403,6 +419,7 @@ pub async fn add_devices( Ok(()) } +#[instrument] async fn add_device( device: &Device, spec: &mut Spec, @@ -437,6 +454,7 @@ async fn add_device( // update_device_cgroup update the device cgroup for container // to not allow access to the guest root partition. This prevents // the container from being able to access the VM rootfs. +#[instrument] pub fn update_device_cgroup(spec: &mut Spec) -> Result<()> { let meta = fs::metadata(VM_ROOTFS)?; let rdev = meta.dev(); diff --git a/src/agent/src/main.rs b/src/agent/src/main.rs index 3ab3971988..4431158355 100644 --- a/src/agent/src/main.rs +++ b/src/agent/src/main.rs @@ -32,6 +32,7 @@ use std::os::unix::io::AsRawFd; use std::path::Path; use std::process::exit; use std::sync::Arc; +use tracing::instrument; mod config; mod console; @@ -79,6 +80,7 @@ lazy_static! { Arc::new(RwLock::new(config::AgentConfig::new())); } +#[instrument] fn announce(logger: &Logger, config: &AgentConfig) { info!(logger, "announce"; "agent-commit" => version::VERSION_COMMIT, @@ -259,6 +261,7 @@ fn main() -> std::result::Result<(), Box> { rt.block_on(real_main()) } +#[instrument] async fn start_sandbox( logger: &Logger, config: &AgentConfig, @@ -345,6 +348,7 @@ fn init_agent_as_init(logger: &Logger, unified_cgroup_hierarchy: bool) -> Result Ok(()) } +#[instrument] fn sethostname(hostname: &OsStr) -> Result<()> { let size = hostname.len() as usize; diff --git a/src/agent/src/metrics.rs b/src/agent/src/metrics.rs index f1c2da0cb4..c49b41d98a 100644 --- a/src/agent/src/metrics.rs +++ b/src/agent/src/metrics.rs @@ -8,6 +8,7 @@ extern crate procfs; use prometheus::{Encoder, Gauge, GaugeVec, IntCounter, TextEncoder}; use anyhow::Result; +use tracing::instrument; const NAMESPACE_KATA_AGENT: &str = "kata_agent"; const NAMESPACE_KATA_GUEST: &str = "kata_guest"; @@ -68,6 +69,7 @@ lazy_static! { prometheus::register_gauge_vec!(format!("{}_{}",NAMESPACE_KATA_GUEST,"meminfo").as_ref() , "Statistics about memory usage in the system.", &["item"]).unwrap(); } +#[instrument] pub fn get_metrics(_: &protocols::agent::GetMetricsRequest) -> Result { AGENT_SCRAPE_COUNT.inc(); @@ -87,6 +89,7 @@ pub fn get_metrics(_: &protocols::agent::GetMetricsRequest) -> Result { Ok(String::from_utf8(buffer).unwrap()) } +#[instrument] fn update_agent_metrics() { let me = procfs::process::Process::myself(); @@ -136,6 +139,7 @@ fn update_agent_metrics() { } } +#[instrument] fn update_guest_metrics() { // try get load and task info match procfs::LoadAverage::new() { @@ -218,6 +222,7 @@ fn update_guest_metrics() { } } +#[instrument] fn set_gauge_vec_meminfo(gv: &prometheus::GaugeVec, meminfo: &procfs::Meminfo) { gv.with_label_values(&["mem_total"]) .set(meminfo.mem_total as f64); @@ -332,6 +337,7 @@ fn set_gauge_vec_meminfo(gv: &prometheus::GaugeVec, meminfo: &procfs::Meminfo) { .set(meminfo.k_reclaimable.unwrap_or(0) as f64); } +#[instrument] fn set_gauge_vec_cpu_time(gv: &prometheus::GaugeVec, cpu: &str, cpu_time: &procfs::CpuTime) { gv.with_label_values(&[cpu, "user"]) .set(cpu_time.user as f64); @@ -355,6 +361,7 @@ fn set_gauge_vec_cpu_time(gv: &prometheus::GaugeVec, cpu: &str, cpu_time: &procf .set(cpu_time.guest_nice.unwrap_or(0.0) as f64); } +#[instrument] fn set_gauge_vec_diskstat(gv: &prometheus::GaugeVec, diskstat: &procfs::DiskStat) { gv.with_label_values(&[diskstat.name.as_str(), "reads"]) .set(diskstat.reads as f64); @@ -393,6 +400,7 @@ fn set_gauge_vec_diskstat(gv: &prometheus::GaugeVec, diskstat: &procfs::DiskStat } // set_gauge_vec_netdev set gauge for NetDevLine +#[instrument] fn set_gauge_vec_netdev(gv: &prometheus::GaugeVec, status: &procfs::net::DeviceStatus) { gv.with_label_values(&[status.name.as_str(), "recv_bytes"]) .set(status.recv_bytes as f64); @@ -429,6 +437,7 @@ fn set_gauge_vec_netdev(gv: &prometheus::GaugeVec, status: &procfs::net::DeviceS } // set_gauge_vec_proc_status set gauge for ProcStatus +#[instrument] fn set_gauge_vec_proc_status(gv: &prometheus::GaugeVec, status: &procfs::process::Status) { gv.with_label_values(&["vmpeak"]) .set(status.vmpeak.unwrap_or(0) as f64); @@ -469,6 +478,7 @@ fn set_gauge_vec_proc_status(gv: &prometheus::GaugeVec, status: &procfs::process } // set_gauge_vec_proc_io set gauge for ProcIO +#[instrument] fn set_gauge_vec_proc_io(gv: &prometheus::GaugeVec, io_stat: &procfs::process::Io) { gv.with_label_values(&["rchar"]).set(io_stat.rchar as f64); gv.with_label_values(&["wchar"]).set(io_stat.wchar as f64); @@ -483,6 +493,7 @@ fn set_gauge_vec_proc_io(gv: &prometheus::GaugeVec, io_stat: &procfs::process::I } // set_gauge_vec_proc_stat set gauge for ProcStat +#[instrument] fn set_gauge_vec_proc_stat(gv: &prometheus::GaugeVec, stat: &procfs::process::Stat) { gv.with_label_values(&["utime"]).set(stat.utime as f64); gv.with_label_values(&["stime"]).set(stat.stime as f64); diff --git a/src/agent/src/mount.rs b/src/agent/src/mount.rs index b4794f27e0..ca3e5c7dc7 100644 --- a/src/agent/src/mount.rs +++ b/src/agent/src/mount.rs @@ -32,6 +32,7 @@ use crate::protocols::agent::Storage; use crate::Sandbox; use anyhow::{anyhow, Context, Result}; use slog::Logger; +use tracing::instrument; pub const DRIVER_9P_TYPE: &str = "9p"; pub const DRIVER_VIRTIOFS_TYPE: &str = "virtio-fs"; @@ -156,6 +157,7 @@ pub struct BareMount<'a> { // * evaluate all symlinks // * ensure the source exists impl<'a> BareMount<'a> { + #[instrument] pub fn new( s: &'a str, d: &'a str, @@ -174,6 +176,7 @@ impl<'a> BareMount<'a> { } } + #[instrument] pub fn mount(&self) -> Result<()> { let source; let dest; @@ -232,6 +235,7 @@ impl<'a> BareMount<'a> { } } +#[instrument] async fn ephemeral_storage_handler( logger: &Logger, storage: &Storage, @@ -278,6 +282,7 @@ async fn ephemeral_storage_handler( Ok("".to_string()) } +#[instrument] async fn local_storage_handler( _logger: &Logger, storage: &Storage, @@ -324,6 +329,7 @@ async fn local_storage_handler( Ok("".to_string()) } +#[instrument] async fn virtio9p_storage_handler( logger: &Logger, storage: &Storage, @@ -333,6 +339,7 @@ async fn virtio9p_storage_handler( } // virtiommio_blk_storage_handler handles the storage for mmio blk driver. +#[instrument] async fn virtiommio_blk_storage_handler( logger: &Logger, storage: &Storage, @@ -343,6 +350,7 @@ async fn virtiommio_blk_storage_handler( } // virtiofs_storage_handler handles the storage for virtio-fs. +#[instrument] async fn virtiofs_storage_handler( logger: &Logger, storage: &Storage, @@ -352,6 +360,7 @@ async fn virtiofs_storage_handler( } // virtio_blk_storage_handler handles the storage for blk driver. +#[instrument] async fn virtio_blk_storage_handler( logger: &Logger, storage: &Storage, @@ -377,7 +386,8 @@ async fn virtio_blk_storage_handler( common_storage_handler(logger, &storage) } -// virtio_scsi_storage_handler handles the storage for scsi driver. +// virtio_scsi_storage_handler handles the storage for scsi driver. +#[instrument] async fn virtio_scsi_storage_handler( logger: &Logger, storage: &Storage, @@ -392,6 +402,7 @@ async fn virtio_scsi_storage_handler( common_storage_handler(logger, &storage) } +#[instrument] fn common_storage_handler(logger: &Logger, storage: &Storage) -> Result { // Mount the storage device. let mount_point = storage.mount_point.to_string(); @@ -400,6 +411,7 @@ fn common_storage_handler(logger: &Logger, storage: &Storage) -> Result } // nvdimm_storage_handler handles the storage for NVDIMM driver. +#[instrument] async fn nvdimm_storage_handler( logger: &Logger, storage: &Storage, @@ -414,6 +426,7 @@ async fn nvdimm_storage_handler( } // mount_storage performs the mount described by the storage structure. +#[instrument] fn mount_storage(logger: &Logger, storage: &Storage) -> Result<()> { let logger = logger.new(o!("subsystem" => "mount")); @@ -464,6 +477,7 @@ fn mount_storage(logger: &Logger, storage: &Storage) -> Result<()> { } /// Looks for `mount_point` entry in the /proc/mounts. +#[instrument] fn is_mounted(mount_point: &str) -> Result { let mount_point = mount_point.trim_end_matches('/'); let found = fs::metadata(mount_point).is_ok() @@ -481,6 +495,7 @@ fn is_mounted(mount_point: &str) -> Result { Ok(found) } +#[instrument] fn parse_mount_flags_and_options(options_vec: Vec<&str>) -> (MsFlags, String) { let mut flags = MsFlags::empty(); let mut options: String = "".to_string(); @@ -509,6 +524,7 @@ fn parse_mount_flags_and_options(options_vec: Vec<&str>) -> (MsFlags, String) { // associated operations such as waiting for the device to show up, and mount // it to a specific location, according to the type of handler chosen, and for // each storage. +#[instrument] pub async fn add_storages( logger: Logger, storages: Vec, @@ -558,6 +574,7 @@ pub async fn add_storages( Ok(mount_list) } +#[instrument] fn mount_to_rootfs(logger: &Logger, m: &InitMount) -> Result<()> { let options_vec: Vec<&str> = m.options.clone(); @@ -583,6 +600,7 @@ fn mount_to_rootfs(logger: &Logger, m: &InitMount) -> Result<()> { Ok(()) } +#[instrument] pub fn general_mount(logger: &Logger) -> Result<()> { let logger = logger.new(o!("subsystem" => "mount")); @@ -600,6 +618,7 @@ pub fn get_mount_fs_type(mount_point: &str) -> Result { // get_mount_fs_type_from_file returns the FS type corresponding to the passed mount point and // any error ecountered. +#[instrument] pub fn get_mount_fs_type_from_file(mount_file: &str, mount_point: &str) -> Result { if mount_point.is_empty() { return Err(anyhow!("Invalid mount point {}", mount_point)); @@ -630,6 +649,7 @@ pub fn get_mount_fs_type_from_file(mount_file: &str, mount_point: &str) -> Resul )) } +#[instrument] pub fn get_cgroup_mounts( logger: &Logger, cg_path: &str, @@ -720,6 +740,7 @@ pub fn get_cgroup_mounts( Ok(cg_mounts) } +#[instrument] pub fn cgroups_mount(logger: &Logger, unified_cgroup_hierarchy: bool) -> Result<()> { let logger = logger.new(o!("subsystem" => "mount")); @@ -735,6 +756,7 @@ pub fn cgroups_mount(logger: &Logger, unified_cgroup_hierarchy: bool) -> Result< Ok(()) } +#[instrument] pub fn remove_mounts(mounts: &[String]) -> Result<()> { for m in mounts.iter() { mount::umount(m.as_str()).context(format!("failed to umount {:?}", m))?; @@ -744,6 +766,7 @@ pub fn remove_mounts(mounts: &[String]) -> Result<()> { // ensure_destination_exists will recursively create a given mountpoint. If directories // are created, their permissions are initialized to mountPerm(0755) +#[instrument] fn ensure_destination_exists(destination: &str, fs_type: &str) -> Result<()> { let d = Path::new(destination); if !d.exists() { @@ -764,6 +787,7 @@ fn ensure_destination_exists(destination: &str, fs_type: &str) -> Result<()> { Ok(()) } +#[instrument] fn parse_options(option_list: Vec) -> HashMap { let mut options = HashMap::new(); for opt in option_list.iter() { diff --git a/src/agent/src/namespace.rs b/src/agent/src/namespace.rs index 8f14c8e4c7..749be2ceb5 100644 --- a/src/agent/src/namespace.rs +++ b/src/agent/src/namespace.rs @@ -11,6 +11,7 @@ use std::fmt; use std::fs; use std::fs::File; use std::path::{Path, PathBuf}; +use tracing::instrument; use crate::mount::{BareMount, FLAGS}; use slog::Logger; @@ -20,6 +21,7 @@ pub const NSTYPEIPC: &str = "ipc"; pub const NSTYPEUTS: &str = "uts"; pub const NSTYPEPID: &str = "pid"; +#[instrument] pub fn get_current_thread_ns_path(ns_type: &str) -> String { format!( "/proc/{}/task/{}/ns/{}", @@ -40,6 +42,7 @@ pub struct Namespace { } impl Namespace { + #[instrument] pub fn new(logger: &Logger) -> Self { Namespace { logger: logger.clone(), @@ -50,11 +53,13 @@ impl Namespace { } } + #[instrument] pub fn get_ipc(mut self) -> Self { self.ns_type = NamespaceType::Ipc; self } + #[instrument] pub fn get_uts(mut self, hostname: &str) -> Self { self.ns_type = NamespaceType::Uts; if !hostname.is_empty() { @@ -63,6 +68,7 @@ impl Namespace { self } + #[instrument] pub fn get_pid(mut self) -> Self { self.ns_type = NamespaceType::Pid; self @@ -76,6 +82,7 @@ impl Namespace { // setup creates persistent namespace without switching to it. // Note, pid namespaces cannot be persisted. + #[instrument] pub async fn setup(mut self) -> Result { fs::create_dir_all(&self.persistent_ns_dir)?; diff --git a/src/agent/src/random.rs b/src/agent/src/random.rs index a58213de2e..4713134a8f 100644 --- a/src/agent/src/random.rs +++ b/src/agent/src/random.rs @@ -9,6 +9,7 @@ use nix::fcntl::{self, OFlag}; use nix::sys::stat::Mode; use std::fs; use std::os::unix::io::{AsRawFd, FromRawFd}; +use tracing::instrument; pub const RNGDEV: &str = "/dev/random"; pub const RNDADDTOENTCNT: libc::c_int = 0x40045201; @@ -20,6 +21,7 @@ type IoctlRequestType = libc::c_int; #[cfg(target_env = "gnu")] type IoctlRequestType = libc::c_ulong; +#[instrument] pub fn reseed_rng(data: &[u8]) -> Result<()> { let len = data.len() as libc::c_long; fs::write(RNGDEV, data)?; diff --git a/src/agent/src/sandbox.rs b/src/agent/src/sandbox.rs index b71fa5534a..56afa02fcd 100644 --- a/src/agent/src/sandbox.rs +++ b/src/agent/src/sandbox.rs @@ -28,6 +28,7 @@ use std::{thread, time}; use tokio::sync::mpsc::{channel, Receiver, Sender}; use tokio::sync::oneshot; use tokio::sync::Mutex; +use tracing::instrument; type UeventWatcher = (Box, oneshot::Sender); @@ -56,6 +57,7 @@ pub struct Sandbox { } impl Sandbox { + #[instrument] pub fn new(logger: &Logger) -> Result { let fs_type = get_mount_fs_type("/")?; let logger = logger.new(o!("subsystem" => "sandbox")); @@ -94,6 +96,7 @@ impl Sandbox { // // It's assumed that caller is calling this method after // acquiring a lock on sandbox. + #[instrument] pub fn set_sandbox_storage(&mut self, path: &str) -> bool { match self.storages.get_mut(path) { None => { @@ -116,6 +119,7 @@ impl Sandbox { // // It's assumed that caller is calling this method after // acquiring a lock on sandbox. + #[instrument] pub fn unset_sandbox_storage(&mut self, path: &str) -> Result { match self.storages.get_mut(path) { None => Err(anyhow!("Sandbox storage with path {} not found", path)), @@ -135,6 +139,7 @@ impl Sandbox { // // It's assumed that caller is calling this method after // acquiring a lock on sandbox. + #[instrument] pub fn remove_sandbox_storage(&self, path: &str) -> Result<()> { let mounts = vec![path.to_string()]; remove_mounts(&mounts)?; @@ -148,6 +153,7 @@ impl Sandbox { // // It's assumed that caller is calling this method after // acquiring a lock on sandbox. + #[instrument] pub fn unset_and_remove_sandbox_storage(&mut self, path: &str) -> Result<()> { if self.unset_sandbox_storage(path)? { return self.remove_sandbox_storage(path); @@ -156,6 +162,7 @@ impl Sandbox { Ok(()) } + #[instrument] pub async fn setup_shared_namespaces(&mut self) -> Result { // Set up shared IPC namespace self.shared_ipcns = Namespace::new(&self.logger) @@ -174,10 +181,12 @@ impl Sandbox { Ok(true) } + #[instrument] pub fn add_container(&mut self, c: LinuxContainer) { self.containers.insert(c.id.clone(), c); } + #[instrument] pub fn update_shared_pidns(&mut self, c: &LinuxContainer) -> Result<()> { // Populate the shared pid path only if this is an infra container and // sandbox_pidns has not been passed in the create_sandbox request. @@ -201,10 +210,12 @@ impl Sandbox { Ok(()) } + #[instrument] pub fn get_container(&mut self, id: &str) -> Option<&mut LinuxContainer> { self.containers.get_mut(id) } + #[instrument] pub fn find_process(&mut self, pid: pid_t) -> Option<&mut Process> { for (_, c) in self.containers.iter_mut() { if c.processes.get(&pid).is_some() { @@ -215,6 +226,7 @@ impl Sandbox { None } + #[instrument] pub async fn destroy(&mut self) -> Result<()> { for ctr in self.containers.values_mut() { ctr.destroy().await?; @@ -222,6 +234,7 @@ impl Sandbox { Ok(()) } + #[instrument] pub fn online_cpu_memory(&self, req: &OnlineCPUMemRequest) -> Result<()> { if req.nb_cpus > 0 { // online cpus @@ -265,6 +278,7 @@ impl Sandbox { Ok(()) } + #[instrument] pub fn add_hooks(&mut self, dir: &str) -> Result<()> { let mut hooks = Hooks::default(); if let Ok(hook) = self.find_hooks(dir, "prestart") { @@ -280,6 +294,7 @@ impl Sandbox { Ok(()) } + #[instrument] fn find_hooks(&self, hook_path: &str, hook_type: &str) -> Result> { let mut hooks = Vec::new(); for entry in fs::read_dir(Path::new(hook_path).join(hook_type))? { @@ -316,6 +331,7 @@ impl Sandbox { Ok(hooks) } + #[instrument] pub async fn run_oom_event_monitor(&self, mut rx: Receiver, container_id: String) { let logger = self.logger.clone(); @@ -348,6 +364,7 @@ impl Sandbox { } } +#[instrument] fn online_resources(logger: &Logger, path: &str, pattern: &str, num: i32) -> Result { let mut count = 0; let re = Regex::new(pattern)?; @@ -393,6 +410,7 @@ fn online_resources(logger: &Logger, path: &str, pattern: &str, num: i32) -> Res const ONLINE_CPUMEM_WATI_MILLIS: u64 = 50; const ONLINE_CPUMEM_MAX_RETRIES: u32 = 100; +#[instrument] fn online_cpus(logger: &Logger, num: i32) -> Result { let mut onlined_count: i32 = 0; @@ -422,6 +440,7 @@ fn online_cpus(logger: &Logger, num: i32) -> Result { )) } +#[instrument] fn online_memory(logger: &Logger) -> Result<()> { online_resources(logger, SYSFS_MEMORY_ONLINE_PATH, r"memory[0-9]+", -1)?; Ok(()) diff --git a/src/agent/src/uevent.rs b/src/agent/src/uevent.rs index d00de30160..e7d7b3bf68 100644 --- a/src/agent/src/uevent.rs +++ b/src/agent/src/uevent.rs @@ -18,6 +18,7 @@ use std::sync::Arc; use tokio::select; use tokio::sync::watch::Receiver; use tokio::sync::Mutex; +use tracing::instrument; // Convenience macro to obtain the scope logger macro_rules! sl { @@ -64,6 +65,7 @@ impl Uevent { event } + #[instrument] async fn process_add(&self, logger: &Logger, sandbox: &Arc>) { // Special case for memory hot-adds first let online_path = format!("{}/{}/online", SYSFS_DIR, &self.devpath); @@ -95,6 +97,7 @@ impl Uevent { } } + #[instrument] async fn process(&self, logger: &Logger, sandbox: &Arc>) { if self.action == U_EVENT_ACTION_ADD { return self.process_add(logger, sandbox).await; @@ -103,6 +106,7 @@ impl Uevent { } } +#[instrument] pub async fn wait_for_uevent( sandbox: &Arc>, matcher: impl UeventMatcher, @@ -145,6 +149,7 @@ pub async fn wait_for_uevent( Ok(uev) } +#[instrument] pub async fn watch_uevents( sandbox: Arc>, mut shutdown: Receiver, diff --git a/src/agent/src/util.rs b/src/agent/src/util.rs index 82877f8ba6..0e262e7ee3 100644 --- a/src/agent/src/util.rs +++ b/src/agent/src/util.rs @@ -11,6 +11,7 @@ use std::os::unix::io::{FromRawFd, RawFd}; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::sync::watch::Receiver; use tokio_vsock::{Incoming, VsockListener, VsockStream}; +use tracing::instrument; // Size of I/O read buffer const BUF_SIZE: usize = 8192; @@ -56,10 +57,12 @@ where Ok(total_bytes) } +#[instrument] pub fn get_vsock_incoming(fd: RawFd) -> Incoming { unsafe { VsockListener::from_raw_fd(fd).incoming() } } +#[instrument] pub async fn get_vsock_stream(fd: RawFd) -> Result { let stream = get_vsock_incoming(fd).next().await.unwrap()?; Ok(stream)