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 <me@as-cii.com>
This commit is contained in:
Nathan Sobo 2022-05-17 09:11:43 -06:00
parent ad94b4cc73
commit fb246ac343
4 changed files with 35 additions and 6 deletions

13
Cargo.lock generated
View File

@ -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]]

View File

@ -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"

View File

@ -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

View File

@ -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<String>,
pub honeycomb_dataset: Option<String>,
pub rust_log: Option<String>,
pub log_json: Option<bool>,
}
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<dyn Layer<_> + 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();