Skip to content

Commit

Permalink
output: Fix "placeholder chunk_id not replaced" warnings
Browse files Browse the repository at this point in the history
On some setup, Fluentd warns about "chunk_id" being not found in
`@chunk_keys`.

This warning is misguided. `${chunk_id}` is a special field that
Fluentd should automatically fill it with `chunk.unique_id`. It
should never try to look up the key in `@chunk_keys`.

This patch fixes the warning by adjusting the evaluation order,
extending the preceding work f18640b by Brian Atkinson.

Signed-off-by: Fujimoto Seiji <[email protected]>
  • Loading branch information
fujimotos committed Apr 20, 2021
1 parent 8c7d674 commit 3e6180c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
20 changes: 11 additions & 9 deletions lib/fluent/plugin/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,17 @@ def extract_placeholders(str, chunk)
log.warn "tag placeholder '#{$1}' not replaced. tag:#{metadata.tag}, template:#{str}"
end
end
# ${a_chunk_key}, ...

# First we replace ${chunk_id} with chunk.unique_id (hexlified).
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
}

# Then, replace other ${chunk_key}s.
if !@chunk_keys.empty? && metadata.variables
hash = {'${tag}' => '${tag}'} # not to erase this wrongly
@chunk_keys.each do |key|
Expand All @@ -769,14 +779,6 @@ def extract_placeholders(str, chunk)
end
end

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
Expand Down
4 changes: 2 additions & 2 deletions test/plugin/test_output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,8 @@ def waiting(seconds)
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"
@i.configure(config_element('ROOT', '', {}, [config_element('buffer', 'key1')]))
tmpl = "/mypath/${chunk_id}/${key1}/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)
Expand Down

0 comments on commit 3e6180c

Please sign in to comment.