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 500-character BufWriter buffer to prevent
excessive syscalls.
  • Loading branch information
hcstern committed Aug 12, 2024
1 parent f0f0d74 commit eb785a2
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 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 500 characters
const BUF_SIZE: usize = 500;

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 Expand Up @@ -165,6 +171,7 @@ where
JsonDrain::new(output)
.add_default_keys()
.set_pretty(false)
.set_flush(true)
.build()
.fuse()
}

0 comments on commit eb785a2

Please sign in to comment.