From c288ad878ff1681d9281462b5ffe1076832e430e Mon Sep 17 00:00:00 2001 From: Lucas Barros Date: Sat, 21 Nov 2026 11:45:12 -0300 Subject: [PATCH 1/2] Raise an error when the host is empty after placeholder expansion Fixes #96 Signed-off-by: Lucas Barros --- lib/fluent/plugin/out_remote_syslog.rb | 7 +++++++ test/plugin/out_remote_syslog.rb | 28 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/lib/fluent/plugin/out_remote_syslog.rb b/lib/fluent/plugin/out_remote_syslog.rb index 77b9b90..b8d8b05 100644 --- a/lib/fluent/plugin/out_remote_syslog.rb +++ b/lib/fluent/plugin/out_remote_syslog.rb @@ -110,6 +110,13 @@ def write(chunk) port = @port end + # A placeholder can expand to an empty string when the chunk has no + # value for its key. Raise, so that the chunk is retried or goes to + # , instead of sending it to the local host. + if host.nil? || host.empty? + raise "host is empty after placeholders are replaced: host=#{@host.inspect} host_with_port=#{@host_with_port.inspect}" + end + host_with_port = "#{host}:#{port}" Thread.current[host_with_port] ||= create_sender(host, port) diff --git a/test/plugin/out_remote_syslog.rb b/test/plugin/out_remote_syslog.rb index c03ebfe..d9e378c 100644 --- a/test/plugin/out_remote_syslog.rb +++ b/test/plugin/out_remote_syslog.rb @@ -445,4 +445,32 @@ def test_severity_mapper(data) out = Fluent::Plugin::RemoteSyslogOutput::SeverityMapper.map(data[:in]) assert_equal(data[:out], out) end + + data("host udp", {target: %[host "${dest}"\nport 5568], protocol: "udp"}) + data("host tcp", {target: %[host "${dest}"\nport 5568], protocol: "tcp"}) + data("host_with_port udp", {target: %[host_with_port "${dest}:5568"], protocol: "udp"}) + data("host_with_port tcp", {target: %[host_with_port "${dest}:5568"], protocol: "tcp"}) + def test_write_raises_on_empty_host(data) + d = create_driver_out_syslog %[ + @type remote_syslog + #{data[:target]} + protocol #{data[:protocol]} + + + + + + @type single_value + message_key message + + ] + + time = Fluent::EventTime.now + record = {"message" => "hello"} + chunk = Fluent::Plugin::Buffer::MemoryChunk.new(d.instance.metadata("tag", time, record)) + chunk.append([d.instance.format("tag", time, record)]) + + error = assert_raise(RuntimeError) { d.instance.write(chunk) } + assert_match(/host is empty/, error.message) + end end From 036283984dc64fb61560c932e7bd7712ae3b4d23 Mon Sep 17 00:00:00 2001 From: Lucas Barros Date: Tue, 24 Nov 2026 19:41:16 -0300 Subject: [PATCH 2/2] Raise an unrecoverable error only when the expansion empties the host A plain raise made Fluentd retry a chunk that can never succeed, and the chunks for valid destinations waited behind it. Raise Fluent::UnrecoverableError, so the chunk goes to or the backup at once. Keep sending to the local host when the host is empty in the configuration, as on master (host "" with a port, host_with_port ":514"). Only an empty host after the placeholders are replaced is an error. Signed-off-by: Lucas Barros --- lib/fluent/plugin/out_remote_syslog.rb | 11 +++++--- test/plugin/out_remote_syslog.rb | 37 +++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/lib/fluent/plugin/out_remote_syslog.rb b/lib/fluent/plugin/out_remote_syslog.rb index b8d8b05..0472bd3 100644 --- a/lib/fluent/plugin/out_remote_syslog.rb +++ b/lib/fluent/plugin/out_remote_syslog.rb @@ -52,6 +52,9 @@ def configure(conf) if @host.nil? && @host_with_port.nil? raise ConfigError, "host or host_with_port is required" end + # The host part of the configured value. An empty host here sends to + # the local host, so only an empty host after the expansion is an error. + @configured_host = (@host_with_port ? split_host_with_port(@host_with_port).first : @host).to_s @formatter = formatter_create unless @formatter.formatter_type == :text_per_line @@ -111,10 +114,10 @@ def write(chunk) end # A placeholder can expand to an empty string when the chunk has no - # value for its key. Raise, so that the chunk is retried or goes to - # , instead of sending it to the local host. - if host.nil? || host.empty? - raise "host is empty after placeholders are replaced: host=#{@host.inspect} host_with_port=#{@host_with_port.inspect}" + # value for its key. Retrying cannot fix such a chunk, so raise an + # unrecoverable error to move it to or the backup at once. + if host.to_s.empty? && !@configured_host.empty? + raise Fluent::UnrecoverableError, "host is empty after placeholders are replaced: host=#{@host.inspect} host_with_port=#{@host_with_port.inspect}" end host_with_port = "#{host}:#{port}" diff --git a/test/plugin/out_remote_syslog.rb b/test/plugin/out_remote_syslog.rb index d9e378c..71e639f 100644 --- a/test/plugin/out_remote_syslog.rb +++ b/test/plugin/out_remote_syslog.rb @@ -450,6 +450,7 @@ def test_severity_mapper(data) data("host tcp", {target: %[host "${dest}"\nport 5568], protocol: "tcp"}) data("host_with_port udp", {target: %[host_with_port "${dest}:5568"], protocol: "udp"}) data("host_with_port tcp", {target: %[host_with_port "${dest}:5568"], protocol: "tcp"}) + data("host_with_port without port", {target: %[host_with_port "${dest}"], protocol: "udp"}) def test_write_raises_on_empty_host(data) d = create_driver_out_syslog %[ @type remote_syslog @@ -470,7 +471,41 @@ def test_write_raises_on_empty_host(data) chunk = Fluent::Plugin::Buffer::MemoryChunk.new(d.instance.metadata("tag", time, record)) chunk.append([d.instance.format("tag", time, record)]) - error = assert_raise(RuntimeError) { d.instance.write(chunk) } + error = assert_raise(Fluent::UnrecoverableError) { d.instance.write(chunk) } assert_match(/host is empty/, error.message) end + + data("host with a port", {target: %[host ""\nport 5571], port: 5571}) + data("host_with_port", {target: %[host_with_port ":5572"], port: 5572}) + def test_write_with_empty_configured_host_sends_to_local_host(data) + d = create_driver_out_syslog %[ + @type remote_syslog + #{data[:target]} + program minitest + protocol tcp + + + @type single_value + message_key message + + ] + + server = TCPServer.new("127.0.0.1", data[:port]) + received = Thread.new do + client = server.accept + line = client.gets + client.close + line + end + begin + d.run do + d.feed("tag", Fluent::EventTime.now, {"message" => "hello"}) + end + assert_not_nil(received.join(5), "no message received") + assert_match(/minitest: hello/, received.value) + ensure + received.kill + server.close + end + end end