Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffer JSON slog messages #62

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions foundations/src/telemetry/log/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use slog_json::{Json as JsonDrain, Json};
use slog_term::{FullFormat as TextDrain, PlainDecorator, TermDecorator};
use std::fs::File;
use std::io;
use std::io::BufWriter;
use std::panic::RefUnwindSafe;
use std::sync::Arc;

Expand Down Expand Up @@ -70,6 +71,10 @@ pub(crate) fn init(service_info: &ServiceInfo, settings: &LoggingSettings) -> Bo
// NOTE: OXY-178, default is 128 (https://docs.rs/slog-async/2.7.0/src/slog_async/lib.rs.html#251)
const CHANNEL_SIZE: usize = 1024;

// buffer json log lines up to 4kb characters. `set_flush` is enabled on the `JsonDrain` below
// so messages less than 4k will still be written even if the buffer isn't full.
const BUF_SIZE: usize = 4096;

let base_drain = match (&settings.output, &settings.format) {
(LogOutput::Terminal, LogFormat::Text) => {
let drain = TextDrain::new(TermDecorator::new().stdout().build())
Expand All @@ -78,7 +83,8 @@ pub(crate) fn init(service_info: &ServiceInfo, settings: &LoggingSettings) -> Bo
AsyncDrain::new(drain).chan_size(CHANNEL_SIZE).build()
}
(LogOutput::Terminal, LogFormat::Json) => {
let drain = build_json_log_drain(io::stdout());
let buf = BufWriter::with_capacity(BUF_SIZE, io::stdout());
let drain = build_json_log_drain(buf);
AsyncDrain::new(drain).chan_size(CHANNEL_SIZE).build()
}
(LogOutput::File(file), LogFormat::Text) => {
Expand All @@ -88,7 +94,8 @@ pub(crate) fn init(service_info: &ServiceInfo, settings: &LoggingSettings) -> Bo
AsyncDrain::new(drain).chan_size(CHANNEL_SIZE).build()
}
(LogOutput::File(file), LogFormat::Json) => {
let drain = build_json_log_drain(File::create(file)?);
let buf = BufWriter::with_capacity(BUF_SIZE, File::create(file)?);
let drain = build_json_log_drain(buf);
AsyncDrain::new(drain).chan_size(CHANNEL_SIZE).build()
}
};
Expand Down Expand Up @@ -165,6 +172,7 @@ where
JsonDrain::new(output)
.add_default_keys()
.set_pretty(false)
.set_flush(true)
.build()
.fuse()
}