Skip to content

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

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

Merged
merged 4 commits into from
Jul 14, 2025
Merged
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
3 changes: 3 additions & 0 deletions changelog.d/batch-newline.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Batches encoded using newline delimited framing now end with a trailing newline.

authors: jszwedko
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] {
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 @@ -323,4 +325,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");
}
}
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 @@ -65,7 +66,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 @@ -295,9 +296,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 @@ -332,11 +333,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