Skip to content

Commit

Permalink
Merge pull request #2733 from ganmacs/fix-regex
Browse files Browse the repository at this point in the history
Fix regex and output warning log correctly
  • Loading branch information
repeatedly authored Dec 12, 2019
2 parents 3c6c64a + 9dc185c commit 3bba504
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/fluent/plugin/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Output < Base
helpers_internal :thread, :retry_state

CHUNK_KEY_PATTERN = /^[-_.@a-zA-Z0-9]+$/
CHUNK_KEY_PLACEHOLDER_PATTERN = /\$\{[-_.@$a-zA-Z0-9]+\}/
CHUNK_KEY_PLACEHOLDER_PATTERN = /\$\{([-_.@$a-zA-Z0-9]+)\}/
CHUNK_TAG_PLACEHOLDER_PATTERN = /\$\{(tag(?:\[-?\d+\])?)\}/
CHUNK_ID_PLACEHOLDER_PATTERN = /\$\{chunk_id\}/

Expand Down Expand Up @@ -707,7 +707,7 @@ def get_placeholders_tag(str)
end

def get_placeholders_keys(str)
str.scan(CHUNK_KEY_PLACEHOLDER_PATTERN).map{|ph| ph[2..-2]}.reject{|s| (s == "tag") || (s == 'chunk_id') }.sort
str.scan(CHUNK_KEY_PLACEHOLDER_PATTERN).map(&:first).reject{|s| (s == "tag") || (s == 'chunk_id') }.sort
end

# TODO: optimize this code
Expand Down Expand Up @@ -759,11 +759,19 @@ def extract_placeholders(str, chunk)
@chunk_keys.each do |key|
hash["${#{key}}"] = metadata.variables[key.to_sym]
end
rvalue = rvalue.gsub(CHUNK_KEY_PLACEHOLDER_PATTERN, hash)

rvalue = rvalue.gsub(CHUNK_KEY_PLACEHOLDER_PATTERN) do |matched|
hash.fetch(matched) do
log.warn "chunk key placeholder '#{matched[2..-2]}' not replaced. template:#{str}"
''
end
end
end

if rvalue =~ CHUNK_KEY_PLACEHOLDER_PATTERN
log.warn "chunk key placeholder '#{$&}' not replaced. template:#{str}"
log.warn "chunk key placeholder '#{$1}' not replaced. template:#{str}"
end

rvalue.sub(CHUNK_ID_PLACEHOLDER_PATTERN) {
if chunk_passed
dump_unique_id_hex(chunk.unique_id)
Expand Down
24 changes: 24 additions & 0 deletions test/plugin/test_output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,30 @@ def waiting(seconds)
assert { logs.any? { |log| log.include?("${chunk_id} is not allowed in this plugin") } }
end

test '#extract_placeholders logs warn message with not replaced key' do
@i.configure(config_element('ROOT', '', {}, [config_element('buffer', '')]))
tmpl = "/mypath/${key1}/test"
t = event_time('2016-04-11 20:30:00 +0900')
v = { key1: "value1" }
m = create_metadata(timekey: t, tag: 'fluentd.test.output', variables: v)
@i.extract_placeholders(tmpl, m)
logs = @i.log.out.logs

assert { logs.any? { |log| log.include?("chunk key placeholder 'key1' not replaced. template:#{tmpl}") } }
end

test '#extract_placeholders logs warn message with not replaced key if variables exist and chunk_key is not empty' do
@i.configure(config_element('ROOT', '', {}, [config_element('buffer', 'key1')]))
tmpl = "/mypath/${key1}/${key2}/test"
t = event_time('2016-04-11 20:30:00 +0900')
v = { key1: "value1" }
m = create_metadata(timekey: t, tag: 'fluentd.test.output', variables: v)
@i.extract_placeholders(tmpl, m)
logs = @i.log.out.logs

assert { logs.any? { |log| log.include?("chunk key placeholder 'key2' not replaced. template:#{tmpl}") } }
end

sub_test_case '#placeholder_validators' do
test 'returns validators for time, tag and keys when a template has placeholders even if plugin is not configured with these keys' do
@i.configure(config_element('ROOT', '', {}, [config_element('buffer', '')]))
Expand Down

0 comments on commit 3bba504

Please sign in to comment.