From f18640bf55e839b2292201deeb1532a462b4bf95 Mon Sep 17 00:00:00 2001 From: Brian Atkinson Date: Fri, 25 Sep 2020 09:27:58 -0700 Subject: [PATCH] output: Replace ${chunk_id} before logging warning. The updated implementation makes sure to replace instances of ${chunk_id} prior to looking for and warning on unreplaced keys in the pattern. Prior to this, a warning line was printed for every chunk that was flushed even though nothing was actually wrong (replacement happened right after). Signed-off-by: Brian Atkinson --- lib/fluent/plugin/output.rb | 12 +++++++----- test/plugin/test_output.rb | 12 ++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/fluent/plugin/output.rb b/lib/fluent/plugin/output.rb index 8e9950fd5b..1f357828a0 100644 --- a/lib/fluent/plugin/output.rb +++ b/lib/fluent/plugin/output.rb @@ -769,17 +769,19 @@ def extract_placeholders(str, chunk) end end - if rvalue =~ CHUNK_KEY_PLACEHOLDER_PATTERN - log.warn "chunk key placeholder '#{$1}' not replaced. template:#{str}" - end - - rvalue.sub(CHUNK_ID_PLACEHOLDER_PATTERN) { + rvalue = rvalue.sub(CHUNK_ID_PLACEHOLDER_PATTERN) { if chunk_passed dump_unique_id_hex(chunk.unique_id) else log.warn "${chunk_id} is not allowed in this plugin. Pass Chunk instead of metadata in extract_placeholders's 2nd argument" end } + + if rvalue =~ CHUNK_KEY_PLACEHOLDER_PATTERN + log.warn "chunk key placeholder '#{$1}' not replaced. template:#{str}" + end + + rvalue end end diff --git a/test/plugin/test_output.rb b/test/plugin/test_output.rb index db8295a87c..f6878de803 100644 --- a/test/plugin/test_output.rb +++ b/test/plugin/test_output.rb @@ -378,6 +378,18 @@ def waiting(seconds) assert { logs.any? { |log| log.include?("${chunk_id} is not allowed in this plugin") } } end + test '#extract_placeholders does not log for ${chunk_id} placeholder' do + @i.configure(config_element('ROOT', '', {}, [config_element('buffer', '')])) + tmpl = "/mypath/${chunk_id}/tail" + t = event_time('2016-04-11 20:30:00 +0900') + v = {key1: "value1", key2: "value2"} + c = create_chunk(timekey: t, tag: 'fluentd.test.output', variables: v) + @i.log.out.logs.clear + @i.extract_placeholders(tmpl, c) + logs = @i.log.out.logs + assert { logs.none? { |log| log.include?("${chunk_id}") } } + end + test '#extract_placeholders logs warn message with not replaced key' do @i.configure(config_element('ROOT', '', {}, [config_element('buffer', '')])) tmpl = "/mypath/${key1}/test"