Skip to content

Commit 6f3c7a8

Browse files
committed
Make logging 30% more compact
1 parent edbccba commit 6f3c7a8

File tree

3 files changed

+62
-31
lines changed

3 files changed

+62
-31
lines changed

distribution/lambda/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ bench-index:
111111
done
112112

113113
bench-search-term:
114-
export QW_LAMBDA_LOG_SPAN_BOUNDARIES=true
114+
export QW_LAMBDA_ENABLE_VERBOSE_JSON_LOGS=true
115115
mem_sizes=( 1024 2048 4096 8192 )
116116
for mem_size in "$${mem_sizes[@]}"
117117
do
@@ -121,7 +121,7 @@ bench-search-term:
121121
done
122122

123123
bench-search-histogram:
124-
export QW_LAMBDA_LOG_SPAN_BOUNDARIES=true
124+
export QW_LAMBDA_ENABLE_VERBOSE_JSON_LOGS=true
125125
mem_sizes=( 1024 2048 4096 8192 )
126126
for mem_size in "$${mem_sizes[@]}"
127127
do

quickwit/quickwit-lambda/src/environment.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ use once_cell::sync::Lazy;
2424
pub static INDEX_ID: Lazy<String> =
2525
Lazy::new(|| var("QW_LAMBDA_INDEX_ID").expect("QW_LAMBDA_INDEX_ID must be set"));
2626

27-
/// Configure the fmt tracing subscriber to log span boundaries. This is very verbose and is
28-
/// only used to generate advanced KPIs from Lambda runs (e.g for blogpost benchmarks)
29-
pub static LOG_SPAN_BOUNDARIES: Lazy<bool> =
30-
Lazy::new(|| var("QW_LAMBDA_LOG_SPAN_BOUNDARIES").is_ok_and(|v| v.as_str() == "true"));
27+
/// Configure the fmt tracing subscriber to log as json and include span
28+
/// boundaries. This is very verbose and is only used to generate advanced KPIs
29+
/// from Lambda runs (e.g for blog post benchmarks)
30+
pub static ENABLE_VERBOSE_JSON_LOGS: Lazy<bool> =
31+
Lazy::new(|| var("QW_LAMBDA_ENABLE_VERBOSE_JSON_LOGS").is_ok_and(|v| v.as_str() == "true"));
3132

3233
pub static OPENTELEMETRY_URL: Lazy<Option<String>> =
3334
Lazy::new(|| var("QW_LAMBDA_OPENTELEMETRY_URL").ok());

quickwit/quickwit-lambda/src/logger.rs

+55-25
Original file line numberDiff line numberDiff line change
@@ -32,46 +32,76 @@ use tracing_subscriber::prelude::*;
3232
use tracing_subscriber::registry::LookupSpan;
3333
use tracing_subscriber::{EnvFilter, Layer};
3434

35-
use crate::environment::{LOG_SPAN_BOUNDARIES, OPENTELEMETRY_AUTHORIZATION, OPENTELEMETRY_URL};
35+
use crate::environment::{
36+
ENABLE_VERBOSE_JSON_LOGS, OPENTELEMETRY_AUTHORIZATION, OPENTELEMETRY_URL,
37+
};
3638

3739
static TRACER_PROVIDER: OnceCell<Option<TracerProvider>> = OnceCell::new();
3840
pub(crate) const RUNTIME_CONTEXT_SPAN: &str = "runtime_context";
3941

40-
fn fmt_layer<S>(level: Level) -> impl Layer<S>
41-
where
42-
S: for<'a> LookupSpan<'a>,
43-
S: tracing::Subscriber,
44-
{
42+
fn fmt_env_filter(level: Level) -> EnvFilter {
4543
let default_filter = format!("quickwit={level}")
4644
.parse()
4745
.expect("Invalid default filter");
48-
let env_filter = EnvFilter::builder()
46+
EnvFilter::builder()
4947
.with_default_directive(default_filter)
50-
.from_env_lossy();
48+
.from_env_lossy()
49+
}
50+
51+
fn fmt_time_format() -> UtcTime<std::vec::Vec<time::format_description::FormatItem<'static>>> {
52+
// We do not rely on the Rfc3339 implementation, because it has a nanosecond precision.
53+
// See discussion here: https://github.com/time-rs/time/discussions/418
54+
UtcTime::new(
55+
time::format_description::parse(
56+
"[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:3]Z",
57+
)
58+
.expect("Time format invalid."),
59+
)
60+
}
61+
62+
fn compact_fmt_layer<S>(level: Level) -> impl Layer<S>
63+
where
64+
S: for<'a> LookupSpan<'a>,
65+
S: tracing::Subscriber,
66+
{
5167
let event_format = tracing_subscriber::fmt::format()
5268
.with_target(true)
53-
.with_timer(
54-
// We do not rely on the Rfc3339 implementation, because it has a nanosecond precision.
55-
// See discussion here: https://github.com/time-rs/time/discussions/418
56-
UtcTime::new(
57-
time::format_description::parse(
58-
"[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:3]Z",
59-
)
60-
.expect("Time format invalid."),
61-
),
62-
)
69+
.with_timer(fmt_time_format())
70+
.compact();
71+
72+
tracing_subscriber::fmt::layer::<S>()
73+
.event_format(event_format)
74+
.with_ansi(false)
75+
.with_filter(fmt_env_filter(level))
76+
}
77+
78+
fn json_fmt_layer<S>(level: Level) -> impl Layer<S>
79+
where
80+
S: for<'a> LookupSpan<'a>,
81+
S: tracing::Subscriber,
82+
{
83+
let event_format = tracing_subscriber::fmt::format()
84+
.with_target(true)
85+
.with_timer(fmt_time_format())
6386
.json();
64-
let fmt_span = if *LOG_SPAN_BOUNDARIES {
65-
FmtSpan::NEW | FmtSpan::CLOSE
66-
} else {
67-
FmtSpan::NONE
68-
};
6987
tracing_subscriber::fmt::layer::<S>()
70-
.with_span_events(fmt_span)
88+
.with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
7189
.event_format(event_format)
7290
.fmt_fields(JsonFields::default())
7391
.with_ansi(false)
74-
.with_filter(env_filter)
92+
.with_filter(fmt_env_filter(level))
93+
}
94+
95+
fn fmt_layer<S>(level: Level) -> Box<dyn Layer<S> + Send + Sync + 'static>
96+
where
97+
S: for<'a> LookupSpan<'a>,
98+
S: tracing::Subscriber,
99+
{
100+
if *ENABLE_VERBOSE_JSON_LOGS {
101+
json_fmt_layer(level).boxed()
102+
} else {
103+
compact_fmt_layer(level).boxed()
104+
}
75105
}
76106

77107
fn otlp_layer<S>(

0 commit comments

Comments
 (0)