tenderdash_abci/
tracing_span.rs

1use tenderdash_proto::abci::request::Value;
2use tracing::Level;
3
4const SPAN_NAME: &str = "abci";
5const LEVEL: Level = Level::ERROR;
6
7macro_rules! block_span {
8    ($request: expr, $endpoint:expr, $request_id:expr) => {
9        tracing::span!(
10            LEVEL,
11            SPAN_NAME,
12            endpoint = $endpoint,
13            request_id = $request_id,
14            height = $request.height,
15            round = $request.round
16        )
17    };
18}
19/// Creates a new span for tracing.
20///
21/// This function creates a new `tracing::span::EnteredSpan` based on the
22/// provided request. It uses the request to determine the endpoint and includes
23/// a unique request ID in the span.
24///
25/// The level of the span is set to ERROR, so it will be included on all log
26/// levels.
27///
28/// # Arguments
29///
30/// * `request` - A value that can be converted into a `Value`. Depending on the
31///   specific variant of `Value`, additional information like height, round, or
32///   path might be included in the span.
33///
34/// # Returns
35///
36/// An entered span which represents an active or entered span state.
37///
38/// # Examples
39///
40/// ```
41/// # use tenderdash_proto::abci::{RequestInfo, request};
42/// # use tenderdash_abci::tracing_span::span;
43///
44/// let request = request::Value::Info(RequestInfo::default());
45/// let span = span(request);
46/// ```
47pub fn span<T>(request: T) -> tracing::span::EnteredSpan
48where
49    T: Into<Value>,
50{
51    let value = request.into();
52
53    let endpoint = abci_method_name(&value);
54    let request_id = uuid::Uuid::new_v4().to_string();
55
56    let span = match value {
57        Value::Info(_r) => tracing::span!(LEVEL, SPAN_NAME, endpoint, request_id),
58        Value::InitChain(_r) => {
59            tracing::span!(LEVEL, SPAN_NAME, endpoint, request_id)
60        },
61        Value::PrepareProposal(r) => block_span!(r, endpoint, request_id),
62        Value::ProcessProposal(r) => block_span!(r, endpoint, request_id),
63        Value::ExtendVote(r) => block_span!(r, endpoint, request_id),
64        Value::VerifyVoteExtension(r) => block_span!(r, endpoint, request_id),
65        Value::FinalizeBlock(r) => block_span!(r, endpoint, request_id),
66        Value::CheckTx(_r) => {
67            tracing::span!(LEVEL, SPAN_NAME, endpoint, request_id)
68        },
69        Value::Query(r) => {
70            tracing::span!(LEVEL, SPAN_NAME, endpoint, request_id, path = r.path)
71        },
72        _ => tracing::span!(LEVEL, SPAN_NAME, endpoint, request_id),
73    };
74
75    span.entered()
76}
77
78fn abci_method_name(request: &Value) -> String {
79    match request {
80        Value::ApplySnapshotChunk(_) => "ApplySnapshotChunk",
81        Value::CheckTx(_) => "CheckTx",
82        Value::Echo(_) => "Echo",
83        Value::ExtendVote(_) => "ExtendVote",
84        Value::FinalizeBlock(_) => "FinalizeBlock",
85        Value::Flush(_) => "Flush",
86        Value::Info(_) => "Info",
87        Value::InitChain(_) => "InitChain",
88        Value::ListSnapshots(_) => "ListSnapshots",
89        Value::LoadSnapshotChunk(_) => "LoadSnapshotChunk",
90        Value::OfferSnapshot(_) => "OfferSnapshot",
91        Value::PrepareProposal(_) => "PrepareProposal",
92        Value::ProcessProposal(_) => "ProcessProposal",
93        Value::Query(_) => "Query",
94        Value::VerifyVoteExtension(_) => "VerifyVoteExtension",
95    }
96    .to_string()
97}