improve tracing

This commit is contained in:
nora 2022-03-07 15:15:47 +01:00
parent bd5d4c03c5
commit 08fa9163b8
14 changed files with 273 additions and 150 deletions

View file

@ -2,7 +2,9 @@
use anyhow::Result;
use clap::Parser;
use std::str::FromStr;
use tracing::{info, info_span, Instrument};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry};
/// An AMQP 0-9-1 broker implementation.
#[derive(Parser)]
@ -10,9 +12,10 @@ struct Args {
/// Whether to serve the dashboard on localhost. Port defaults to 3000.
#[clap(short, long)]
dashboard: bool,
/// The log level of the application. Overwrites the `RUST_LOG` env var.
/// Displays logs in a flat structure, otherwise as a tree
#[clap(long)]
log_level: Option<String>,
flat_log: bool,
}
#[tokio::main]
@ -24,29 +27,49 @@ async fn main() -> Result<()> {
let global_data = amqp_core::GlobalData::default();
if args.dashboard {
let dashboard_span = info_span!("dashboard");
tokio::spawn(amqp_dashboard::dashboard(global_data.clone()).instrument(dashboard_span));
let global_data = global_data.clone();
tokio::spawn(async move {
amqp_dashboard::dashboard(global_data)
.instrument(info_span!("dashboard"))
.await
});
}
amqp_transport::do_thing_i_guess(global_data).await
let res = amqp_transport::do_thing_i_guess(global_data, terminate()).await;
info!("Bye!");
res
}
fn setup_tracing(args: &Args) {
const DEFAULT_LOG: &str = "hyper=info,debug";
const DEFAULT_LOG: &str = "hyper=info,debug"; // set hyper to info because I really don't care about hyper
let log_filter = args
.log_level
.clone()
.or_else(|| std::env::var("RUST_LOG").ok())
.unwrap_or_else(|| DEFAULT_LOG.to_owned());
let log_filter = std::env::var("RUST_LOG").unwrap_or_else(|_| DEFAULT_LOG.to_owned());
tracing_subscriber::fmt()
.with_level(true)
.with_timer(tracing_subscriber::fmt::time::time())
.with_ansi(true)
.with_thread_names(true)
.with_env_filter(&log_filter)
.init();
let registry = Registry::default().with(EnvFilter::from_str(&log_filter).unwrap());
if args.flat_log {
let fmt_layer = tracing_subscriber::fmt::layer()
.with_level(true)
.with_timer(tracing_subscriber::fmt::time::time())
.with_ansi(true)
.with_thread_names(true);
registry.with(fmt_layer).init();
} else {
let tree_layer = tracing_tree::HierarchicalLayer::new(2)
.with_targets(true)
.with_bracketed_fields(true);
registry.with(tree_layer).init();
};
info!(%log_filter, "Using log filter level");
}
async fn terminate() {
tokio::signal::ctrl_c()
.await
.expect("failed to install ctrl-c signal handler");
}