diff --git a/src/agent/Cargo.lock b/src/agent/Cargo.lock index 05b2dfd6e1..a7c48ca7f6 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" @@ -74,12 +83,27 @@ dependencies = [ "rustc-demangle", ] +[[package]] +name = "bincode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + [[package]] name = "bitflags" 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 +499,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 +526,7 @@ dependencies = [ "netlink-sys", "nix 0.17.0", "oci", + "opentelemetry", "procfs", "prometheus", "protobuf", @@ -509,7 +543,11 @@ dependencies = [ "tempfile", "tokio 1.6.0", "tokio-vsock", + "tracing", + "tracing-opentelemetry", + "tracing-subscriber", "ttrpc", + "vsock-exporter", ] [[package]] @@ -573,6 +611,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 +839,24 @@ 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", + "serde", + "thiserror", +] + [[package]] name = "parking_lot" version = "0.11.1" @@ -842,6 +907,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 +922,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 +1192,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" @@ -1211,6 +1312,9 @@ name = "serde" version = "1.0.126" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec7505abeacaec74ae4778d9d9328fe5a5d04253220a85c4ee022239fc996d03" +dependencies = [ + "serde_derive", +] [[package]] name = "serde_derive" @@ -1256,6 +1360,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 +1602,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" @@ -1569,12 +1770,82 @@ dependencies = [ "nix 0.19.1", ] +[[package]] +name = "vsock-exporter" +version = "0.1.0" +dependencies = [ + "async-trait", + "bincode", + "byteorder", + "libc", + "nix 0.20.0", + "opentelemetry", + "serde", + "slog", + "thiserror", + "vsock", +] + [[package]] name = "wasi" 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 fbdd2cfc31..f4b3267084 100644 --- a/src/agent/Cargo.toml +++ b/src/agent/Cargo.toml @@ -45,6 +45,13 @@ 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" +vsock-exporter = { path = "vsock-exporter" } + [dev-dependencies] tempfile = "3.1.0" diff --git a/src/agent/src/config.rs b/src/agent/src/config.rs index bc415cdc15..01ae7a226a 100644 --- a/src/agent/src/config.rs +++ b/src/agent/src/config.rs @@ -2,13 +2,16 @@ // // SPDX-License-Identifier: Apache-2.0 // +use crate::tracer; 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"; +const TRACE_MODE_OPTION: &str = "agent.trace"; const LOG_LEVEL_OPTION: &str = "agent.log"; const SERVER_ADDR_OPTION: &str = "agent.server_addr"; const HOTPLUG_TIMOUT_OPTION: &str = "agent.hotplug_timeout"; @@ -26,6 +29,7 @@ const VSOCK_PORT: u16 = 1024; // Environment variables used for development and testing const SERVER_ADDR_ENV_VAR: &str = "KATA_AGENT_SERVER_ADDR"; const LOG_LEVEL_ENV_VAR: &str = "KATA_AGENT_LOG_LEVEL"; +const TRACE_TYPE_ENV_VAR: &str = "KATA_AGENT_TRACE_TYPE"; const ERR_INVALID_LOG_LEVEL: &str = "invalid log level"; const ERR_INVALID_LOG_LEVEL_PARAM: &str = "invalid log level parameter"; @@ -54,6 +58,7 @@ pub struct AgentConfig { pub container_pipe_size: i32, pub server_addr: String, pub unified_cgroup_hierarchy: bool, + pub tracing: tracer::TraceType, } // parse_cmdline_param parse commandline parameters. @@ -98,9 +103,11 @@ impl AgentConfig { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: format!("{}:{}", VSOCK_ADDR, VSOCK_PORT), unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, } } + #[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(); @@ -109,6 +116,15 @@ impl AgentConfig { parse_cmdline_param!(param, DEBUG_CONSOLE_FLAG, self.debug_console); parse_cmdline_param!(param, DEV_MODE_FLAG, self.dev_mode); + // Support "bare" tracing option for backwards compatibility with + // Kata 1.x. + if param == &TRACE_MODE_OPTION { + self.tracing = tracer::TraceType::Isolated; + continue; + } + + parse_cmdline_param!(param, TRACE_MODE_OPTION, self.tracing, get_trace_type); + // parse cmdline options parse_cmdline_param!(param, LOG_LEVEL_OPTION, self.log_level, get_log_level); parse_cmdline_param!( @@ -167,10 +183,17 @@ impl AgentConfig { } } + if let Ok(value) = env::var(TRACE_TYPE_ENV_VAR) { + if let Ok(result) = value.parse::() { + self.tracing = result; + } + } + Ok(()) } } +#[instrument] fn get_vsock_port(p: &str) -> Result { let fields: Vec<&str> = p.split('=').collect(); if fields.len() != 2 { @@ -185,6 +208,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 +231,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 +246,28 @@ fn get_log_level(param: &str) -> Result { } } +#[instrument] +fn get_trace_type(param: &str) -> Result { + if param.is_empty() { + return Err(anyhow!("invalid trace type parameter")); + } + + let fields: Vec<&str> = param.split('=').collect(); + + if fields[0] != TRACE_MODE_OPTION { + return Err(anyhow!("invalid trace type key name")); + } + + if fields.len() == 1 { + return Ok(tracer::TraceType::Isolated); + } + + let result = fields[1].parse::()?; + + Ok(result) +} + +#[instrument] fn get_hotplug_timeout(param: &str) -> Result { let fields: Vec<&str> = param.split('=').collect(); @@ -241,6 +288,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 +313,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 +334,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(); @@ -319,6 +369,10 @@ mod tests { use std::time; use tempfile::tempdir; + const ERR_INVALID_TRACE_TYPE_PARAM: &str = "invalid trace type parameter"; + const ERR_INVALID_TRACE_TYPE: &str = "invalid trace type"; + const ERR_INVALID_TRACE_TYPE_KEY: &str = "invalid trace type key name"; + // helper function to make errors less crazy-long fn make_err(desc: &str) -> Error { anyhow!(desc.to_string()) @@ -374,6 +428,7 @@ mod tests { container_pipe_size: i32, server_addr: &'a str, unified_cgroup_hierarchy: bool, + tracing: tracer::TraceType, } let tests = &[ @@ -387,6 +442,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "agent.debug_console agent.devmodex", @@ -398,6 +454,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "agent.logx=debug", @@ -409,6 +466,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "agent.log=debug", @@ -420,6 +478,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "agent.log=debug", @@ -431,6 +490,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "", @@ -442,6 +502,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "foo", @@ -453,6 +514,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "foo bar", @@ -464,6 +526,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "foo bar", @@ -475,6 +538,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "foo agent bar", @@ -486,6 +550,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "foo debug_console agent bar devmode", @@ -497,6 +562,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "agent.debug_console", @@ -508,6 +574,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: " agent.debug_console ", @@ -519,6 +586,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "agent.debug_console foo", @@ -530,6 +598,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: " agent.debug_console foo", @@ -541,6 +610,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "foo agent.debug_console bar", @@ -552,6 +622,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "foo agent.debug_console", @@ -563,6 +634,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "foo agent.debug_console ", @@ -574,6 +646,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "agent.devmode", @@ -585,6 +658,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: " agent.devmode ", @@ -596,6 +670,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "agent.devmode foo", @@ -607,6 +682,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: " agent.devmode foo", @@ -618,6 +694,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "foo agent.devmode bar", @@ -629,6 +706,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "foo agent.devmode", @@ -640,6 +718,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "foo agent.devmode ", @@ -651,6 +730,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "agent.devmode agent.debug_console", @@ -662,6 +742,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "agent.devmode agent.debug_console agent.hotplug_timeout=100 agent.unified_cgroup_hierarchy=a", @@ -673,6 +754,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "agent.devmode agent.debug_console agent.hotplug_timeout=0 agent.unified_cgroup_hierarchy=11", @@ -684,6 +766,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: true, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "agent.devmode agent.debug_console agent.container_pipe_size=2097152 agent.unified_cgroup_hierarchy=false", @@ -695,6 +778,7 @@ mod tests { container_pipe_size: 2097152, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "agent.devmode agent.debug_console agent.container_pipe_size=100 agent.unified_cgroup_hierarchy=true", @@ -706,6 +790,7 @@ mod tests { container_pipe_size: 100, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: true, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "agent.devmode agent.debug_console agent.container_pipe_size=0 agent.unified_cgroup_hierarchy=0", @@ -717,6 +802,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "agent.devmode agent.debug_console agent.container_pip_siz=100 agent.unified_cgroup_hierarchy=1", @@ -728,6 +814,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: true, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "", @@ -739,6 +826,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "", @@ -750,6 +838,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: "foo", unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "", @@ -761,6 +850,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: "=", unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "", @@ -772,6 +862,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: "=foo", unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "", @@ -783,6 +874,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: "foo=bar=baz=", unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "", @@ -794,6 +886,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: "unix:///tmp/foo.socket", unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "", @@ -805,6 +898,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: "unix://@/tmp/foo.socket", unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "", @@ -816,6 +910,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "", @@ -827,6 +922,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "", @@ -838,6 +934,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "", @@ -849,6 +946,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "server_addr=unix:///tmp/foo.socket", @@ -860,6 +958,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "agent.server_address=unix:///tmp/foo.socket", @@ -871,6 +970,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: TEST_SERVER_ADDR, unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: "agent.server_addr=unix:///tmp/foo.socket", @@ -882,6 +982,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: "unix:///tmp/foo.socket", unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: " agent.server_addr=unix:///tmp/foo.socket", @@ -893,6 +994,7 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: "unix:///tmp/foo.socket", unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, TestData { contents: " agent.server_addr=unix:///tmp/foo.socket a", @@ -904,6 +1006,115 @@ mod tests { container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, server_addr: "unix:///tmp/foo.socket", unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, + }, + TestData { + contents: "trace", + env_vars: Vec::new(), + debug_console: false, + dev_mode: false, + log_level: DEFAULT_LOG_LEVEL, + hotplug_timeout: DEFAULT_HOTPLUG_TIMEOUT, + container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, + server_addr: TEST_SERVER_ADDR, + unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, + }, + TestData { + contents: ".trace", + env_vars: Vec::new(), + debug_console: false, + dev_mode: false, + log_level: DEFAULT_LOG_LEVEL, + hotplug_timeout: DEFAULT_HOTPLUG_TIMEOUT, + container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, + server_addr: TEST_SERVER_ADDR, + unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, + }, + TestData { + contents: "agent.tracer", + env_vars: Vec::new(), + debug_console: false, + dev_mode: false, + log_level: DEFAULT_LOG_LEVEL, + hotplug_timeout: DEFAULT_HOTPLUG_TIMEOUT, + container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, + server_addr: TEST_SERVER_ADDR, + unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, + }, + TestData { + contents: "agent.trac", + env_vars: Vec::new(), + debug_console: false, + dev_mode: false, + log_level: DEFAULT_LOG_LEVEL, + hotplug_timeout: DEFAULT_HOTPLUG_TIMEOUT, + container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, + server_addr: TEST_SERVER_ADDR, + unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, + }, + TestData { + contents: "agent.trace", + env_vars: Vec::new(), + debug_console: false, + dev_mode: false, + log_level: DEFAULT_LOG_LEVEL, + hotplug_timeout: DEFAULT_HOTPLUG_TIMEOUT, + container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, + server_addr: TEST_SERVER_ADDR, + unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Isolated, + }, + TestData { + contents: "agent.trace=isolated", + env_vars: Vec::new(), + debug_console: false, + dev_mode: false, + log_level: DEFAULT_LOG_LEVEL, + hotplug_timeout: DEFAULT_HOTPLUG_TIMEOUT, + container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, + server_addr: TEST_SERVER_ADDR, + unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Isolated, + }, + TestData { + contents: "agent.trace=disabled", + env_vars: Vec::new(), + debug_console: false, + dev_mode: false, + log_level: DEFAULT_LOG_LEVEL, + hotplug_timeout: DEFAULT_HOTPLUG_TIMEOUT, + container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, + server_addr: TEST_SERVER_ADDR, + unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, + }, + TestData { + contents: "", + env_vars: vec!["KATA_AGENT_TRACE_TYPE=isolated"], + debug_console: false, + dev_mode: false, + log_level: DEFAULT_LOG_LEVEL, + hotplug_timeout: DEFAULT_HOTPLUG_TIMEOUT, + container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, + server_addr: TEST_SERVER_ADDR, + unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Isolated, + }, + TestData { + contents: "", + env_vars: vec!["KATA_AGENT_TRACE_TYPE=disabled"], + debug_console: false, + dev_mode: false, + log_level: DEFAULT_LOG_LEVEL, + hotplug_timeout: DEFAULT_HOTPLUG_TIMEOUT, + container_pipe_size: DEFAULT_CONTAINER_PIPE_SIZE, + server_addr: TEST_SERVER_ADDR, + unified_cgroup_hierarchy: false, + tracing: tracer::TraceType::Disabled, }, ]; @@ -958,6 +1169,7 @@ mod tests { ); assert_eq!(config.container_pipe_size, 0, "{}", msg); assert_eq!(config.server_addr, TEST_SERVER_ADDR, "{}", msg); + assert_eq!(config.tracing, tracer::TraceType::Disabled, "{}", msg); let result = config.parse_cmdline(filename); assert!(result.is_ok(), "{}", msg); @@ -973,6 +1185,7 @@ mod tests { assert_eq!(d.hotplug_timeout, config.hotplug_timeout, "{}", msg); assert_eq!(d.container_pipe_size, config.container_pipe_size, "{}", msg); assert_eq!(d.server_addr, config.server_addr, "{}", msg); + assert_eq!(d.tracing, config.tracing, "{}", msg); for v in vars_to_unset { env::remove_var(v); @@ -1369,4 +1582,62 @@ mod tests { assert_result!(d.result, result, msg); } } + + #[test] + fn test_get_trace_type() { + #[derive(Debug)] + struct TestData<'a> { + param: &'a str, + result: Result, + } + + let tests = &[ + TestData { + param: "", + result: Err(make_err(ERR_INVALID_TRACE_TYPE_PARAM)), + }, + TestData { + param: "agent.tracer", + result: Err(make_err(ERR_INVALID_TRACE_TYPE_KEY)), + }, + TestData { + param: "agent.trac", + result: Err(make_err(ERR_INVALID_TRACE_TYPE_KEY)), + }, + TestData { + param: "agent.trace=", + result: Err(make_err(ERR_INVALID_TRACE_TYPE)), + }, + TestData { + param: "agent.trace==", + result: Err(make_err(ERR_INVALID_TRACE_TYPE)), + }, + TestData { + param: "agent.trace=foo", + result: Err(make_err(ERR_INVALID_TRACE_TYPE)), + }, + TestData { + param: "agent.trace", + result: Ok(tracer::TraceType::Isolated), + }, + TestData { + param: "agent.trace=isolated", + result: Ok(tracer::TraceType::Isolated), + }, + TestData { + param: "agent.trace=disabled", + result: Ok(tracer::TraceType::Disabled), + }, + ]; + + for (i, d) in tests.iter().enumerate() { + let msg = format!("test[{}]: {:?}", i, d); + + let result = get_trace_type(d.param); + + let msg = format!("{}: result: {:?}", msg, result); + + assert_result!(d.result, result, msg); + } + } } 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 1d5fbbbb5b..8adea7a6f2 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, span}; mod config; mod console; @@ -55,7 +56,7 @@ mod version; use mount::{cgroups_mount, general_mount}; use sandbox::Sandbox; use signal::setup_signal_handler; -use slog::Logger; +use slog::{error, info, o, warn, Logger}; use uevent::watch_uevents; use futures::future::join_all; @@ -70,6 +71,7 @@ use tokio::{ }; mod rpc; +mod tracer; const NAME: &str = "kata-agent"; const KERNEL_CMDLINE_FILE: &str = "/proc/cmdline"; @@ -79,6 +81,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, @@ -199,6 +202,17 @@ async fn real_main() -> std::result::Result<(), Box> { ttrpc_log_guard = Ok(slog_stdlog::init().map_err(|e| e)?); } + if config.tracing != tracer::TraceType::Disabled { + let _ = tracer::setup_tracing(NAME, &logger, &config)?; + } + + let root = span!(tracing::Level::TRACE, "root-span", work_units = 2); + + // XXX: Start the root trace transaction. + // + // XXX: Note that *ALL* spans needs to start after this point!! + let _enter = root.enter(); + // Start the sandbox and wait for its ttRPC server to end start_sandbox(&logger, &config, init_mode, &mut tasks, shutdown_rx.clone()).await?; @@ -227,6 +241,10 @@ async fn real_main() -> std::result::Result<(), Box> { } } + if config.tracing != tracer::TraceType::Disabled { + tracer::end_tracing(); + } + eprintln!("{} shutdown complete", NAME); Ok(()) @@ -260,6 +278,7 @@ fn main() -> std::result::Result<(), Box> { rt.block_on(real_main()) } +#[instrument] async fn start_sandbox( logger: &Logger, config: &AgentConfig, @@ -346,6 +365,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/tracer.rs b/src/agent/src/tracer.rs new file mode 100644 index 0000000000..4ae5111eb4 --- /dev/null +++ b/src/agent/src/tracer.rs @@ -0,0 +1,91 @@ +// Copyright (c) 2020-2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +use crate::config::AgentConfig; +use anyhow::Result; +use opentelemetry::{global, sdk::trace::Config, trace::TracerProvider}; +use slog::{info, o, Logger}; +use std::error::Error; +use std::fmt; +use std::str::FromStr; +use tracing_opentelemetry::OpenTelemetryLayer; +use tracing_subscriber::layer::SubscriberExt; +use tracing_subscriber::Registry; + +#[derive(Debug, PartialEq)] +pub enum TraceType { + Disabled, + Isolated, +} + +#[derive(Debug)] +pub struct TraceTypeError { + details: String, +} + +impl TraceTypeError { + fn new(msg: &str) -> TraceTypeError { + TraceTypeError { + details: msg.into(), + } + } +} + +impl Error for TraceTypeError {} + +impl fmt::Display for TraceTypeError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.details) + } +} + +impl FromStr for TraceType { + type Err = TraceTypeError; + + fn from_str(s: &str) -> Result { + match s { + "isolated" => Ok(TraceType::Isolated), + "disabled" => Ok(TraceType::Disabled), + _ => Err(TraceTypeError::new("invalid trace type")), + } + } +} + +pub fn setup_tracing(name: &'static str, logger: &Logger, _agent_cfg: &AgentConfig) -> Result<()> { + let logger = logger.new(o!("subsystem" => "vsock-tracer")); + + let exporter = vsock_exporter::Exporter::builder() + .with_logger(&logger) + .init(); + + let config = Config::default(); + + let builder = opentelemetry::sdk::trace::TracerProvider::builder() + .with_simple_exporter(exporter) + .with_config(config); + + let provider = builder.build(); + + // We don't need a versioned tracer. + let version = None; + + let tracer = provider.get_tracer(name, version); + + let _global_provider = global::set_tracer_provider(provider); + + let layer = OpenTelemetryLayer::new(tracer); + + let subscriber = Registry::default().with(layer); + + tracing::subscriber::set_global_default(subscriber)?; + + info!(logger, "tracing setup"); + + Ok(()) +} + +pub fn end_tracing() { + global::shutdown_tracer_provider(); +} 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) diff --git a/src/agent/vsock-exporter/Cargo.toml b/src/agent/vsock-exporter/Cargo.toml new file mode 100644 index 0000000000..b91ca4f18c --- /dev/null +++ b/src/agent/vsock-exporter/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "vsock-exporter" +version = "0.1.0" +authors = ["James O. D. Hunt "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +nix = "0.20.0" +libc = "0.2.94" +thiserror = "1.0.24" +opentelemetry = { version = "0.14.0", features=["serialize"] } +serde = { version = "1.0.126", features = ["derive"] } +vsock = "0.2.3" +bincode = "1.3.3" +byteorder = "1.4.3" +slog = { version = "2.5.2", features = ["dynamic-keys", "max_level_trace", "release_max_level_info"] } +async-trait = "0.1.50" diff --git a/src/agent/vsock-exporter/src/lib.rs b/src/agent/vsock-exporter/src/lib.rs new file mode 100644 index 0000000000..99aaf341bb --- /dev/null +++ b/src/agent/vsock-exporter/src/lib.rs @@ -0,0 +1,196 @@ +// Copyright (c) 2020-2021 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +// The VSOCK Exporter sends trace spans "out" to the forwarder running on the +// host (which then forwards them on to a trace collector). The data is sent +// via a VSOCK socket that the forwarder process is listening on. To allow the +// forwarder to know how much data to each for each trace span the simplest +// protocol is employed which uses a header packet and the payload (trace +// span) data. The header packet is a simple count of the number of bytes in the +// payload, which allows the forwarder to know how many bytes it must read to +// consume the trace span. The payload is a serialised version of the trace span. + +use async_trait::async_trait; +use byteorder::{ByteOrder, NetworkEndian}; +use opentelemetry::sdk::export::trace::{ExportResult, SpanData, SpanExporter}; +use opentelemetry::sdk::export::ExportError; +use slog::{error, o, Logger}; +use std::io::{ErrorKind, Write}; +use std::net::Shutdown; +use std::sync::Mutex; +use vsock::{SockAddr, VsockStream}; + +const ANY_CID: &str = "any"; + +// Must match the value of the variable of the same name in the trace forwarder. +const HEADER_SIZE_BYTES: u64 = std::mem::size_of::() as u64; + +// By default, the VSOCK exporter should talk "out" to the host where the +// forwarder is running. +const DEFAULT_CID: u32 = libc::VMADDR_CID_HOST; + +// The VSOCK port the forwarders listens on by default +const DEFAULT_PORT: u32 = 10240; + +#[derive(Debug)] +pub struct Exporter { + port: u32, + cid: u32, + conn: Mutex, + logger: Logger, +} + +impl Exporter { + /// Create a new exporter builder. + pub fn builder() -> Builder { + Builder::default() + } +} + +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error("connection error: {0}")] + ConnectionError(String), + #[error("serialisation error: {0}")] + SerialisationError(#[from] bincode::Error), + #[error("I/O error: {0}")] + IOError(#[from] std::io::Error), +} + +impl ExportError for Error { + fn exporter_name(&self) -> &'static str { + "vsock-exporter" + } +} + +fn make_io_error(desc: String) -> std::io::Error { + std::io::Error::new(ErrorKind::Other, desc) +} + +// Send a trace span to the forwarder running on the host. +fn write_span(writer: &mut dyn Write, span: &SpanData) -> Result<(), std::io::Error> { + let encoded_payload: Vec = + bincode::serialize(&span).map_err(|e| make_io_error(e.to_string()))?; + + let payload_len: u64 = encoded_payload.len() as u64; + + let mut payload_len_as_bytes: [u8; HEADER_SIZE_BYTES as usize] = + [0; HEADER_SIZE_BYTES as usize]; + + // Encode the header + NetworkEndian::write_u64(&mut payload_len_as_bytes, payload_len); + + // Send the header + writer + .write_all(&payload_len_as_bytes) + .map_err(|e| make_io_error(format!("failed to write trace header: {:?}", e)))?; + + writer + .write_all(&encoded_payload) + .map_err(|e| make_io_error(format!("failed to write trace payload: {:?}", e))) +} + +fn handle_batch(writer: &mut dyn Write, batch: Vec) -> ExportResult { + for span_data in batch { + write_span(writer, &span_data).map_err(Error::IOError)?; + } + + Ok(()) +} + +#[async_trait] +impl SpanExporter for Exporter { + async fn export(&mut self, batch: Vec) -> ExportResult { + let conn = self.conn.lock(); + + match conn { + Ok(mut c) => handle_batch(&mut *c, batch), + Err(e) => { + error!(self.logger, "failed to obtain connection"; + "error" => format!("{}", e)); + + return Err(Error::ConnectionError(e.to_string()).into()); + } + } + } + + fn shutdown(&mut self) { + let conn = match self.conn.lock() { + Ok(conn) => conn, + Err(e) => { + error!(self.logger, "failed to obtain connection"; + "error" => format!("{}", e)); + return; + } + }; + + conn.shutdown(Shutdown::Write) + .expect("failed to shutdown VSOCK connection"); + } +} + +#[derive(Debug)] +pub struct Builder { + port: u32, + cid: u32, + logger: Logger, +} + +impl Default for Builder { + fn default() -> Self { + let logger = Logger::root(slog::Discard, o!()); + + Builder { + cid: DEFAULT_CID, + port: DEFAULT_PORT, + logger, + } + } +} + +impl Builder { + pub fn with_cid(self, cid: u32) -> Self { + Builder { cid, ..self } + } + + pub fn with_port(self, port: u32) -> Self { + Builder { port, ..self } + } + + pub fn with_logger(self, logger: &Logger) -> Self { + Builder { + logger: logger.new(o!()), + ..self + } + } + + pub fn init(self) -> Exporter { + let Builder { port, cid, logger } = self; + + let sock_addr = SockAddr::new_vsock(self.cid, self.port); + + let cid_str: String; + + if self.cid == libc::VMADDR_CID_ANY { + cid_str = ANY_CID.to_string(); + } else { + cid_str = format!("{}", self.cid); + } + + let msg = format!( + "failed to connect to VSOCK server (port: {}, cid: {}) - {}", + self.port, cid_str, "ensure trace forwarder is running on host" + ); + + let conn = VsockStream::connect(&sock_addr).expect(&msg); + + Exporter { + port, + cid, + conn: Mutex::new(conn), + logger: logger.new(o!("cid" => cid_str, "port" => port)), + } + } +} diff --git a/src/trace-forwarder/Cargo.lock b/src/trace-forwarder/Cargo.lock index bf0be35b92..b6d4c1beac 100644 --- a/src/trace-forwarder/Cargo.lock +++ b/src/trace-forwarder/Cargo.lock @@ -1,14 +1,5 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -[[package]] -name = "aho-corasick" -version = "0.7.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "043164d8ba5c4c3035fec9bbee8647c0261d788f3474306f93bb65901cae0e86" -dependencies = [ - "memchr", -] - [[package]] name = "ansi_term" version = "0.11.0" @@ -18,6 +9,15 @@ dependencies = [ "winapi", ] +[[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.31" @@ -30,6 +30,17 @@ version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d25d88fd6b8041580a654f9d0c581a047baee2b3efee13275f2fc392fc75034" +[[package]] +name = "async-trait" +version = "0.1.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b98e84bbb4cbcdd97da190ba0c58a1bb0de2c1fdf67d159e192ed766aeca722" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "atty" version = "0.2.14" @@ -49,11 +60,10 @@ checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" [[package]] name = "bincode" -version = "1.3.1" +version = "1.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f30d3a39baa26f9651f17b375061f3233dde33424a8b72b0dbe93a68a0bc896d" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" dependencies = [ - "byteorder", "serde", ] @@ -64,10 +74,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" [[package]] -name = "byteorder" -version = "1.3.4" +name = "bumpalo" +version = "3.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" +checksum = "63396b8a4b9de3f4fdfb320ab6080762242f66a8ef174c49d8e19b674db4cdbe" + +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" [[package]] name = "cc" @@ -82,14 +98,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" [[package]] -name = "chrono" -version = "0.4.11" +name = "cfg-if" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "80094f509cf8b5ae86a4966a39b3ff66cd7e2a3e594accec3743ff3fabeab5b2" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "chrono" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" dependencies = [ + "libc", "num-integer", "num-traits", "time", + "winapi", ] [[package]] @@ -98,7 +122,7 @@ version = "2.33.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bdfa80d47f954d53a35a64987ca1422f495b8d6483c0fe9f7117b36c2a792129" dependencies = [ - "ansi_term", + "ansi_term 0.11.0", "atty", "bitflags", "strsim", @@ -113,10 +137,20 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cced8691919c02aac3cb0a1bc2e9b73d89e832bf9a06fc579d4e71b68a2da061" dependencies = [ - "crossbeam-utils", + "crossbeam-utils 0.7.2", "maybe-uninit", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06ed27e177f16d65f0f0c22a213e17c696ace5dd64b14258b52f9417ccb52db4" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-utils 0.8.4", +] + [[package]] name = "crossbeam-utils" version = "0.7.2" @@ -124,21 +158,26 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" dependencies = [ "autocfg", - "cfg-if", + "cfg-if 0.1.10", "lazy_static", ] [[package]] -name = "fnv" -version = "1.0.7" +name = "crossbeam-utils" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +checksum = "4feb231f0d4d6af81aed15928e58ecf5816aa62a2393e2c82f46973e92a9a278" +dependencies = [ + "autocfg", + "cfg-if 1.0.0", + "lazy_static", +] [[package]] name = "futures" -version = "0.3.5" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e05b85ec287aac0dc34db7d4a569323df697f9c55b99b15d6b4ef8cde49f613" +checksum = "0e7e43a803dae2fa37c1f6a8fe121e1f7bf9548b4dfc0522a42f34145dadfc27" dependencies = [ "futures-channel", "futures-core", @@ -151,9 +190,9 @@ dependencies = [ [[package]] name = "futures-channel" -version = "0.3.5" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f366ad74c28cca6ba456d95e6422883cfb4b252a83bed929c83abfdbbf2967d5" +checksum = "e682a68b29a882df0545c143dc3646daefe80ba479bcdede94d5a703de2871e2" dependencies = [ "futures-core", "futures-sink", @@ -161,15 +200,15 @@ dependencies = [ [[package]] name = "futures-core" -version = "0.3.5" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59f5fff90fd5d971f936ad674802482ba441b6f09ba5e15fd8b39145582ca399" +checksum = "0402f765d8a89a26043b889b26ce3c4679d268fa6bb22cd7c6aad98340e179d1" [[package]] name = "futures-executor" -version = "0.3.5" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10d6bb888be1153d3abeb9006b11b02cf5e9b209fda28693c31ae1e4e012e314" +checksum = "badaa6a909fac9e7236d0620a2f57f7664640c56575b71a7552fbd68deafab79" dependencies = [ "futures-core", "futures-task", @@ -178,16 +217,17 @@ dependencies = [ [[package]] name = "futures-io" -version = "0.3.5" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de27142b013a8e869c14957e6d2edeef89e97c289e69d042ee3a49acd8b51789" +checksum = "acc499defb3b348f8d8f3f66415835a9131856ff7714bf10dadfc4ec4bdb29a1" [[package]] name = "futures-macro" -version = "0.3.5" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0b5a30a4328ab5473878237c447333c093297bded83a4983d10f4deea240d39" +checksum = "a4c40298486cdf52cc00cd6d6987892ba502c7656a16a4192a9992b1ccedd121" dependencies = [ + "autocfg", "proc-macro-hack", "proc-macro2", "quote", @@ -196,25 +236,23 @@ dependencies = [ [[package]] name = "futures-sink" -version = "0.3.5" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f2032893cb734c7a05d85ce0cc8b8c4075278e93b24b66f9de99d6eb0fa8acc" +checksum = "a57bead0ceff0d6dde8f465ecd96c9338121bb7717d3e7b108059531870c4282" [[package]] name = "futures-task" -version = "0.3.5" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bdb66b5f09e22019b1ab0830f7785bcea8e7a42148683f99214f73f8ec21a626" -dependencies = [ - "once_cell", -] +checksum = "8a16bef9fc1a4dddb5bee51c989e3fbba26569cbb0e31f5b303c184e3dd33dae" [[package]] name = "futures-util" -version = "0.3.5" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8764574ff08b701a084482c3c7031349104b07ac897393010494beaa18ce32c6" +checksum = "feb5c238d27e2bf94ffdfd27b2c29e3df4a68c4193bb6427384259e2bf191967" dependencies = [ + "autocfg", "futures-channel", "futures-core", "futures-io", @@ -222,7 +260,7 @@ dependencies = [ "futures-sink", "futures-task", "memchr", - "pin-project", + "pin-project-lite", "pin-utils", "proc-macro-hack", "proc-macro-nested", @@ -235,9 +273,20 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "libc", - "wasi", + "wasi 0.9.0+wasi-snapshot-preview1", +] + +[[package]] +name = "getrandom" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" +dependencies = [ + "cfg-if 1.0.0", + "libc", + "wasi 0.10.2+wasi-snapshot-preview1", ] [[package]] @@ -261,6 +310,15 @@ version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" +[[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-trace-forwarder" version = "0.0.1" @@ -269,9 +327,10 @@ dependencies = [ "bincode", "byteorder", "clap", + "futures", "libc", "logging", - "nix", + "nix 0.20.0", "opentelemetry", "opentelemetry-jaeger", "protobuf", @@ -293,9 +352,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.71" +version = "0.2.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49" +checksum = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e" [[package]] name = "log" @@ -303,7 +362,7 @@ version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", ] [[package]] @@ -340,15 +399,26 @@ checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" [[package]] name = "nix" -version = "0.15.0" +version = "0.19.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b2e0b4f3320ed72aaedb9a5ac838690a8047c7b275da22711fddff4f8a14229" +checksum = "b2ccba0cfe4fdf15982d1674c69b1fd80bad427d293849982668dfe454bd61f2" dependencies = [ "bitflags", "cc", - "cfg-if", + "cfg-if 1.0.0", + "libc", +] + +[[package]] +name = "nix" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa9b4819da1bc61c0ea48b63b7bc8604064dd43013e7cc325df098d49cd7c18a" +dependencies = [ + "bitflags", + "cc", + "cfg-if 1.0.0", "libc", - "void", ] [[package]] @@ -380,35 +450,34 @@ dependencies = [ "libc", ] -[[package]] -name = "once_cell" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b631f7e854af39a1739f401cf34a8a013dfe09eac4fa4dba91e9768bd28168d" - [[package]] name = "opentelemetry" -version = "0.5.0" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1dbc1028f1f215373e28473238527c8799fbe88c58bae255585602c7316fa30" +checksum = "492848ff47f11b7f9de0443b404e2c5775f695e1af6b7076ca25f999581d547a" dependencies = [ - "bincode", + "async-trait", + "crossbeam-channel 0.5.1", "futures", + "js-sys", "lazy_static", "percent-encoding", "pin-project", - "prometheus", - "rand", + "rand 0.8.3", "serde", + "thiserror", ] [[package]] name = "opentelemetry-jaeger" -version = "0.4.0" +version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4da8b4162e8ae52c894f23895477f0d0340d34d8d35c367edf53de83adb1a6df" +checksum = "97fd9ed34f208e0394bfb17522ba0d890925685dfd883147670ed474339d4647" dependencies = [ + "async-trait", + "lazy_static", "opentelemetry", + "thiserror", "thrift", ] @@ -429,24 +498,30 @@ checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" [[package]] name = "pin-project" -version = "0.4.22" +version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12e3a6cdbfe94a5e4572812a0201f8c0ed98c1c452c7b8563ce2276988ef9c17" +checksum = "c7509cc106041c40a4518d2af7a61530e1eed0e6285296a3d8c5472806ccc4a4" dependencies = [ "pin-project-internal", ] [[package]] name = "pin-project-internal" -version = "0.4.22" +version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a0ffd45cf79d88737d7cc85bfd5d2894bee1139b356e616fe85dc389c61aaf7" +checksum = "48c950132583b500556b1efd71d45b319029f2b71518d979fcc208e16b42426f" dependencies = [ "proc-macro2", "quote", "syn", ] +[[package]] +name = "pin-project-lite" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc0e1f259c92177c30a4c9d177246edd0a3568b25756a977d0632cf8fa37e905" + [[package]] name = "pin-utils" version = "0.1.0" @@ -461,9 +536,9 @@ checksum = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea" [[package]] name = "proc-macro-hack" -version = "0.5.16" +version = "0.5.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e0456befd48169b9f13ef0f0ad46d492cf9d2dbb918bcf38e01eed4ce3ec5e4" +checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" [[package]] name = "proc-macro-nested" @@ -473,39 +548,19 @@ checksum = "eba180dafb9038b050a4c280019bbedf9f2467b61e5d892dcad585bb57aadc5a" [[package]] name = "proc-macro2" -version = "1.0.18" +version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "beae6331a816b1f65d04c45b078fd8e6c93e8071771f41b8163255bbd8d7c8fa" +checksum = "f0d8caf72986c1a598726adc988bb5984792ef84f5ee5aa50209145ee8077038" dependencies = [ "unicode-xid", ] -[[package]] -name = "prometheus" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5567486d5778e2c6455b1b90ff1c558f29e751fc018130fa182e15828e728af1" -dependencies = [ - "cfg-if", - "fnv", - "lazy_static", - "protobuf", - "quick-error", - "spin", -] - [[package]] name = "protobuf" version = "2.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e86d370532557ae7573551a1ec8235a0f8d6cb276c7c9e6aa490b511c447485" -[[package]] -name = "quick-error" -version = "1.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" - [[package]] name = "quote" version = "1.0.7" @@ -521,11 +576,23 @@ version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" dependencies = [ - "getrandom", + "getrandom 0.1.14", "libc", - "rand_chacha", - "rand_core", - "rand_hc", + "rand_chacha 0.2.2", + "rand_core 0.5.1", + "rand_hc 0.2.0", +] + +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "libc", + "rand_chacha 0.3.0", + "rand_core 0.6.2", + "rand_hc 0.3.0", ] [[package]] @@ -535,7 +602,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.5.1", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.2", ] [[package]] @@ -544,7 +621,16 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" dependencies = [ - "getrandom", + "getrandom 0.1.14", +] + +[[package]] +name = "rand_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +dependencies = [ + "getrandom 0.2.3", ] [[package]] @@ -553,7 +639,16 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" dependencies = [ - "rand_core", + "rand_core 0.5.1", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core 0.6.2", ] [[package]] @@ -568,10 +663,7 @@ version = "1.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c3780fcf44b193bc4d09f36d2a3c87b251da4a046c87795a0d35f4f927ad8e6" dependencies = [ - "aho-corasick", - "memchr", "regex-syntax", - "thread_local", ] [[package]] @@ -607,18 +699,18 @@ checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" [[package]] name = "serde" -version = "1.0.114" +version = "1.0.126" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5317f7588f0a5078ee60ef675ef96735a1442132dc645eb1d12c018620ed8cd3" +checksum = "ec7505abeacaec74ae4778d9d9328fe5a5d04253220a85c4ee022239fc996d03" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.114" +version = "1.0.126" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a0be94b04690fbaed37cddffc5c134bf537c8e3329d53e982fe04c374978f8e" +checksum = "963a7dbc9895aeac7ac90e74f34a5d5261828f79df35cbed41e10189d3804d43" dependencies = [ "proc-macro2", "quote", @@ -638,9 +730,9 @@ dependencies = [ [[package]] name = "sharded-slab" -version = "0.0.9" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06d5a3f5166fb5b42a5439f2eee8b9de149e235961e3eb21c5808fc3ea17ff3e" +checksum = "79c719719ee05df97490f80a45acfc99e5a30ce98a1e4fb67aee422745ae14e3" dependencies = [ "lazy_static", ] @@ -663,7 +755,7 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51b3336ce47ce2f96673499fc07eb85e3472727b9a7a2959964b002c2ce8fbbb" dependencies = [ - "crossbeam-channel", + "crossbeam-channel 0.4.2", "slog", "take_mut", "thread_local", @@ -698,12 +790,6 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c7cb5678e1615754284ec264d9bb5b4c27d2018577fd90ac0ceb578591ed5ee4" -[[package]] -name = "spin" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" - [[package]] name = "strsim" version = "0.8.0" @@ -712,9 +798,9 @@ checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" [[package]] name = "syn" -version = "1.0.33" +version = "1.0.72" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8d5d96e8cbb005d6959f119f773bfaebb5684296108fb32600c00cde305b2cd" +checksum = "a1e8cdbefb79a9a5a65e0db8b47b723ee907b7c7f8496c76a1770b5c310bab82" dependencies = [ "proc-macro2", "quote", @@ -733,9 +819,9 @@ version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "libc", - "rand", + "rand 0.7.3", "redox_syscall", "remove_dir_all", "winapi", @@ -750,6 +836,26 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "thiserror" +version = "1.0.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa6f76457f59514c7eeb4e59d891395fab0b2fd1d40723ae737d64153392e9c6" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a36768c0fbf1bb15eca10defa29526bda730a2376c2ab4393ccfa16fb1a318d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "thread_local" version = "1.0.1" @@ -793,20 +899,21 @@ dependencies = [ [[package]] name = "tracing" -version = "0.1.15" +version = "0.1.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a41f40ed0e162c911ac6fcb53ecdc8134c46905fdbbae8c50add462a538b495f" +checksum = "09adeb8c97449311ccd28a427f96fb563e7fd31aabf994189879d9da2394b89d" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", + "pin-project-lite", "tracing-attributes", "tracing-core", ] [[package]] name = "tracing-attributes" -version = "0.1.8" +version = "0.1.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99bbad0de3fd923c9c3232ead88510b783e5a4d16a6154adffa3d53308de984c" +checksum = "c42e6fa53307c8a17e4ccd4dc81cf5ec38db9209f59b222210375b54ee40d1e2" dependencies = [ "proc-macro2", "quote", @@ -815,18 +922,18 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.10" +version = "0.1.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0aa83a9a47081cd522c09c81b31aec2c9273424976f922ad61c053b58350b715" +checksum = "a9ff14f98b1a4b289c6248a023c1c2fa1491062964e9fed67ab29c4e4da4a052" dependencies = [ "lazy_static", ] [[package]] name = "tracing-log" -version = "0.1.1" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e0f8c7178e13481ff6765bd169b33e8d554c5d2bbede5e32c356194be02b9b9" +checksum = "a6923477a48e41c1951f1999ef8bb5a3023eb723ceadafe78ffb65dc366761e3" dependencies = [ "lazy_static", "log", @@ -835,22 +942,22 @@ dependencies = [ [[package]] name = "tracing-opentelemetry" -version = "0.4.0" +version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d79fdf3c82e6ea14c0f54cc9418aef57252150c6da0bc5f18553f0136f0dcd31" +checksum = "e2f4cb277b92a8ba1170b3b911056428ce2ef9993351baf5965bb0359a2e5963" dependencies = [ "opentelemetry", - "rand", "tracing", "tracing-core", + "tracing-log", "tracing-subscriber", ] [[package]] name = "tracing-serde" -version = "0.1.1" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6ccba2f8f16e0ed268fc765d9b7ff22e965e7185d32f8f1ec8294fe17d86e79" +checksum = "fb65ea441fbb84f9f6748fd496cf7f63ec9af5bca94dd86456978d055e8eb28b" dependencies = [ "serde", "tracing-core", @@ -858,11 +965,11 @@ dependencies = [ [[package]] name = "tracing-subscriber" -version = "0.2.6" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04a11b459109e38ff6e1b580bafef4142a11d44889f5d07424cbce2fd2a2a119" +checksum = "aa5553bf0883ba7c9cbe493b085c29926bd41b66afc31ff72cf17ff4fb60dcd5" dependencies = [ - "ansi_term", + "ansi_term 0.12.1", "chrono", "lazy_static", "matchers", @@ -871,6 +978,8 @@ dependencies = [ "serde_json", "sharded-slab", "smallvec", + "thread_local", + "tracing", "tracing-core", "tracing-log", "tracing-serde", @@ -894,20 +1003,14 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" -[[package]] -name = "void" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" - [[package]] name = "vsock" -version = "0.1.5" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63b4354dabee252603a8b1a63e4adb3934dfbf1ff05ef4c3d653c2dfd67f0788" +checksum = "50e2ef09834e1d203d24556512c0e58e66de203440bd9d74c30a33f7240091c6" dependencies = [ "libc", - "nix", + "nix 0.19.1", ] [[package]] @@ -916,6 +1019,66 @@ version = "0.9.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" +[[package]] +name = "wasi" +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", + "quote", + "syn", + "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", + "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", + "quote", + "syn", + "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 = "winapi" version = "0.3.9" diff --git a/src/trace-forwarder/Cargo.toml b/src/trace-forwarder/Cargo.toml index 6a565970ff..b2344a51e6 100644 --- a/src/trace-forwarder/Cargo.toml +++ b/src/trace-forwarder/Cargo.toml @@ -10,21 +10,22 @@ authors = ["The Kata Containers community "] edition = "2018" [dependencies] +futures = "0.3.15" clap = "2.33.0" -vsock = "0.1.5" -nix = "0.15.0" -libc = "0.2.66" -serde = { version = "1.0.106", features = ["derive"] } -bincode = "1.2.1" -byteorder = "1.3.4" +vsock = "0.2.3" +nix = "0.20.0" +libc = "0.2.94" +serde = { version = "1.0.126", features = ["derive"] } +bincode = "1.3.3" +byteorder = "1.4.3" serde_json = "1.0.44" anyhow = "1.0.31" -opentelemetry = { version = "0.5.0", features=["serialize"] } -opentelemetry-jaeger = "0.4.0" +opentelemetry = { version = "0.14.0", features=["serialize"] } +opentelemetry-jaeger = "0.13.0" protobuf = "=2.14.0" -tracing-opentelemetry = "0.4.0" -tracing = "0.1.14" -tracing-subscriber = "0.2.5" +tracing-opentelemetry = "0.13.0" +tracing = "0.1.26" +tracing-subscriber = "0.2.18" logging = { path = "../../pkg/logging" } slog = "2.5.2" diff --git a/src/trace-forwarder/src/handler.rs b/src/trace-forwarder/src/handler.rs index ced7f7aec5..377ee0e82d 100644 --- a/src/trace-forwarder/src/handler.rs +++ b/src/trace-forwarder/src/handler.rs @@ -3,13 +3,12 @@ // SPDX-License-Identifier: Apache-2.0 // +use anyhow::{anyhow, Context, Result}; use byteorder::{ByteOrder, NetworkEndian}; -use opentelemetry::exporter::trace::SpanData; -use opentelemetry::exporter::trace::{ExportResult, SpanExporter}; +use opentelemetry::sdk::export::trace::{SpanData, SpanExporter}; use slog::{debug, info, o, Logger}; use std::io::{ErrorKind, Read}; use std::net::Shutdown; -use std::sync::Arc; use vsock::VsockStream; // The VSOCK "packet" protocol used comprises two elements: @@ -20,23 +19,27 @@ use vsock::VsockStream; // This constant defines the number of bytes used to encode the header on the // wire. In other words, the first 64-bits of the packet contain a number // specifying how many bytes are in the remainder of the packet. +// +// Must match the value of the variable of the same name in the agents +// vsock-exporter. const HEADER_SIZE_BYTES: u64 = std::mem::size_of::() as u64; fn mk_io_err(msg: &str) -> std::io::Error { std::io::Error::new(std::io::ErrorKind::Other, msg.to_string()) } -pub fn handle_connection( +pub async fn handle_connection<'a>( logger: Logger, mut conn: VsockStream, - exporter: &dyn SpanExporter, -) -> Result<(), std::io::Error> { + exporter: &'a mut dyn SpanExporter, +) -> Result<()> { let logger = logger.new(o!("subsystem" => "handler", "connection" => format!("{:?}", conn))); debug!(logger, "handling connection"); handle_trace_data(logger.clone(), &mut conn, exporter) + .await .map_err(|e| mk_io_err(&format!("failed to handle data: {:}", e)))?; debug!(&logger, "handled data"); @@ -49,11 +52,11 @@ pub fn handle_connection( Ok(()) } -fn handle_trace_data( +async fn handle_trace_data<'a>( logger: Logger, - reader: &mut dyn Read, - exporter: &dyn SpanExporter, -) -> Result<(), String> { + reader: &'a mut dyn Read, + exporter: &'a mut dyn SpanExporter, +) -> Result<()> { loop { let mut header: [u8; HEADER_SIZE_BYTES as usize] = [0; HEADER_SIZE_BYTES as usize]; @@ -67,7 +70,7 @@ fn handle_trace_data( break; } - return Err(format!("failed to read header: {:}", e)); + return Err(anyhow!("failed to read header: {:}", e)); } }; @@ -78,7 +81,7 @@ fn handle_trace_data( reader .read_exact(&mut encoded_payload) - .map_err(|e| format!("failed to read payload: {:}", e))?; + .with_context(|| format!("failed to read payload"))?; debug!(logger, "read payload"); @@ -87,15 +90,15 @@ fn handle_trace_data( debug!(logger, "deserialised payload"); - let mut batch = Vec::>::new(); + let mut batch = Vec::::new(); - batch.push(Arc::new(span_data)); + batch.push(span_data); // Call low-level Jaeger exporter to send the trace span immediately. - let result = exporter.export(batch); + let result = exporter.export(batch).await; - if result != ExportResult::Success { - return Err(format!("failed to export trace spans: {:?}", result)); + if result.is_err() { + return Err(anyhow!("failed to export trace spans: {:?}", result)); } debug!(logger, "exported trace spans"); diff --git a/src/trace-forwarder/src/server.rs b/src/trace-forwarder/src/server.rs index 7f296c005f..371acca3d3 100644 --- a/src/trace-forwarder/src/server.rs +++ b/src/trace-forwarder/src/server.rs @@ -1,13 +1,13 @@ -// Copyright (c) 2020 Intel Corporation +// Copyright (c) 2020-2021 Intel Corporation // // SPDX-License-Identifier: Apache-2.0 // use crate::handler; -use nix::sys::socket::{SockAddr, VsockAddr}; +use anyhow::Result; +use futures::executor::block_on; use slog::{debug, error, info, o, Logger}; -use std::io; -use vsock::VsockListener; +use vsock::{SockAddr, VsockListener}; use crate::tracer; @@ -44,9 +44,8 @@ impl VsockTraceServer { } } - pub fn start(&mut self) -> Result<(), io::Error> { - let vsock_addr = VsockAddr::new(self.vsock_cid, self.vsock_port); - let sock_addr = SockAddr::Vsock(vsock_addr); + pub fn start(&mut self) -> Result<()> { + let sock_addr = SockAddr::new_vsock(self.vsock_cid, self.vsock_port); let listener = VsockListener::bind(&sock_addr)?; @@ -58,7 +57,7 @@ impl VsockTraceServer { self.jaeger_port, ); - let exporter = result?; + let mut exporter = result?; for conn in listener.incoming() { debug!(self.logger, "got client connection"); @@ -72,7 +71,9 @@ impl VsockTraceServer { let logger = self.logger.new(o!()); - handler::handle_connection(logger, conn, &exporter)?; + let f = handler::handle_connection(logger, conn, &mut exporter); + + block_on(f)?; } } diff --git a/src/trace-forwarder/src/tracer.rs b/src/trace-forwarder/src/tracer.rs index dd6ffd5384..bc4d30200f 100644 --- a/src/trace-forwarder/src/tracer.rs +++ b/src/trace-forwarder/src/tracer.rs @@ -1,9 +1,9 @@ -// Copyright (c) 2020 Intel Corporation +// Copyright (c) 2020-2021 Intel Corporation // // SPDX-License-Identifier: Apache-2.0 // -use opentelemetry::api::Key; +use opentelemetry::KeyValue; use std::net::SocketAddr; pub fn create_jaeger_trace_exporter( @@ -15,11 +15,6 @@ pub fn create_jaeger_trace_exporter( let jaeger_addr = format!("{}:{}", jaeger_host, jaeger_port); - let process = opentelemetry_jaeger::Process { - service_name: jaeger_service_name, - tags: vec![Key::new("exporter").string(exporter_type)], - }; - let socket_addr: SocketAddr = match jaeger_addr.parse() { Ok(a) => a, Err(e) => { @@ -30,10 +25,11 @@ pub fn create_jaeger_trace_exporter( } }; - let exporter = match opentelemetry_jaeger::Exporter::builder() + let exporter = match opentelemetry_jaeger::new_pipeline() + .with_service_name(jaeger_service_name) .with_agent_endpoint(socket_addr.to_string()) - .with_process(process) - .init() + .with_tags(vec![KeyValue::new("exporter", exporter_type)]) + .init_exporter() { Ok(x) => x, Err(e) => {