From 2db670308bb58b090440a16d2967c54146ab8318 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 27 Apr 2022 15:26:54 -0600 Subject: [PATCH] Allow tracing level to be customized --- crates/collab/src/main.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/crates/collab/src/main.rs b/crates/collab/src/main.rs index 09467558da..82176a53ca 100644 --- a/crates/collab/src/main.rs +++ b/crates/collab/src/main.rs @@ -11,6 +11,7 @@ use std::{ net::{SocketAddr, TcpListener}, sync::Arc, }; +use tracing::metadata::LevelFilter; #[derive(Default, Deserialize)] pub struct Config { @@ -19,6 +20,7 @@ pub struct Config { pub api_token: String, pub honeycomb_api_key: Option, pub honeycomb_dataset: Option, + pub trace_level: Option, } pub struct AppState { @@ -56,12 +58,6 @@ async fn main() -> Result<()> { init_tracing(&config); let state = AppState::new(&config).await?; - { - let root = tracing::span!(tracing::Level::TRACE, "testing_1", work_units = 2_i32); - let _enter = root.enter(); - tracing::error!("test_error_1"); - } - let listener = TcpListener::bind(&format!("0.0.0.0:{}", config.http_port)) .expect("failed to bind TCP listener"); @@ -124,6 +120,7 @@ impl std::fmt::Display for Error { pub fn init_tracing(config: &Config) -> Option<()> { use opentelemetry::KeyValue; use opentelemetry_otlp::WithExportConfig; + use std::str::FromStr; use tracing_opentelemetry::OpenTelemetryLayer; use tracing_subscriber::layer::SubscriberExt; @@ -153,7 +150,15 @@ pub fn init_tracing(config: &Config) -> Option<()> { let subscriber = tracing_subscriber::Registry::default() .with(OpenTelemetryLayer::new(tracer)) - .with(tracing_subscriber::fmt::layer()); + .with(tracing_subscriber::fmt::layer()) + .with( + config + .trace_level + .as_ref() + .map_or(LevelFilter::INFO, |level| { + LevelFilter::from_str(level).unwrap() + }), + ); tracing::subscriber::set_global_default(subscriber).unwrap();