From 65605bad860c83e58dd60e480a067c4c3d88ed5a Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Mon, 11 Nov 2024 13:32:59 +0200 Subject: [PATCH] feat(worker): trace feature for tokio-console --- .cargo/config.toml | 2 +- Cargo.lock | 129 ++++++++++++++++++++++++++++++++++++- emerwen-worker/Cargo.toml | 4 ++ emerwen-worker/src/main.rs | 47 ++++++++------ flake.nix | 1 + justfile | 3 + 6 files changed, 165 insertions(+), 21 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index d29d6c3..e544e4e 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,3 +1,3 @@ [target.x86_64-unknown-linux-gnu] linker = "clang" -rustflags = ["-C", "link-arg=-fuse-ld=mold"] +rustflags = ["--cfg", "tokio_unstable", "-C", "link-arg=-fuse-ld=mold"] diff --git a/Cargo.lock b/Cargo.lock index 5fffa42..e64da4c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -188,6 +188,12 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "base64" +version = "0.21.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" + [[package]] name = "base64" version = "0.22.1" @@ -279,6 +285,45 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" +[[package]] +name = "console-api" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8030735ecb0d128428b64cd379809817e620a40e5001c54465b99ec5feec2857" +dependencies = [ + "futures-core", + "prost", + "prost-types", + "tonic", + "tracing-core", +] + +[[package]] +name = "console-subscriber" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6539aa9c6a4cd31f4b1c040f860a1eac9aa80e7df6b05d506a6e7179936d6a01" +dependencies = [ + "console-api", + "crossbeam-channel", + "crossbeam-utils", + "futures-task", + "hdrhistogram", + "humantime", + "hyper-util", + "prost", + "prost-types", + "serde", + "serde_json", + "thread_local", + "tokio", + "tokio-stream", + "tonic", + "tracing", + "tracing-core", + "tracing-subscriber", +] + [[package]] name = "core-foundation" version = "0.9.4" @@ -295,6 +340,30 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" +[[package]] +name = "crc32fast" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "crossbeam-channel" +version = "0.5.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33480d6946193aa8033910124896ca395333cae7e2d1113d1fef6c3272217df2" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" + [[package]] name = "displaydoc" version = "0.2.5" @@ -338,6 +407,7 @@ name = "emerwen-worker" version = "0.1.0" dependencies = [ "clap", + "console-subscriber", "emerwen-protocol", "reqwest", "surge-ping", @@ -384,6 +454,16 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" +[[package]] +name = "flate2" +version = "1.0.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1b589b4dc103969ad3cf85c950899926ec64300a1a46d76c03a6072957036f0" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + [[package]] name = "fnv" version = "1.0.7" @@ -507,6 +587,19 @@ version = "0.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a9bfc1af68b1726ea47d3d5109de126281def866b33970e10fbab11b5dafab3" +[[package]] +name = "hdrhistogram" +version = "7.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "765c9198f173dd59ce26ff9f95ef0aafd0a0fe01fb9d72841bc5066a4c06511d" +dependencies = [ + "base64 0.21.7", + "byteorder", + "flate2", + "nom", + "num-traits", +] + [[package]] name = "heck" version = "0.5.0" @@ -571,6 +664,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "hyper" version = "1.5.0" @@ -919,6 +1018,12 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "miniz_oxide" version = "0.8.0" @@ -969,6 +1074,16 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43794a0ace135be66a25d3ae77d41b91615fb68ae937f904090203e81f755b65" +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + [[package]] name = "nu-ansi-term" version = "0.46.0" @@ -979,6 +1094,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "object" version = "0.36.5" @@ -1342,7 +1466,7 @@ version = "0.12.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a77c62af46e79de0a562e1a9849205ffcb7fc1238876e9bd743357570e04046f" dependencies = [ - "base64", + "base64 0.22.1", "bytes", "encoding_rs", "futures-core", @@ -1761,6 +1885,7 @@ dependencies = [ "signal-hook-registry", "socket2", "tokio-macros", + "tracing", "windows-sys 0.52.0", ] @@ -1829,7 +1954,7 @@ dependencies = [ "async-stream", "async-trait", "axum", - "base64", + "base64 0.22.1", "bytes", "h2", "http", diff --git a/emerwen-worker/Cargo.toml b/emerwen-worker/Cargo.toml index 88f3541..546c109 100644 --- a/emerwen-worker/Cargo.toml +++ b/emerwen-worker/Cargo.toml @@ -12,9 +12,13 @@ repository.workspace = true emerwen-protocol = { path = "../emerwen-protocol" } clap = { version = "4", features = ["derive"] } +console-subscriber = { version = "0.4", optional = true } reqwest = "0.12" surge-ping = "0.8.1" tokio = { version = "1", features = ["full"] } tonic = "0.12" tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter"] } + +[features] +trace = ["tokio/tracing", "dep:console-subscriber"] diff --git a/emerwen-worker/src/main.rs b/emerwen-worker/src/main.rs index 3e381e8..9396746 100644 --- a/emerwen-worker/src/main.rs +++ b/emerwen-worker/src/main.rs @@ -8,7 +8,7 @@ use clap::Parser; use emerwen_protocol::emerwen_protocol_client::EmerwenProtocolClient; use tonic::{metadata::MetadataValue, service::Interceptor, transport::Channel}; -use tracing::{debug, info, level_filters::LevelFilter}; +use tracing::{debug, info}; use tracing_subscriber::{prelude::*, EnvFilter}; mod monitor; @@ -33,23 +33,34 @@ struct Args { async fn main() -> Result<(), Box> { let args = Args::parse(); - tracing_subscriber::registry() - .with( - tracing_subscriber::fmt::layer() - .with_file(args.debug) - .with_line_number(args.debug) - .with_level(true), - ) - .with( - EnvFilter::builder() - .with_default_directive(if args.debug { - LevelFilter::DEBUG.into() - } else { - LevelFilter::INFO.into() - }) - .from_env_lossy(), - ) - .init(); + #[cfg(feature = "trace")] + { + let console_layer = console_subscriber::spawn(); + tracing_subscriber::registry() + .with(console_layer) + .with( + tracing_subscriber::fmt::layer() + .with_file(true) + .with_line_number(true) + .with_level(true), + ) + .with(EnvFilter::from_default_env().add_directive("trace".parse()?)) + .init(); + } + #[cfg(not(feature = "trace"))] + { + let mut filter = EnvFilter::from_default_env(); + if args.debug { + filter = filter.add_directive("emerwen_worker=debug".parse()?); + } else { + filter = filter.add_directive("emerwen_worker=info".parse()?); + } + + tracing_subscriber::registry() + .with(tracing_subscriber::fmt::layer().with_level(true)) + .with(filter) + .init(); + } info!("Starting emerwen worker..."); debug!("Hello, debug!"); diff --git a/flake.nix b/flake.nix index fa63e3c..a03e956 100644 --- a/flake.nix +++ b/flake.nix @@ -88,6 +88,7 @@ protobuf just + tokio-console #cargo-leptos diff --git a/justfile b/justfile index 85df916..13e07e2 100644 --- a/justfile +++ b/justfile @@ -7,5 +7,8 @@ master *ARGS: worker *ARGS: cargo run --bin emerwen-worker -- {{ARGS}} +worker-trace *ARGS: + cargo run --bin emerwen-worker --features trace -- {{ARGS}} + test-protocol: cargo test --package emerwen-protocol -- 2.44.1