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

Conversation

hcstern
Copy link
Contributor

@hcstern hcstern commented Aug 12, 2024

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.

@hcstern
Copy link
Contributor Author

hcstern commented Aug 12, 2024

The non-flushing behavior is probably unexpected and might make debugging confusing but I don't think is a problem in prod. Would it make sense to only turn on the bufwriter behavior with a release profile?

@inikulin
Copy link
Collaborator

The non-flushing behavior is probably unexpected and might make debugging confusing but I don't think is a problem in prod. Would it make sense to only turn on the bufwriter behavior with a release profile?

I think it's fine if we enable https://docs.rs/slog-json/latest/slog_json/struct.JsonBuilder.html#method.set_flush

@hcstern
Copy link
Contributor Author

hcstern commented Aug 12, 2024

The non-flushing behavior is probably unexpected and might make debugging confusing but I don't think is a problem in prod. Would it make sense to only turn on the bufwriter behavior with a release profile?

I think it's fine if we enable https://docs.rs/slog-json/latest/slog_json/struct.JsonBuilder.html#method.set_flush

Nice catch, thanks! I'll increase the buffer size as well.

@@ -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;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With lots of keys in json it's easy to exceed this. I'd go with something like 4k.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, didn't see this last night and it didn't go to my cloudflare email for some reason even though I added it to this account. Thanks again!

@inikulin inikulin self-requested a review August 13, 2024 11:33
serde_json will happily do a `write` syscall for every token it
serializes. Add a std::io::BufWriter to prevent excessive syscalls.
@inikulin inikulin merged commit 69aa541 into cloudflare:main Aug 14, 2024
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants