From fb246ac34391e7b50d0ee7337ba8b4249a935e7c Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 17 May 2022 09:11:43 -0600 Subject: [PATCH] Log JSON in Kubernetes If you set LOG_JSON=true, we'll output JSON from the tracing subscriber instead of pretty-printing trace output. Co-Authored-By: Antonio Scandurra --- Cargo.lock | 13 +++++++++++++ crates/collab/Cargo.toml | 2 +- crates/collab/k8s/manifest.template.yml | 2 ++ crates/collab/src/main.rs | 24 +++++++++++++++++++----- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c12a15ec57..2dff791fea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5333,6 +5333,16 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "tracing-serde" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc6b213177105856957181934e4920de57730fc69bf42c37ee5bb664d406d9e1" +dependencies = [ + "serde", + "tracing-core", +] + [[package]] name = "tracing-subscriber" version = "0.3.11" @@ -5343,12 +5353,15 @@ dependencies = [ "lazy_static", "matchers", "regex", + "serde", + "serde_json", "sharded-slab", "smallvec", "thread_local", "tracing", "tracing-core", "tracing-log", + "tracing-serde", ] [[package]] diff --git a/crates/collab/Cargo.toml b/crates/collab/Cargo.toml index 3ed2beca14..db8386330a 100644 --- a/crates/collab/Cargo.toml +++ b/crates/collab/Cargo.toml @@ -45,7 +45,7 @@ toml = "0.5.8" tracing = "0.1.34" tracing-log = "0.1.3" tracing-opentelemetry = "0.17" -tracing-subscriber = { version = "0.3.11", features = ["env-filter"] } +tracing-subscriber = { version = "0.3.11", features = ["env-filter", "json"] } [dependencies.sqlx] version = "0.5.2" diff --git a/crates/collab/k8s/manifest.template.yml b/crates/collab/k8s/manifest.template.yml index 73b20409fd..861bd5e783 100644 --- a/crates/collab/k8s/manifest.template.yml +++ b/crates/collab/k8s/manifest.template.yml @@ -83,6 +83,8 @@ spec: key: token - name: RUST_LOG value: ${RUST_LOG} + - name: LOG_JSON + value: "true" - name: HONEYCOMB_DATASET value: "collab" - name: HONEYCOMB_API_KEY diff --git a/crates/collab/src/main.rs b/crates/collab/src/main.rs index bd9f8fd69d..36a902565f 100644 --- a/crates/collab/src/main.rs +++ b/crates/collab/src/main.rs @@ -12,7 +12,7 @@ use std::{ sync::Arc, }; use tracing_log::LogTracer; -use tracing_subscriber::filter::EnvFilter; +use tracing_subscriber::{filter::EnvFilter, fmt::format::JsonFields, Layer}; use util::ResultExt; #[derive(Default, Deserialize)] @@ -23,6 +23,7 @@ pub struct Config { pub honeycomb_api_key: Option, pub honeycomb_dataset: Option, pub rust_log: Option, + pub log_json: Option, } pub struct AppState { @@ -152,10 +153,23 @@ pub fn init_tracing(config: &Config) -> Option<()> { let subscriber = tracing_subscriber::Registry::default() .with(open_telemetry_layer) - .with( - tracing_subscriber::fmt::layer() - .event_format(tracing_subscriber::fmt::format().pretty()), - ) + .with(if config.log_json.unwrap_or(false) { + Box::new( + tracing_subscriber::fmt::layer() + .fmt_fields(JsonFields::default()) + .event_format( + tracing_subscriber::fmt::format() + .json() + .flatten_event(true) + .with_span_list(true), + ), + ) as Box + Send + Sync> + } else { + Box::new( + tracing_subscriber::fmt::layer() + .event_format(tracing_subscriber::fmt::format().pretty()), + ) + }) .with(EnvFilter::from_str(rust_log.as_str()).log_err()?); tracing::subscriber::set_global_default(subscriber).unwrap();