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

fix(codecs): Ensure that batches using newline delimited framing end in a newline #21097

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions changelog.d/batch-newline.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Batches encoded using newline delimited framing now end with a trailing newline.
25 changes: 23 additions & 2 deletions src/codecs/encoding/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,14 @@ impl Encoder<Framer> {
}

/// Get the suffix that encloses a batch of events.
pub const fn batch_suffix(&self) -> &[u8] {
match (&self.framer, &self.serializer) {
pub const fn batch_suffix(&self, empty: bool) -> &[u8] {
Copy link
Member Author

@jszwedko jszwedko Aug 19, 2024

Choose a reason for hiding this comment

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

I think ideally the encoding, for newline delimited framing, would append the newline while encoding each element (including the last) rather than adding it when finalizing the batch, but that was a bit more refactoring than I was willing to take on at the moment.

match (&self.framer, &self.serializer, empty) {
(
Framer::CharacterDelimited(CharacterDelimitedEncoder { delimiter: b',' }),
Serializer::Json(_) | Serializer::NativeJson(_),
_,
) => b"]",
(Framer::NewlineDelimited(_), _, false) => b"\n",
_ => &[],
}
}
Expand Down Expand Up @@ -322,4 +324,23 @@ mod tests {
let sink = framed.into_inner();
assert_eq!(sink, b"(foo)(bar)");
}

#[tokio::test]
async fn test_encode_batch_newline() {
let encoder = Encoder::<Framer>::new(
Framer::NewlineDelimited(NewlineDelimitedEncoder::default()),
TextSerializerConfig::default().build().into(),
);
let source = futures::stream::iter(vec![
Event::Log(LogEvent::from("bar")),
Event::Log(LogEvent::from("baz")),
Event::Log(LogEvent::from("bat")),
])
.map(Ok);
let sink: Vec<u8> = Vec::new();
let mut framed = FramedWrite::new(sink, encoder);
source.forward(&mut framed).await.unwrap();
let sink = framed.into_inner();
assert_eq!(sink, b"bar\nbaz\nbat\n");
neuronull marked this conversation as resolved.
Show resolved Hide resolved
}
}
11 changes: 6 additions & 5 deletions src/sinks/util/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ impl Encoder<Vec<Event>> for (Transformer, crate::codecs::Encoder<Framer>) {
let mut encoder = self.1.clone();
let mut bytes_written = 0;
let mut n_events_pending = events.len();
let is_empty = events.is_empty();
let batch_prefix = encoder.batch_prefix();
write_all(writer, n_events_pending, batch_prefix)?;
bytes_written += batch_prefix.len();
Expand Down Expand Up @@ -62,7 +63,7 @@ impl Encoder<Vec<Event>> for (Transformer, crate::codecs::Encoder<Framer>) {
n_events_pending -= 1;
}

let batch_suffix = encoder.batch_suffix();
let batch_suffix = encoder.batch_suffix(is_empty);
assert!(n_events_pending == 0);
write_all(writer, 0, batch_suffix)?;
bytes_written += batch_suffix.len();
Expand Down Expand Up @@ -289,9 +290,9 @@ mod tests {
.sum::<JsonSize>();

let (written, json_size) = encoding.encode_input(input, &mut writer).unwrap();
assert_eq!(written, 15);
assert_eq!(written, 16);

assert_eq!(String::from_utf8(writer).unwrap(), r#"{"key":"value"}"#);
assert_eq!(String::from_utf8(writer).unwrap(), "{\"key\":\"value\"}\n");
assert_eq!(CountByteSize(1, input_json_size), json_size.size().unwrap());
}

Expand Down Expand Up @@ -326,11 +327,11 @@ mod tests {
.sum::<JsonSize>();

let (written, json_size) = encoding.encode_input(input, &mut writer).unwrap();
assert_eq!(written, 50);
assert_eq!(written, 51);

assert_eq!(
String::from_utf8(writer).unwrap(),
"{\"key\":\"value1\"}\n{\"key\":\"value2\"}\n{\"key\":\"value3\"}"
"{\"key\":\"value1\"}\n{\"key\":\"value2\"}\n{\"key\":\"value3\"}\n"
);
assert_eq!(CountByteSize(3, input_json_size), json_size.size().unwrap());
}
Expand Down
Loading