Skip to content

Commit

Permalink
use constant regexp for tag placeholder, and shrink the code
Browse files Browse the repository at this point in the history
  • Loading branch information
tagomoris committed Oct 5, 2016
1 parent de693b5 commit c3f4260
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/fluent/plugin/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Output < Base

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

CHUNKING_FIELD_WARN_NUM = 4

Expand Down Expand Up @@ -606,7 +607,7 @@ def get_placeholders_time(str)
def get_placeholders_tag(str)
# [["tag"],["tag[0]"]]
parts = []
str.scan(/\$\{(tag(?:\[\d+\])?)\}/).map(&:first).each do |ph|
str.scan(CHUNK_TAG_PLACEHOLDER_PATTERN).map(&:first).each do |ph|
if ph == "tag"
parts << -1
elsif ph =~ /^tag\[(\d+)\]$/
Expand All @@ -633,16 +634,17 @@ def extract_placeholders(str, metadata)
end
# ${tag}, ${tag[0]}, ${tag[1]}, ...
if @chunk_key_tag
if str =~ /\$\{tag\[\d+\]\}/
hash = {'${tag}' => metadata.tag}
if str.include?('${tag}')
rvalue = rvalue.gsub('${tag}', metadata.tag)
end
if str =~ CHUNK_TAG_PLACEHOLDER_PATTERN
hash = {}
metadata.tag.split('.').each_with_index do |part, i|
hash["${tag[#{i}]}"] = part
end
rvalue = rvalue.gsub(/\$\{tag(\[\d+\])?\}/, hash)
elsif str.include?('${tag}')
rvalue = rvalue.gsub('${tag}', metadata.tag)
rvalue = rvalue.gsub(CHUNK_TAG_PLACEHOLDER_PATTERN, hash)
end
if rvalue =~ /\$\{tag(?:\[\d+\])?\}/
if rvalue =~ CHUNK_TAG_PLACEHOLDER_PATTERN
log.warn "tag placeholder '#{$1}' not replaced. tag:#{metadata.tag}, template:#{str}"
end
end
Expand Down

0 comments on commit c3f4260

Please sign in to comment.