zebrad: pass TracingSection to Tracing component

This commit is contained in:
Henry de Valence 2020-11-30 11:59:40 -08:00
parent 4544463059
commit e8c16b172f
2 changed files with 19 additions and 9 deletions

View File

@ -139,16 +139,24 @@ impl Application for ZebradApp {
.map(ZebradCmd::is_server) .map(ZebradCmd::is_server)
.unwrap_or(false); .unwrap_or(false);
// Launch network endpoints for long-running commands // Launch network endpoints only for long-running commands.
if is_server { if is_server {
let filter = cfg_ref.tracing.filter.as_deref().unwrap_or(default_filter); // Override the default tracing filter based on the command-line verbosity.
let flame_root = cfg_ref.tracing.flamegraph.as_deref(); let mut tracing_config = cfg_ref.tracing.clone();
components.push(Box::new(Tracing::new(filter, flame_root)?)); tracing_config.filter = tracing_config
.filter
.or_else(|| Some(default_filter.to_owned()));
components.push(Box::new(Tracing::new(tracing_config)?));
components.push(Box::new(TokioComponent::new()?)); components.push(Box::new(TokioComponent::new()?));
components.push(Box::new(TracingEndpoint::new(cfg_ref)?)); components.push(Box::new(TracingEndpoint::new(cfg_ref)?));
components.push(Box::new(MetricsEndpoint::new(cfg_ref)?)); components.push(Box::new(MetricsEndpoint::new(cfg_ref)?));
} else { } else {
components.push(Box::new(Tracing::new(default_filter, None)?)); // Don't apply the configured filter for short-lived commands.
let mut tracing_config = cfg_ref.tracing.clone();
tracing_config.filter = Some(default_filter.to_owned());
tracing_config.flamegraph = None;
components.push(Box::new(Tracing::new(tracing_config)?));
} }
self.state.components.register(components) self.state.components.register(components)

View File

@ -1,12 +1,11 @@
use std::path::Path; use abscissa_core::{Component, FrameworkError, FrameworkErrorKind, Shutdown};
use tracing_error::ErrorLayer; use tracing_error::ErrorLayer;
use tracing_subscriber::{ use tracing_subscriber::{
fmt::Formatter, layer::SubscriberExt, reload::Handle, util::SubscriberInitExt, EnvFilter, fmt::Formatter, layer::SubscriberExt, reload::Handle, util::SubscriberInitExt, EnvFilter,
FmtSubscriber, FmtSubscriber,
}; };
use abscissa_core::{Component, FrameworkError, FrameworkErrorKind, Shutdown}; use crate::config::TracingSection;
use super::flame; use super::flame;
@ -18,7 +17,10 @@ pub struct Tracing {
impl Tracing { impl Tracing {
/// Try to create a new [`Tracing`] component with the given `filter`. /// Try to create a new [`Tracing`] component with the given `filter`.
pub fn new(filter: &str, flame_root: Option<&Path>) -> Result<Self, FrameworkError> { pub fn new(config: TracingSection) -> Result<Self, FrameworkError> {
let filter = config.filter.unwrap_or_else(|| "".to_string());
let flame_root = &config.flamegraph;
// Construct a tracing subscriber with the supplied filter and enable reloading. // Construct a tracing subscriber with the supplied filter and enable reloading.
let builder = FmtSubscriber::builder() let builder = FmtSubscriber::builder()
.with_ansi(true) .with_ansi(true)