From f1270041ca856e616d83ec42584e8634431da77d Mon Sep 17 00:00:00 2001 From: Renato Dinhani <101204870+dinhani-cw@users.noreply.github.com> Date: Mon, 24 Jun 2024 16:34:43 -0300 Subject: [PATCH] refactor: move tracing extensions to tracing module (#1226) --- src/bin/importer_online.rs | 2 +- src/eth/block_miner.rs | 2 +- src/eth/executor.rs | 2 +- src/eth/relayer/external.rs | 2 +- src/eth/rpc/rpc_middleware.rs | 2 +- src/eth/rpc/rpc_server.rs | 2 +- src/eth/storage/stratus_storage.rs | 2 +- src/ext.rs | 91 +---------------------------- src/infra/tracing.rs | 94 ++++++++++++++++++++++++++++++ 9 files changed, 102 insertions(+), 97 deletions(-) diff --git a/src/bin/importer_online.rs b/src/bin/importer_online.rs index 5b66396e7..15415f8f6 100644 --- a/src/bin/importer_online.rs +++ b/src/bin/importer_online.rs @@ -21,12 +21,12 @@ use stratus::ext::spawn_named; use stratus::ext::traced_sleep; use stratus::ext::DisplayExt; use stratus::ext::SleepReason; -use stratus::ext::SpanExt; use stratus::if_else; #[cfg(feature = "metrics")] use stratus::infra::metrics; use stratus::infra::tracing::warn_task_rx_closed; use stratus::infra::tracing::warn_task_tx_closed; +use stratus::infra::tracing::SpanExt; use stratus::infra::BlockchainClient; use stratus::log_and_err; #[cfg(feature = "metrics")] diff --git a/src/eth/block_miner.rs b/src/eth/block_miner.rs index 7a0daee1e..625f83811 100644 --- a/src/eth/block_miner.rs +++ b/src/eth/block_miner.rs @@ -28,7 +28,7 @@ use crate::ext::parse_duration; use crate::ext::spawn_blocking_named_or_thread; use crate::ext::spawn_named; use crate::ext::DisplayExt; -use crate::ext::SpanExt; +use crate::infra::tracing::SpanExt; use crate::log_and_err; pub struct BlockMiner { diff --git a/src/eth/executor.rs b/src/eth/executor.rs index 780a7777d..9ef33448c 100644 --- a/src/eth/executor.rs +++ b/src/eth/executor.rs @@ -46,11 +46,11 @@ use crate::eth::BlockMiner; use crate::ext::spawn_blocking_named; use crate::ext::spawn_blocking_named_or_thread; use crate::ext::ResultExt; -use crate::ext::SpanExt; #[cfg(feature = "metrics")] use crate::infra::metrics; use crate::infra::tracing::info_task_spawn; use crate::infra::tracing::warn_task_tx_closed; +use crate::infra::tracing::SpanExt; use crate::infra::BlockchainClient; use crate::GlobalState; diff --git a/src/eth/relayer/external.rs b/src/eth/relayer/external.rs index b0ad3defd..2121129f3 100644 --- a/src/eth/relayer/external.rs +++ b/src/eth/relayer/external.rs @@ -26,10 +26,10 @@ use crate::eth::primitives::TransactionMined; use crate::ext::traced_sleep; use crate::ext::ResultExt; use crate::ext::SleepReason; -use crate::ext::SpanExt; use crate::infra::blockchain_client::pending_transaction::PendingTransaction; #[cfg(feature = "metrics")] use crate::infra::metrics; +use crate::infra::tracing::SpanExt; use crate::infra::BlockchainClient; use crate::log_and_err; diff --git a/src/eth/rpc/rpc_middleware.rs b/src/eth/rpc/rpc_middleware.rs index 31a2c6a4e..022e5135f 100644 --- a/src/eth/rpc/rpc_middleware.rs +++ b/src/eth/rpc/rpc_middleware.rs @@ -25,11 +25,11 @@ use crate::eth::rpc::next_rpc_param; use crate::eth::rpc::parse_rpc_rlp; use crate::eth::rpc::rpc_parser::RpcExtensionsExt; use crate::eth::rpc::RpcClientApp; -use crate::ext::SpanExt; use crate::if_else; #[cfg(feature = "metrics")] use crate::infra::metrics; use crate::infra::tracing::new_cid; +use crate::infra::tracing::SpanExt; // ----------------------------------------------------------------------------- // Active requests tracking diff --git a/src/eth/rpc/rpc_server.rs b/src/eth/rpc/rpc_server.rs index e8659b06d..27ffdca94 100644 --- a/src/eth/rpc/rpc_server.rs +++ b/src/eth/rpc/rpc_server.rs @@ -51,9 +51,9 @@ use crate::eth::Consensus; use crate::eth::Executor; use crate::ext::not; use crate::ext::ResultExt; -use crate::ext::SpanExt; use crate::infra::build_info; use crate::infra::tracing::warn_task_cancellation; +use crate::infra::tracing::SpanExt; use crate::GlobalState; // ----------------------------------------------------------------------------- diff --git a/src/eth/storage/stratus_storage.rs b/src/eth/storage/stratus_storage.rs index 844f5e7d6..b82e3597b 100644 --- a/src/eth/storage/stratus_storage.rs +++ b/src/eth/storage/stratus_storage.rs @@ -21,7 +21,7 @@ use crate::eth::primitives::TransactionExecution; use crate::eth::primitives::TransactionMined; use crate::eth::storage::PermanentStorage; use crate::eth::storage::TemporaryStorage; -use crate::ext::SpanExt; +use crate::infra::tracing::SpanExt; cfg_if::cfg_if! { if #[cfg(test)] { diff --git a/src/ext.rs b/src/ext.rs index b58f368fd..87f429d25 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -9,9 +9,9 @@ use tokio::signal::unix::signal; use tokio::signal::unix::SignalKind; use tracing::info_span; use tracing::Instrument; -use tracing::Span; use crate::infra::tracing::info_task_spawn; +use crate::log_and_err; use crate::GlobalState; // ----------------------------------------------------------------------------- @@ -221,95 +221,6 @@ macro_rules! channel_read_sync_impl { }}; } -// ----------------------------------------------------------------------------- -// Tracing -// ----------------------------------------------------------------------------- - -/// Extensions for `tracing::Span`. -pub trait SpanExt { - #[cfg(feature = "tracing")] - /// Applies the provided function to the current span. - fn with(fill: F) - where - F: Fn(Span), - { - let span = Span::current(); - fill(span); - } - - #[cfg(not(feature = "tracing"))] - fn with(_: F) - where - F: Fn(Span), - { - } - - /// Records a value using `ToString` implementation. - fn rec_str(&self, field: &'static str, value: &T) - where - T: ToString; - - /// Records a value using `ToString` implementation if the option value is present. - fn rec_opt(&self, field: &'static str, value: &Option) - where - T: ToString; -} - -impl SpanExt for Span { - fn rec_str(&self, field: &'static str, value: &T) - where - T: ToString, - { - self.record(field, value.to_string().as_str()); - } - - fn rec_opt(&self, field: &'static str, value: &Option) - where - T: ToString, - { - if let Some(ref value) = value { - self.record(field, value.to_string().as_str()); - } - } -} - -/// Logs an error and also wrap the existing error with the provided message. -#[macro_export] -macro_rules! log_and_err { - // with reason: wrap the original error with provided message - (reason = $error:ident, payload = $payload:expr, $msg:expr) => { - { - use anyhow::Context; - tracing::error!(reason = ?$error, payload = ?$payload, message = %$msg); - Err($error).context($msg) - } - }; - (reason = $error:ident, $msg:expr) => { - { - use anyhow::Context; - tracing::error!(reason = ?$error, message = %$msg); - Err($error).context($msg) - } - }; - // without reason: generate a new error using provided message - (payload = $payload:expr, $msg:expr) => { - { - use anyhow::Context; - use anyhow::anyhow; - tracing::error!(payload = ?$payload, message = %$msg); - let message = format!("{} | payload={:?}", $msg, $payload); - Err(anyhow!(message)) - } - }; - ($msg:expr) => { - { - use anyhow::anyhow; - tracing::error!(message = %$msg); - Err(anyhow!($msg)) - } - }; -} - // ----------------------------------------------------------------------------- // Tokio // ----------------------------------------------------------------------------- diff --git a/src/infra/tracing.rs b/src/infra/tracing.rs index 85b520b93..792d3222b 100644 --- a/src/infra/tracing.rs +++ b/src/infra/tracing.rs @@ -31,6 +31,7 @@ use tonic::metadata::MetadataMap; use tracing::span; use tracing::span::Attributes; use tracing::Event; +use tracing::Span; use tracing::Subscriber; use tracing_serde::fields::AsMap; use tracing_serde::fields::SerializeFieldMap; @@ -443,6 +444,99 @@ impl FormatTime for MinimalTimer { } } +// ----------------------------------------------------------------------------- +// Tracing extensions +// ----------------------------------------------------------------------------- + +/// Extensions for `tracing::Span`. +pub trait SpanExt { + #[cfg(feature = "tracing")] + /// Applies the provided function to the current span. + fn with(fill: F) + where + F: Fn(Span), + { + let span = Span::current(); + fill(span); + } + + #[cfg(not(feature = "tracing"))] + fn with(_: F) + where + F: Fn(Span), + { + } + + /// Records a value using `ToString` implementation. + fn rec_str(&self, field: &'static str, value: &T) + where + T: ToString; + + /// Records a value using `ToString` implementation if the option value is present. + fn rec_opt(&self, field: &'static str, value: &Option) + where + T: ToString; +} + +impl SpanExt for Span { + fn rec_str(&self, field: &'static str, value: &T) + where + T: ToString, + { + self.record(field, value.to_string().as_str()); + } + + fn rec_opt(&self, field: &'static str, value: &Option) + where + T: ToString, + { + if let Some(ref value) = value { + self.record(field, value.to_string().as_str()); + } + } +} + +// ----------------------------------------------------------------------------- +// Tracing macros +// ----------------------------------------------------------------------------- + +/// Logs an error and also wrap the existing error with the provided message. +#[macro_export] +macro_rules! log_and_err { + // with reason: wrap the original error with provided message + (reason = $error:ident, payload = $payload:expr, $msg:expr) => { + { + use anyhow::Context; + tracing::error!(reason = ?$error, payload = ?$payload, message = %$msg); + Err($error).context($msg) + } + }; + (reason = $error:ident, $msg:expr) => { + { + use anyhow::Context; + tracing::error!(reason = ?$error, message = %$msg); + Err($error).context($msg) + } + }; + // without reason: generate a new error using provided message + (payload = $payload:expr, $msg:expr) => { + { + use anyhow::Context; + use anyhow::anyhow; + tracing::error!(payload = ?$payload, message = %$msg); + let message = format!("{} | payload={:?}", $msg, $payload); + Err(anyhow!(message)) + } + }; + ($msg:expr) => { + { + use anyhow::anyhow; + tracing::error!(message = %$msg); + Err(anyhow!($msg)) + } + }; +} + // ----------------------------------------------------------------------------- // Tracing functions // -----------------------------------------------------------------------------