Skip to content

Commit

Permalink
Buffer JSON slog messages
Browse files Browse the repository at this point in the history
serde_json will happily do a `write` syscall for every token it
serializes. Add a small 100-character BufWriter buffer to prevent
excessive syscalls.

Note that serde_json does not appear to call `Write::flush` so short
lines will not be printed until the buffer is full.
  • Loading branch information
hcstern committed Aug 12, 2024
1 parent f0f0d74 commit fea4d81
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 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,9 @@ 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 100 characters
const BUF_SIZE: usize = 100;

let base_drain = match (&settings.output, &settings.format) {
(LogOutput::Terminal, LogFormat::Text) => {
let drain = TextDrain::new(TermDecorator::new().stdout().build())
Expand All @@ -78,7 +82,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 +93,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

0 comments on commit fea4d81

Please sign in to comment.