Skip to content
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

output: Fix "placeholder chunk_id not replaced" warnings #3339

Merged
merged 1 commit into from
Apr 20, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
12 changes: 12 additions & 0 deletions test/plugin/test_output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,18 @@ def waiting(seconds)
assert { logs.none? { |log| log.include?("${chunk_id}") } }
end

test '#extract_placeholders does not log for ${chunk_id} placeholder (with @chunk_keys)' do
@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)
@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"
Expand Down