From ee00940e1bd8b0d6ce0089f59a50d943403b1369 Mon Sep 17 00:00:00 2001 From: Lev Roitman Date: Sun, 2 Jun 2024 20:54:48 +0300 Subject: [PATCH] feat: add tracing --- Cargo.lock | 101 +++++++++++++++++-- Cargo.toml | 2 + crates/gateway/Cargo.toml | 1 + crates/gateway/src/gateway.rs | 3 +- crates/mempool_infra/Cargo.toml | 1 + crates/mempool_infra/src/component_server.rs | 5 +- crates/mempool_node/Cargo.toml | 2 + crates/mempool_node/src/main.rs | 22 +++- crates/mempool_node/src/servers.rs | 7 +- 9 files changed, 127 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b69d20969..3ffc836e9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2435,8 +2435,8 @@ dependencies = [ "aho-corasick", "bstr", "log", - "regex-automata", - "regex-syntax", + "regex-automata 0.4.7", + "regex-syntax 0.8.4", ] [[package]] @@ -2763,7 +2763,7 @@ dependencies = [ "globset", "log", "memchr", - "regex-automata", + "regex-automata 0.4.7", "same-file", "walkdir", "winapi-util", @@ -3155,7 +3155,7 @@ dependencies = [ "petgraph", "pico-args", "regex", - "regex-syntax", + "regex-syntax 0.8.4", "string_cache", "term", "tiny-keccak", @@ -3169,7 +3169,7 @@ version = "0.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "507460a910eb7b32ee961886ff48539633b788a36b65692b95f225b844c82553" dependencies = [ - "regex-automata", + "regex-automata 0.4.7", ] [[package]] @@ -3298,6 +3298,15 @@ dependencies = [ "hashbrown 0.14.5", ] +[[package]] +name = "matchers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" +dependencies = [ + "regex-automata 0.1.10", +] + [[package]] name = "matches" version = "0.1.10" @@ -4261,7 +4270,7 @@ dependencies = [ "rand", "rand_chacha", "rand_xorshift", - "regex-syntax", + "regex-syntax 0.8.4", "unarray", ] @@ -4382,8 +4391,17 @@ checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f" dependencies = [ "aho-corasick", "memchr", - "regex-automata", - "regex-syntax", + "regex-automata 0.4.7", + "regex-syntax 0.8.4", +] + +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax 0.6.29", ] [[package]] @@ -4394,9 +4412,15 @@ checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" dependencies = [ "aho-corasick", "memchr", - "regex-syntax", + "regex-syntax 0.8.4", ] +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + [[package]] name = "regex-syntax" version = "0.8.4" @@ -4971,6 +4995,15 @@ dependencies = [ "keccak", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "shlex" version = "1.3.0" @@ -5287,6 +5320,7 @@ dependencies = [ "test_utils", "thiserror", "tokio", + "tracing", "validator", ] @@ -5320,6 +5354,7 @@ dependencies = [ "serde", "thiserror", "tokio", + "tracing", ] [[package]] @@ -5372,6 +5407,8 @@ dependencies = [ "starknet_mempool_types", "test_utils", "tokio", + "tracing", + "tracing-subscriber", "validator", ] @@ -5657,6 +5694,16 @@ dependencies = [ "thiserror-impl-no-std", ] +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "time" version = "0.3.36" @@ -5945,6 +5992,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" dependencies = [ "once_cell", + "valuable", ] [[package]] @@ -5957,6 +6005,35 @@ dependencies = [ "tracing", ] +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", +] + [[package]] name = "try-lock" version = "0.2.5" @@ -6133,6 +6210,12 @@ version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad9680608df133af2c1ddd5eaf1ddce91d60d61b6bc51494ef326458365a470a" +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "vcpkg" version = "0.2.15" diff --git a/Cargo.toml b/Cargo.toml index df6c9a84f..0396867d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -75,5 +75,7 @@ tempfile = "3.3.0" thiserror = "1.0" tokio = { version = "1.37.0", features = ["full"] } tokio-test = "0.4.4" +tracing = "0.1.37" +tracing-subscriber = "0.3.16" url = "2.5.0" validator = "0.12" diff --git a/crates/gateway/Cargo.toml b/crates/gateway/Cargo.toml index 6db13e5a7..ce583b1c7 100644 --- a/crates/gateway/Cargo.toml +++ b/crates/gateway/Cargo.toml @@ -32,6 +32,7 @@ starknet-types-core.workspace = true test_utils = { path = "../test_utils", version = "0.0"} thiserror.workspace = true tokio.workspace = true +tracing.workspace = true validator.workspace = true [dev-dependencies] diff --git a/crates/gateway/src/gateway.rs b/crates/gateway/src/gateway.rs index d85cf007f..cd51fb77a 100644 --- a/crates/gateway/src/gateway.rs +++ b/crates/gateway/src/gateway.rs @@ -11,6 +11,7 @@ use starknet_api::transaction::TransactionHash; use starknet_mempool_infra::component_runner::{ComponentRunner, ComponentStartError}; use starknet_mempool_types::communication::SharedMempoolClient; use starknet_mempool_types::mempool_types::{Account, MempoolInput}; +use tracing::info; use crate::compilation::compile_contract_class; use crate::config::{GatewayConfig, GatewayNetworkConfig, RpcStateReaderConfig}; @@ -146,7 +147,7 @@ pub fn create_gateway( #[async_trait] impl ComponentRunner for Gateway { async fn start(&mut self) -> Result<(), ComponentStartError> { - println!("Gateway::start()"); + info!("Gateway::start()"); self.run().await.map_err(|_| ComponentStartError::InternalComponentError) } } diff --git a/crates/mempool_infra/Cargo.toml b/crates/mempool_infra/Cargo.toml index de61d1053..4f8e99602 100644 --- a/crates/mempool_infra/Cargo.toml +++ b/crates/mempool_infra/Cargo.toml @@ -19,6 +19,7 @@ hyper.workspace = true papyrus_config.workspace = true thiserror.workspace = true tokio.workspace = true +tracing.workspace = true [dev-dependencies] assert_matches.workspace = true diff --git a/crates/mempool_infra/src/component_server.rs b/crates/mempool_infra/src/component_server.rs index 94f6d8efa..17cb17bd0 100644 --- a/crates/mempool_infra/src/component_server.rs +++ b/crates/mempool_infra/src/component_server.rs @@ -11,6 +11,7 @@ use hyper::{Body, Request as HyperRequest, Response as HyperResponse, Server, St use serde::{Deserialize, Serialize}; use tokio::sync::mpsc::Receiver; use tokio::sync::Mutex; +use tracing::{error, info}; use crate::component_definitions::{ ComponentRequestAndResponseSender, ComponentRequestHandler, ServerError, @@ -152,8 +153,8 @@ impl EmptyServer { impl ComponentServerStarter for EmptyServer { async fn start(&mut self) { match self.component.start().await { - Ok(_) => println!("ComponentServer::start() completed."), - Err(err) => println!("ComponentServer::start() failed: {:?}", err), + Ok(_) => info!("ComponentServer::start() completed."), + Err(err) => error!("ComponentServer::start() failed: {:?}", err), } } } diff --git a/crates/mempool_node/Cargo.toml b/crates/mempool_node/Cargo.toml index 60ca054a5..50950af56 100644 --- a/crates/mempool_node/Cargo.toml +++ b/crates/mempool_node/Cargo.toml @@ -20,6 +20,8 @@ starknet_mempool_types = { path = "../mempool_types", version = "0.0" } serde.workspace = true papyrus_config.workspace = true tokio.workspace = true +tracing.workspace = true +tracing-subscriber = { workspace = true, features = ["env-filter"] } validator.workspace = true [dev-dependencies] diff --git a/crates/mempool_node/src/main.rs b/crates/mempool_node/src/main.rs index 699e95f99..baa70c50d 100644 --- a/crates/mempool_node/src/main.rs +++ b/crates/mempool_node/src/main.rs @@ -7,9 +7,27 @@ use starknet_mempool_node::communication::{create_node_channels, create_node_cli use starknet_mempool_node::components::create_components; use starknet_mempool_node::config::MempoolNodeConfig; use starknet_mempool_node::servers::{create_servers, run_server_components}; +use tracing::metadata::LevelFilter; +use tracing::{error, info}; +use tracing_subscriber::prelude::*; +use tracing_subscriber::{fmt, EnvFilter}; + +const DEFAULT_LEVEL: LevelFilter = LevelFilter::INFO; + +fn configure_tracing() { + let fmt_layer = fmt::layer().compact().with_target(false); + let level_filter_layer = + EnvFilter::builder().with_default_directive(DEFAULT_LEVEL.into()).from_env_lossy(); + + // This sets a single subscriber to all of the threads. We may want to implement different + // subscriber for some threads and use set_global_default instead of init. + tracing_subscriber::registry().with(fmt_layer).with(level_filter_layer).init(); +} #[tokio::main] async fn main() -> anyhow::Result<()> { + configure_tracing(); + let config = MempoolNodeConfig::load_and_process(args().collect()); if let Err(ConfigError::CommandInput(clap_err)) = config { clap_err.exit(); @@ -17,7 +35,7 @@ async fn main() -> anyhow::Result<()> { let config = config?; if let Err(error) = config_validate(&config) { - println!("Error: {}", error); + error!("{}", error); exit(1); } @@ -26,7 +44,7 @@ async fn main() -> anyhow::Result<()> { let components = create_components(&config, &clients); let servers = create_servers(&config, channels, components); - println!("Info: Starting components!"); + info!("Starting components!"); run_server_components(&config, servers).await?; Ok(()) diff --git a/crates/mempool_node/src/servers.rs b/crates/mempool_node/src/servers.rs index 74b601c48..867b2ac36 100644 --- a/crates/mempool_node/src/servers.rs +++ b/crates/mempool_node/src/servers.rs @@ -5,6 +5,7 @@ use futures::{Future, FutureExt}; use starknet_gateway::communication::{create_gateway_server, GatewayServer}; use starknet_mempool::communication::{create_mempool_server, MempoolServer}; use starknet_mempool_infra::component_server::ComponentServerStarter; +use tracing::error; use crate::communication::MempoolNodeCommunication; use crate::components::Components; @@ -57,15 +58,15 @@ pub async fn run_server_components( tokio::select! { res = gateway_handle => { - println!("Error: Gateway Server stopped."); + error!("Gateway Server stopped."); res? } res = mempool_handle => { - println!("Error: Mempool Server stopped."); + error!("Mempool Server stopped."); res? } }; - println!("Error: Servers ended with unexpected Ok."); + error!("Servers ended with unexpected Ok."); Ok(()) }