Skip to content

Commit

Permalink
refactor: move tracing extensions to tracing module (#1226)
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhani-cw authored Jun 24, 2024
1 parent 714a7fc commit f127004
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 97 deletions.
2 changes: 1 addition & 1 deletion src/bin/importer_online.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
2 changes: 1 addition & 1 deletion src/eth/block_miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/eth/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/eth/relayer/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/eth/rpc/rpc_middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/eth/rpc/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

// -----------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/eth/storage/stratus_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)] {
Expand Down
91 changes: 1 addition & 90 deletions src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -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<F>(fill: F)
where
F: Fn(Span),
{
let span = Span::current();
fill(span);
}

#[cfg(not(feature = "tracing"))]
fn with<F>(_: F)
where
F: Fn(Span),
{
}

/// Records a value using `ToString` implementation.
fn rec_str<T>(&self, field: &'static str, value: &T)
where
T: ToString;

/// Records a value using `ToString` implementation if the option value is present.
fn rec_opt<T>(&self, field: &'static str, value: &Option<T>)
where
T: ToString;
}

impl SpanExt for Span {
fn rec_str<T>(&self, field: &'static str, value: &T)
where
T: ToString,
{
self.record(field, value.to_string().as_str());
}

fn rec_opt<T>(&self, field: &'static str, value: &Option<T>)
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
// -----------------------------------------------------------------------------
Expand Down
94 changes: 94 additions & 0 deletions src/infra/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<F>(fill: F)
where
F: Fn(Span),
{
let span = Span::current();
fill(span);
}

#[cfg(not(feature = "tracing"))]
fn with<F>(_: F)
where
F: Fn(Span),
{
}

/// Records a value using `ToString` implementation.
fn rec_str<T>(&self, field: &'static str, value: &T)
where
T: ToString;

/// Records a value using `ToString` implementation if the option value is present.
fn rec_opt<T>(&self, field: &'static str, value: &Option<T>)
where
T: ToString;
}

impl SpanExt for Span {
fn rec_str<T>(&self, field: &'static str, value: &T)
where
T: ToString,
{
self.record(field, value.to_string().as_str());
}

fn rec_opt<T>(&self, field: &'static str, value: &Option<T>)
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
// -----------------------------------------------------------------------------
Expand Down

0 comments on commit f127004

Please sign in to comment.