Skip to content

Fix sending records when the formatter has add_newline false - #95

Merged
Watson1978 merged 3 commits into
fluent-plugins-nursery:masterfrom
lbarrosby:fix-add-newline-false
Nov 18, 2026
Merged

Watson1978 merged 3 commits into
fluent-plugins-nursery:masterfrom
lbarrosby:fix-add-newline-false

Conversation

@lbarrosby

@lbarrosby lbarrosby commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

With add_newline false in <format> (a common choice with single_value, since a syslog message has no trailing newline anyway), a chunk fails when its last record does not end with a line ending, and the records in a chunk are glued together:

[warn]: got unrecoverable error in primary and no secondary error_class=NoMethodError error="undefined method 'split' for nil"
  [warn]: .../remote_syslog_sender-1.2.4/lib/remote_syslog_sender/sender.rb:37:in 'RemoteSyslogSender::Sender#transmit'
  [warn]: .../lib/fluent/plugin/out_remote_syslog.rb:135:in 'block (2 levels) in Fluent::Plugin::RemoteSyslogOutput#write'

Without use_event_time, #format stores the formatter output as is and #write reads the chunk back with io.each_line and msg.chomp!. Without newlines the whole chunk is one line with no "\n" at the end, chomp! returns nil, and transmit(nil) fails. Fluentd treats NoMethodError as unrecoverable, so the chunk goes straight to the <secondary>, or to the backup directory when there is none. Even if it did not fail, the records of a chunk would be glued into a single message.

#format now appends "\n" when the formatted record does not end with one, so #write no longer glues records together whatever add_newline says. Empty output is kept empty, and the newline check and append operate on bytes, so ISO-2022-JP output is kept as is. Records that already end with a newline are stored exactly as before. use_event_time keeps records apart with msgpack and is unchanged (it uses chomp, not chomp!).

Tests:

  • test_write_with_add_newline_false (UDP and TCP, same in_syslog setup as test_write_udp / test_write_tcp): two records with single_value and add_newline false. On master in_syslog receives nothing and the log has the NoMethodError above; with the change it receives first and second as two messages.
  • test_format_with_add_newline_false_and_iso_2022_jp: ISO-2022-JP output ends with exactly one newline, whether or not the message ends with one.
  • test_format_with_add_newline_false_keeps_empty_message_empty: an empty message stays empty.
  • bundle exec rake test: 40 tests, 0 failures.

write reads the chunk back with io.each_line and calls chomp! on each
line. With add_newline false the records are stored without a newline,
so the chunk is a single line without one, chomp! returns nil and
transmit raises "undefined method 'split' for nil". Fluentd treats
NoMethodError as unrecoverable, so every chunk went to the backup
directory and no message was sent.

End each formatted record with a newline when the formatter did not
add one. use_event_time keeps the records apart with msgpack and is
not affected.

Signed-off-by: Lucas Barros <lucasbarros.kam@gmail.com>

@Watson1978 Watson1978 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for fixing this. I checked the change with single_value, json and ltsv, newline crlf, empty messages, invalid UTF-8 and use_event_time true. With add_newline false, the records of a chunk are no longer joined, and the chunk no longer fails. In these cases with add_newline true, the chunk bytes are the same as on master.

I found one input that works on master and fails with this change. Please see the inline comment.

Comment thread lib/fluent/plugin/out_remote_syslog.rb Resolved
end_with?("\n") raises Encoding::CompatibilityError for a string in an
encoding that is not ASCII compatible, such as ISO-2022-JP. The chunk is
written as bytes, so a binary string is fine here.

Signed-off-by: Lucas Barros <lucasbarros.kam@gmail.com>
@lbarrosby

Copy link
Copy Markdown
Contributor Author

Thanks for catching the ISO-2022-JP case. Pushed edc74ae:

  • format now checks the last byte and adds the newline as bytes, as in your suggestion.
  • Added test_format_with_add_newline_false_and_iso_2022_jp next to test_write_with_add_newline_false, with and without a trailing newline.

Both new cases raise Encoding::CompatibilityError on 12b1ce1 and pass now. bundle exec rake test: 39 tests, 0 failures, 0 errors (::1 is available here, so the ipv6 case passes too).

@Watson1978 Watson1978 added the bug label Nov 15, 2026

@Watson1978 Watson1978 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the update. edc74ae fixes the ISO-2022-JP case from my earlier comment, and the new test covers messages both with and without a trailing newline.

I missed one more regression in my first review, sorry about that. It was already in 12b1ce1. Please see the inline comment.

Comment thread lib/fluent/plugin/out_remote_syslog.rb Resolved
Signed-off-by: Lucas Barros <lucasbarros.kam@gmail.com>

@Watson1978 Watson1978 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the update. 8c3220a keeps an empty message empty, and the new test covers it.

I also ran this version with real Fluentd, single_value and add_newline false, with the messages "first", "", "third\n" and "fourth". I tried file and memory buffers with compress gzip, <buffer tag,time>, flush_mode immediate, and copy with out_file. In each case, the receiver got first, third and fourth, in this order, as with add_newline true on master. Thanks for fixing this!

@Watson1978 Watson1978 changed the title Fix every chunk failing when the formatter has add_newline false Fix sending records when the formatter has add_newline false Nov 18, 2026
@Watson1978
Watson1978 merged commit 3120f44 into fluent-plugins-nursery:master Nov 18, 2026
24 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants