From 18b4dbc16cbde5f2feb55faea0c090be78fffc27 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Thu, 4 Jun 2020 19:34:06 -0700 Subject: [PATCH] fix tracing configuration issues (#432) --- Cargo.lock | 74 ++++++++++------------------------- Cargo.toml | 5 ++- zebrad/Cargo.toml | 2 + zebrad/src/application.rs | 68 ++++++++++++++++++++++++++++---- zebrad/src/commands/config.rs | 6 ++- zebrad/src/commands/start.rs | 6 ++- zebrad/src/config.rs | 11 +++--- 7 files changed, 101 insertions(+), 71 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5acbf967..f28d1d66 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,36 +3,34 @@ [[package]] name = "abscissa_core" version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a07677093120a02583717b6dd1ef81d8de1e8d01bd226c83f0f9bdf3e56bb3a" +source = "git+https://github.com/yaahc/abscissa.git?branch=develop#41d342a9344e38442b2211b07f28a89505892a21" dependencies = [ "abscissa_derive", "backtrace", "canonical-path", "chrono", - "color-backtrace 0.3.0", + "color-backtrace", "generational-arena", "gumdrop", "libc", "once_cell", "regex", "secrecy", - "semver", + "semver 0.10.0", "serde", "signal-hook", "termcolor", "toml", "tracing", "tracing-log", - "tracing-subscriber 0.1.6", + "tracing-subscriber", "wait-timeout", ] [[package]] name = "abscissa_derive" version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74f5722bc48763cb9d81d8427ca05b6aa2842f6632cf8e4c0a29eef9baececcc" +source = "git+https://github.com/yaahc/abscissa.git?branch=develop#41d342a9344e38442b2211b07f28a89505892a21" dependencies = [ "darling", "ident_case", @@ -268,17 +266,6 @@ dependencies = [ "bitflags", ] -[[package]] -name = "color-backtrace" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65d13f1078cc63c791d0deba0dd43db37c9ec02b311f10bed10b577016f3a957" -dependencies = [ - "atty", - "backtrace", - "termcolor", -] - [[package]] name = "color-backtrace" version = "0.4.2" @@ -298,7 +285,7 @@ checksum = "2aaa5f071a62a9c06ebab653ec2e2ba76cb936548a83e7af7f0c5dac525067ff" dependencies = [ "ansi_term", "backtrace", - "color-backtrace 0.4.2", + "color-backtrace", "color-spantrace", "eyre", "indenter", @@ -1081,15 +1068,6 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" -[[package]] -name = "owning_ref" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ff55baddef9e4ad00f88b6c743a2a8062d4c6ade126c2a528644b8e444d52ce" -dependencies = [ - "stable_deref_trait", -] - [[package]] name = "parking_lot" version = "0.9.0" @@ -1378,7 +1356,7 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" dependencies = [ - "semver", + "semver 0.9.0", ] [[package]] @@ -1439,6 +1417,15 @@ name = "semver" version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +dependencies = [ + "semver-parser", +] + +[[package]] +name = "semver" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "394cec28fa623e00903caf7ba4fa6fb9a0e260280bb8cdbbba029611108a0190" dependencies = [ "semver-parser", "serde", @@ -1589,12 +1576,6 @@ dependencies = [ "tracing-futures", ] -[[package]] -name = "stable_deref_trait" -version = "1.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8" - [[package]] name = "strsim" version = "0.9.3" @@ -1932,7 +1913,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4d7c0b83d4a500748fa5879461652b361edf5c9d51ede2a2ac03875ca185e24" dependencies = [ "tracing", - "tracing-subscriber 0.2.5", + "tracing-subscriber", ] [[package]] @@ -1966,23 +1947,6 @@ dependencies = [ "tracing-core", ] -[[package]] -name = "tracing-subscriber" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "192ca16595cdd0661ce319e8eede9c975f227cdaabc4faaefdc256f43d852e45" -dependencies = [ - "ansi_term", - "chrono", - "lazy_static", - "matchers", - "owning_ref", - "regex", - "smallvec 0.6.13", - "tracing-core", - "tracing-log", -] - [[package]] name = "tracing-subscriber" version = "0.2.5" @@ -2211,7 +2175,7 @@ dependencies = [ "tracing", "tracing-error", "tracing-futures", - "tracing-subscriber 0.2.5", + "tracing-subscriber", "zebra-chain", "zebra-test-vectors", ] @@ -2245,8 +2209,10 @@ dependencies = [ "toml", "tower", "tracing", + "tracing-error", "tracing-futures", "tracing-log", + "tracing-subscriber", "zebra-chain", "zebra-network", "zebra-state", diff --git a/Cargo.toml b/Cargo.toml index 1f601707..440c177d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,4 +15,7 @@ members = [ panic = "abort" [profile.release] -panic = "abort" \ No newline at end of file +panic = "abort" + +[patch.crates-io] +abscissa_core = { git = "https://github.com/yaahc/abscissa.git", branch = "develop" } diff --git a/zebrad/Cargo.toml b/zebrad/Cargo.toml index 9b84638d..1abcb271 100644 --- a/zebrad/Cargo.toml +++ b/zebrad/Cargo.toml @@ -33,6 +33,8 @@ zebra-network = { path = "../zebra-network" } eyre = "0.4.3" color-eyre = "0.3.4" zebra-state = { path = "../zebra-state" } +tracing-subscriber = { version = "0.2.5", features = ["tracing-log"] } +tracing-error = "0.1.2" [dev-dependencies] abscissa_core = { version = "0.5", features = ["testing"] } diff --git a/zebrad/src/application.rs b/zebrad/src/application.rs index 84a3eb7a..f1ddf1a0 100644 --- a/zebrad/src/application.rs +++ b/zebrad/src/application.rs @@ -3,7 +3,10 @@ use crate::{commands::ZebradCmd, config::ZebradConfig}; use abscissa_core::{ application::{self, AppCell}, - config, trace, Application, EntryPoint, FrameworkError, StandardPaths, + config, + terminal::component::Terminal, + trace::Tracing, + Application, Component, EntryPoint, FrameworkError, StandardPaths, }; /// Application state @@ -76,6 +79,16 @@ impl Application for ZebradApp { &mut self.state } + fn framework_components( + &mut self, + command: &Self::Cmd, + ) -> Result>>, FrameworkError> { + let terminal = Terminal::new(self.term_colors(command)); + let tracing = self.tracing_component(command); + + Ok(vec![Box::new(terminal), Box::new(tracing)]) + } + /// Register all components used by this application. /// /// If you would like to add additional components to your application @@ -99,21 +112,60 @@ impl Application for ZebradApp { /// Called regardless of whether config is loaded to indicate this is the /// time in app lifecycle when configuration would be loaded if /// possible. - fn after_config(&mut self, config: Self::Cfg) -> Result<(), FrameworkError> { + fn after_config( + &mut self, + config: Self::Cfg, + command: &Self::Cmd, + ) -> Result<(), FrameworkError> { // Configure components self.state.components.after_config(&config)?; self.config = Some(config); + + let level = self.level(command); + self.state + .components + .get_downcast_mut::() + .expect("Tracing component should be available") + .reload_filter(level); + Ok(()) } +} - /// Get logging configuration from command-line options - fn tracing_config(&self, command: &EntryPoint) -> trace::Config { - if let Ok(env) = std::env::var("ZEBRAD_LOG") { - trace::Config::from(env) +impl ZebradApp { + fn level(&self, command: &EntryPoint) -> String { + if let Ok(level) = std::env::var("ZEBRAD_LOG") { + level } else if command.verbose { - trace::Config::verbose() + "debug".to_string() + } else if let Some(ZebradConfig { + tracing: + crate::config::TracingSection { + filter: Some(filter), + }, + .. + }) = &self.config + { + filter.clone() } else { - trace::Config::default() + "info".to_string() } } + + fn tracing_component(&self, command: &EntryPoint) -> Tracing { + use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; + + // Construct a tracing subscriber with the supplied filter and enable reloading. + let builder = tracing_subscriber::FmtSubscriber::builder() + .with_env_filter(self.level(command)) + .with_filter_reloading(); + let filter_handle = builder.reload_handle(); + + builder + .finish() + .with(tracing_error::ErrorLayer::default()) + .init(); + + filter_handle.into() + } } diff --git a/zebrad/src/commands/config.rs b/zebrad/src/commands/config.rs index 67cb65d8..f54edbd4 100644 --- a/zebrad/src/commands/config.rs +++ b/zebrad/src/commands/config.rs @@ -14,7 +14,11 @@ pub struct ConfigCmd { impl Runnable for ConfigCmd { /// Start the application. fn run(&self) { - let default_config = ZebradConfig::default(); + let default_config = ZebradConfig { + tracing: crate::config::TracingSection::populated(), + network: Default::default(), + metrics: Default::default(), + }; let mut output = r"# Default configuration values for zebrad. # # This file is intended as a skeleton for custom configs. diff --git a/zebrad/src/commands/start.rs b/zebrad/src/commands/start.rs index 4f6ebd26..35c2b67e 100644 --- a/zebrad/src/commands/start.rs +++ b/zebrad/src/commands/start.rs @@ -29,7 +29,9 @@ impl Runnable for StartCmd { fn run(&self) { warn!("starting application"); let config = app_config(); - println!("filter: {}!", &config.tracing.filter); + if let Some(filter) = &config.tracing.filter { + println!("filter: {}!", filter); + } let default_config = ZebradConfig::default(); println!("Default config: {:?}", default_config); @@ -59,7 +61,7 @@ impl config::Override for StartCmd { // arguments. fn override_config(&self, mut config: ZebradConfig) -> Result { if !self.filters.is_empty() { - config.tracing.filter = self.filters.join(","); + config.tracing.filter = Some(self.filters.join(",")); } Ok(config) diff --git a/zebrad/src/config.rs b/zebrad/src/config.rs index d57ef6af..6b4e502a 100644 --- a/zebrad/src/config.rs +++ b/zebrad/src/config.rs @@ -13,6 +13,7 @@ use zebra_network::Config as NetworkSection; /// Zebrad Configuration #[derive(Clone, Default, Debug, Deserialize, Serialize)] #[serde(deny_unknown_fields)] +#[serde(default)] pub struct ZebradConfig { /// Tracing configuration pub tracing: TracingSection, @@ -23,17 +24,17 @@ pub struct ZebradConfig { } /// Tracing configuration section. -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, Deserialize, Serialize)] #[serde(deny_unknown_fields)] pub struct TracingSection { /// The filter used for tracing events. - pub filter: String, + pub filter: Option, } -impl Default for TracingSection { - fn default() -> Self { +impl TracingSection { + pub fn populated() -> Self { Self { - filter: "info".to_owned(), + filter: Some("info".to_owned()), } } }