From 8de550d9dd44fdf1404132570170f39979e8b2be Mon Sep 17 00:00:00 2001 From: Lukasz Klimek <842586+lklimek@users.noreply.github.com> Date: Fri, 8 Sep 2023 15:20:22 +0200 Subject: [PATCH] chore: minor improvements and docs --- abci/src/application.rs | 1 - abci/src/lib.rs | 3 +- abci/src/tracing_span.rs | 97 +++++++++++++++++++--------------------- 3 files changed, 49 insertions(+), 52 deletions(-) diff --git a/abci/src/application.rs b/abci/src/application.rs index 6878411..5b4730e 100644 --- a/abci/src/application.rs +++ b/abci/src/application.rs @@ -179,7 +179,6 @@ impl RequestDispatcher for A { } .unwrap_or_else(|e| e.into()); - // TODO: errors as err if let response::Value::Exception(ref exception) = response { tracing::error!(response=?exception, "sending ABCI exception"); } else { diff --git a/abci/src/lib.rs b/abci/src/lib.rs index 3e457fc..9f65419 100644 --- a/abci/src/lib.rs +++ b/abci/src/lib.rs @@ -27,7 +27,8 @@ pub use tenderdash_proto as proto; #[cfg(feature = "crypto")] pub mod signatures; #[cfg(feature = "tracing-span")] -mod tracing_span; +/// Create tracing::Span for better logging +pub mod tracing_span; /// Errors that may happen during protobuf communication #[derive(Debug, thiserror::Error)] diff --git a/abci/src/tracing_span.rs b/abci/src/tracing_span.rs index 641ffed..f3d2c45 100644 --- a/abci/src/tracing_span.rs +++ b/abci/src/tracing_span.rs @@ -1,13 +1,52 @@ use tenderdash_proto::abci::request::Value; use tracing::Level; +const SPAN_NAME: &str = "abci"; +const LEVEL: Level = Level::ERROR; + +macro_rules! block_span { + ($request: expr, $endpoint:expr, $request_id:expr) => { + tracing::span!( + LEVEL, + SPAN_NAME, + endpoint = $endpoint, + request_id = $request_id, + height = $request.height, + round = $request.round + ) + }; +} +/// Creates a new span for tracing. +/// +/// This function creates a new `tracing::span::EnteredSpan` based on the +/// provided request. It uses the request to determine the endpoint and includes +/// a unique request ID in the span. +/// +/// The level of the span is set to ERROR, so it will be included on all log +/// levels. +/// +/// # Arguments +/// +/// * `request` - A value that can be converted into a `Value`. Depending on the +/// specific variant of `Value`, additional information like height, round, or +/// path might be included in the span. +/// +/// # Returns +/// +/// An entered span which represents an active or entered span state. +/// +/// # Examples +/// +/// ``` +/// let request = Value::Info(RequestInfo::new()); +/// let span = span(request); +/// ``` pub fn span(request: T) -> tracing::span::EnteredSpan where T: Into, { let value = request.into(); - const SPAN_NAME: &str = "abci"; - const LEVEL: Level = Level::ERROR; + let endpoint = abci_method_name(&value); let request_id = uuid::Uuid::new_v4().to_string(); @@ -16,54 +55,11 @@ where Value::InitChain(_r) => { tracing::span!(LEVEL, SPAN_NAME, endpoint, request_id) }, - Value::PrepareProposal(r) => { - tracing::span!( - LEVEL, - SPAN_NAME, - endpoint, - request_id, - height = r.height, - round = r.round, - ) - }, - Value::ProcessProposal(r) => tracing::span!( - LEVEL, - SPAN_NAME, - endpoint, - request_id, - height = r.height, - round = r.round, - ), - Value::ExtendVote(r) => { - tracing::span!( - LEVEL, - SPAN_NAME, - endpoint, - request_id, - height = r.height, - round = r.round - ) - }, - Value::VerifyVoteExtension(r) => { - tracing::span!( - LEVEL, - SPAN_NAME, - endpoint, - request_id, - height = r.height, - round = r.round - ) - }, - Value::FinalizeBlock(r) => { - tracing::span!( - LEVEL, - SPAN_NAME, - endpoint, - request_id, - height = r.height, - round = r.round - ) - }, + Value::PrepareProposal(r) => block_span!(r, endpoint, request_id), + Value::ProcessProposal(r) => block_span!(r, endpoint, request_id), + Value::ExtendVote(r) => block_span!(r, endpoint, request_id), + Value::VerifyVoteExtension(r) => block_span!(r, endpoint, request_id), + Value::FinalizeBlock(r) => block_span!(r, endpoint, request_id), Value::CheckTx(_r) => { tracing::span!(LEVEL, SPAN_NAME, endpoint, request_id) }, @@ -75,6 +71,7 @@ where span.entered() } + fn abci_method_name(request: &Value) -> String { match request { Value::ApplySnapshotChunk(_) => "ApplySnapshotChunk",