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 // 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) // 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) { async fn handle_message_as_request(&mut self, msg: Message) {
trace!(?msg);
let req = match msg { let req = match msg {
Message::Ping(nonce) => { Message::Ping(nonce) => {
trace!(?nonce, "responding to heartbeat"); trace!(?nonce, "responding to heartbeat");

View File

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