network: use short Display for Message in spans

This makes the span data more compact (e.g., `msg_as_req{msg=block}`) and
restores the Debug impl for Message to show all of the data contained in the
message.  The full message is added as a single event at trace level in the
span to preserve the previous full-inspectability.
This commit is contained in:
Henry de Valence 2020-12-01 12:50:50 -08:00
parent f0db75e712
commit 18cf5e0249
2 changed files with 25 additions and 89 deletions

View File

@ -561,8 +561,9 @@ where
// This function has its own span, because we're creating a new work
// context (namely, the work of processing the inbound msg as a request)
#[instrument(skip(self))]
#[instrument(name = "msg_as_req", skip(self, msg), fields(%msg))]
async fn handle_message_as_request(&mut self, msg: Message) {
trace!(?msg);
let req = match msg {
Message::Ping(nonce) => {
trace!(?nonce, "responding to heartbeat");

View File

@ -7,7 +7,6 @@ use chrono::{DateTime, Utc};
use zebra_chain::{
block::{self, Block},
fmt::{DisplayToDebug, SummaryDebug},
transaction::Transaction,
};
@ -31,7 +30,7 @@ use crate::meta_addr::MetaAddr;
/// during serialization).
///
/// [btc_wiki_protocol]: https://en.bitcoin.it/wiki/Protocol_documentation
#[derive(Clone, Eq, PartialEq)]
#[derive(Clone, Eq, PartialEq, Debug)]
pub enum Message {
/// A `version` message.
///
@ -309,92 +308,28 @@ pub enum RejectReason {
Other = 0x50,
}
/// Summarise `Vec`s when debugging messages
impl fmt::Debug for Message {
impl fmt::Display for Message {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Message::Version {
version,
services,
timestamp,
address_recv,
address_from,
nonce,
user_agent,
start_height,
relay,
} => f
.debug_struct("Version")
.field("version", version)
.field("services", services)
.field("timestamp", timestamp)
.field("address_recv", address_recv)
.field("address_from", address_from)
.field("nonce", nonce)
.field("user_agent", user_agent)
.field("start_height", start_height)
.field("relay", relay)
.finish(),
Message::Verack => f.debug_tuple("Verack").finish(),
Message::Ping(nonce) => f.debug_tuple("Ping").field(nonce).finish(),
Message::Pong(nonce) => f.debug_tuple("Pong").field(nonce).finish(),
Message::Reject {
message,
ccode,
reason,
data,
} => f
.debug_struct("Reject")
.field("message", message)
.field("ccode", ccode)
.field("reason", reason)
.field("data", data)
.finish(),
Message::GetAddr => f.debug_tuple("GetAddr").finish(),
Message::Addr(addr) => f.debug_tuple("Addr").field(&SummaryDebug(addr)).finish(),
Message::GetBlocks { known_blocks, stop } => f
.debug_struct("GetBlocks")
.field("known_blocks", &SummaryDebug(known_blocks))
.field("stop", stop)
.finish(),
Message::Inv(inv) => f.debug_tuple("Inv").field(&SummaryDebug(inv)).finish(),
Message::GetHeaders { known_blocks, stop } => f
.debug_struct("GetHeaders")
.field("known_blocks", &SummaryDebug(known_blocks))
.field("stop", stop)
.finish(),
Message::Headers(headers) => f
.debug_tuple("Headers")
.field(&SummaryDebug(headers))
.finish(),
Message::GetData(data) => f.debug_tuple("GetData").field(&SummaryDebug(data)).finish(),
Message::Block(block) => f
.debug_tuple("Block")
.field(&DisplayToDebug(block))
.finish(),
Message::Tx(tx) => f.debug_tuple("Tx").field(&tx).finish(),
Message::NotFound(not_found) => f
.debug_tuple("NotFound")
.field(&SummaryDebug(not_found))
.finish(),
Message::Mempool => f.debug_tuple("Mempool").finish(),
Message::FilterLoad {
filter,
hash_functions_count,
tweak,
flags,
} => f
.debug_struct("FilterLoad")
.field("filter", filter)
.field("hash_functions_count", hash_functions_count)
.field("tweak", tweak)
.field("flags", flags)
.finish(),
Message::FilterAdd { data } => f
.debug_struct("FilterAdd")
.field("data", &SummaryDebug(data))
.finish(),
Message::FilterClear => f.debug_tuple("FilterClear").finish(),
}
f.write_str(match self {
Message::Version { .. } => "version",
Message::Verack => "verack",
Message::Ping(_) => "ping",
Message::Pong(_) => "pong",
Message::Reject { .. } => "reject",
Message::GetAddr => "getaddr",
Message::Addr(_) => "addr",
Message::GetBlocks { .. } => "getblocks",
Message::Inv(_) => "inv",
Message::GetHeaders { .. } => "getheaders",
Message::Headers(_) => "headers",
Message::GetData(_) => "getdata",
Message::Block(_) => "block",
Message::Tx(_) => "tx",
Message::NotFound(_) => "notfound",
Message::Mempool => "mempool",
Message::FilterLoad { .. } => "filterload",
Message::FilterAdd { .. } => "filteradd",
Message::FilterClear => "filterclear",
})
}
}