From a91d0f0bb6841d29ea911d3631bd07eb2518b055 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Tue, 1 Dec 2020 12:13:20 -0800 Subject: [PATCH] Include short sha in log messages and error urls (#1410) As we approach our alpha release we've decided we want to plan ahead for the user bug reports we will eventually receive. One of the bigger issues we foresee is determining exactly what version of the software users are running, and particularly how easy it may or may not be for users to accidentally discard this information when reporting bugs. To defend against this, we've decided to include the exact git sha for any given build in the compiled artifact. This information will then be re-exported as a span early in the application startup process, so that all logs and error messages should include the sha as their very first span. We've also added this sha as issue metadata for `color-eyre`'s github issue url auto generation feature, which should make sure that the sha is easily available in bug reports we receive, even in the absence of logs. Co-authored-by: teor --- Cargo.lock | 11 +++++++++++ zebra-network/src/peer/handshake.rs | 7 +++++-- zebrad/Cargo.toml | 3 +++ zebrad/build.rs | 13 +++++++++++++ zebrad/src/application.rs | 5 +++++ zebrad/src/commands.rs | 16 +++++++++++++++- 6 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 zebrad/build.rs diff --git a/Cargo.lock b/Cargo.lock index 98e047b9..073614bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3019,6 +3019,16 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" +[[package]] +name = "vergen" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ce50d8996df1f85af15f2cd8d33daae6e479575123ef4314a51a70a230739cb" +dependencies = [ + "bitflags", + "chrono", +] + [[package]] name = "version_check" version = "0.1.5" @@ -3345,6 +3355,7 @@ dependencies = [ "tracing-flame", "tracing-futures", "tracing-subscriber 0.2.15", + "vergen", "zebra-chain", "zebra-consensus", "zebra-network", diff --git a/zebra-network/src/peer/handshake.rs b/zebra-network/src/peer/handshake.rs index 3e232b06..ed80a610 100644 --- a/zebra-network/src/peer/handshake.rs +++ b/zebra-network/src/peer/handshake.rs @@ -15,7 +15,7 @@ use futures::{ use tokio::{net::TcpStream, sync::broadcast}; use tokio_util::codec::Framed; use tower::Service; -use tracing::{span, Level}; +use tracing::{span, Level, Span}; use tracing_futures::Instrument; use zebra_chain::block; @@ -44,6 +44,7 @@ pub struct Handshake { user_agent: String, our_services: PeerServices, relay: bool, + parent_span: Span, } pub struct Builder { @@ -136,6 +137,7 @@ where let user_agent = self.user_agent.unwrap_or_else(|| "".to_string()); let our_services = self.our_services.unwrap_or_else(PeerServices::empty); let relay = self.relay.unwrap_or(false); + Ok(Handshake { config, inbound_service, @@ -145,6 +147,7 @@ where user_agent, our_services, relay, + parent_span: Span::current(), }) } } @@ -192,7 +195,7 @@ where // set parent: None for the peer connection span, as it should exist // independently of its creation source (inbound connection, crawler, // initial peer, ...) - let connection_span = span!(parent: None, Level::INFO, "peer", addr = ?addr); + let connection_span = span!(parent: &self.parent_span, Level::INFO, "peer", addr = ?addr); // Clone these upfront, so they can be moved into the future. let nonces = self.nonces.clone(); diff --git a/zebrad/Cargo.toml b/zebrad/Cargo.toml index 89199035..808189b2 100644 --- a/zebrad/Cargo.toml +++ b/zebrad/Cargo.toml @@ -39,6 +39,9 @@ metrics-exporter-prometheus = "0.1.0-alpha.7" dirs = "3.0.1" inferno = { version = "0.10.2", default-features = false } +[build-dependencies] +vergen = "3.1.0" + [dev-dependencies] abscissa_core = { version = "0.5", features = ["testing"] } once_cell = "1.5" diff --git a/zebrad/build.rs b/zebrad/build.rs new file mode 100644 index 00000000..34c707c3 --- /dev/null +++ b/zebrad/build.rs @@ -0,0 +1,13 @@ +extern crate vergen; + +use vergen::{generate_cargo_keys, ConstantsFlags}; + +fn main() { + // Setup the flags, toggling off the 'SEMVER_FROM_CARGO_PKG' flag + let mut flags = ConstantsFlags::empty(); + flags.toggle(ConstantsFlags::SHA_SHORT); + flags.toggle(ConstantsFlags::REBUILD_ON_HEAD_CHANGE); + + // Generate the 'cargo:' key output + generate_cargo_keys(flags).expect("Unable to generate the cargo keys!"); +} diff --git a/zebrad/src/application.rs b/zebrad/src/application.rs index 24956341..916ee1e4 100644 --- a/zebrad/src/application.rs +++ b/zebrad/src/application.rs @@ -43,6 +43,10 @@ pub struct ZebradApp { state: application::State, } +impl ZebradApp { + pub const GIT_COMMIT: &'static str = env!("VERGEN_SHA_SHORT"); +} + /// Initialize a new application instance. /// /// By default no configuration is loaded, and the framework state is @@ -92,6 +96,7 @@ impl Application for ZebradApp { color_eyre::config::HookBuilder::default() .issue_url(concat!(env!("CARGO_PKG_REPOSITORY"), "/issues/new")) .add_issue_metadata("version", env!("CARGO_PKG_VERSION")) + .add_issue_metadata("git commit", Self::GIT_COMMIT) .issue_filter(|kind| match kind { color_eyre::ErrorKind::NonRecoverable(_) => true, color_eyre::ErrorKind::Recoverable(error) => { diff --git a/zebrad/src/commands.rs b/zebrad/src/commands.rs index 244b9776..ab5d5ac4 100644 --- a/zebrad/src/commands.rs +++ b/zebrad/src/commands.rs @@ -8,6 +8,7 @@ use self::ZebradCmd::*; use self::{generate::GenerateCmd, start::StartCmd, version::VersionCmd}; use crate::config::ZebradConfig; +use crate::application::ZebradApp; use abscissa_core::{ config::Override, Command, Configurable, FrameworkError, Help, Options, Runnable, @@ -18,7 +19,7 @@ use std::path::PathBuf; pub const CONFIG_FILE: &str = "zebrad.toml"; /// Zebrad Subcommands -#[derive(Command, Debug, Options, Runnable)] +#[derive(Command, Debug, Options)] pub enum ZebradCmd { /// The `generate` subcommand #[options(help = "generate a skeleton configuration")] @@ -50,6 +51,19 @@ impl ZebradCmd { } } +impl Runnable for ZebradCmd { + fn run(&self) { + let span = error_span!("", zebrad = ZebradApp::GIT_COMMIT); + let _guard = span.enter(); + match self { + Generate(cmd) => cmd.run(), + ZebradCmd::Help(cmd) => cmd.run(), + Start(cmd) => cmd.run(), + Version(cmd) => cmd.run(), + } + } +} + /// This trait allows you to define how application configuration is loaded. impl Configurable for ZebradCmd { /// Location of the configuration file