Skip to content

Commit

Permalink
feat(abci): add tracing spans
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Sep 8, 2023
1 parent 39c87a0 commit b9a6c53
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
3 changes: 2 additions & 1 deletion abci/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ description = """tenderdash-abci provides a simple framework with which to build
low-level applications on top of Tenderdash."""

[features]
default = ["server", "docker-tests", "crypto", "tcp", "unix"]
default = ["server", "docker-tests", "crypto", "tcp", "unix", "tracing-span"]
# docker-tests includes integration tests that require docker to be available
docker-tests = ["server"]
server = [
Expand All @@ -23,6 +23,7 @@ server = [
crypto = ["dep:lhash"]
tcp = ["server"]
unix = ["server"]
tracing-span = []

[[example]]
name = "echo_socket"
Expand Down
85 changes: 84 additions & 1 deletion abci/src/application.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! ABCI application interface.

use tracing::debug;
use hex::ToHex;
use tenderdash_proto::abci::request::Value;
use tracing::{debug, Level};

use crate::proto::{
abci,
Expand Down Expand Up @@ -150,6 +152,8 @@ pub trait RequestDispatcher {
// Implement `RequestDispatcher` for all `Application`s.
impl<A: Application> RequestDispatcher for A {
fn handle(&self, request: abci::Request) -> Option<abci::Response> {
#[cfg(feature = "tracing-span")]
let _span = enter_span(request.clone().value?);
tracing::trace!(?request, "received request");

let response: Result<response::Value, abci::ResponseException> = match request.value? {
Expand Down Expand Up @@ -188,6 +192,85 @@ impl<A: Application> RequestDispatcher for A {
})
}
}
#[cfg(feature = "tracing-span")]
fn enter_span<T>(request: T) -> tracing::span::EnteredSpan
where
T: Into<request::Value>,
{
let value = request.into();
const SPAN_NAME: &str = "abci";
const LEVEL: Level = Level::ERROR;
let endpoint = abci_method_name(&value);

let span = match value {
Value::Info(r) => tracing::span!(
LEVEL,
SPAN_NAME,
endpoint,
tenderdash_version = r.version,
block_version = r.block_version,
p2p_version = r.p2p_version,
),
Value::InitChain(r) => {
tracing::span!(LEVEL, SPAN_NAME, endpoint, chain_id = r.chain_id)
},
Value::PrepareProposal(r) => {
tracing::span!(
LEVEL,
SPAN_NAME,
endpoint,
height = r.height,
round = r.round,
quorum_hash = r.quorum_hash.encode_hex::<String>(),
core_locked_height = r.core_chain_locked_height,
)
},
Value::ProcessProposal(r) => tracing::span!(
LEVEL,
SPAN_NAME,
endpoint,
r.height,
r.round,
quorum_hash = r.quorum_hash.encode_hex::<String>(),
r.core_chain_locked_height,
),
Value::ExtendVote(r) => tracing::span!(LEVEL, SPAN_NAME, endpoint, r.height, r.round),
Value::VerifyVoteExtension(r) => {
tracing::span!(LEVEL, SPAN_NAME, endpoint, r.height, r.round)
},
Value::FinalizeBlock(r) => tracing::span!(LEVEL, SPAN_NAME, endpoint, r.height, r.round),
Value::CheckTx(r) => {
tracing::span!(LEVEL, SPAN_NAME, endpoint, tx = r.tx.encode_hex::<String>())
},
Value::Query(r) => {
tracing::span!(LEVEL, SPAN_NAME, endpoint, path = r.path)
},
_ => tracing::span!(LEVEL, SPAN_NAME, endpoint),
};

span.entered()
}
#[cfg(feature = "tracing-span")]
fn abci_method_name(request: &Value) -> String {
match request {
Value::ApplySnapshotChunk(_) => "ApplySnapshotChunk",
Value::CheckTx(_) => "CheckTx",
Value::Echo(_) => "Echo",
Value::ExtendVote(_) => "ExtendVote",
Value::FinalizeBlock(_) => "FinalizeBlock",
Value::Flush(_) => "Flush",
Value::Info(_) => "Info",
Value::InitChain(_) => "InitChain",
Value::ListSnapshots(_) => "ListSnapshots",
Value::LoadSnapshotChunk(_) => "LoadSnapshotChunk",
Value::OfferSnapshot(_) => "OfferSnapshot",
Value::PrepareProposal(_) => "PrepareProposal",
Value::ProcessProposal(_) => "ProcessProposal",
Value::Query(_) => "Query",
Value::VerifyVoteExtension(_) => "VerifyVoteExtension",
}
.to_string()
}

/// Check if ABCI version sent by Tenderdash matches version of linked protobuf
/// data objects.
Expand Down

0 comments on commit b9a6c53

Please sign in to comment.