Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from cosmo0920/add-filter-plugin
Add filter plugin
- Loading branch information
Showing
7 changed files
with
392 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Fluent | ||
class RewriteFilter < Filter | ||
Fluent::Plugin.register_filter('rewrite', self) | ||
|
||
attr_reader :rewrite_rule | ||
|
||
def configure(conf) | ||
require 'fluent/plugin/rewrite_rule' | ||
|
||
super | ||
|
||
@rewrite_rule = RewriteRule.new(self, conf) | ||
end | ||
|
||
def filter_stream(tag, es) | ||
new_es = MultiEventStream.new | ||
|
||
es.each do |time, record| | ||
record = @rewrite_rule.rewrite(record) | ||
new_es.add(time, record) if record | ||
end | ||
|
||
new_es | ||
end | ||
end if defined?(Filter) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
module Fluent | ||
class RewriteRule | ||
attr_reader :rules | ||
|
||
def initialize(plugin, conf) | ||
@plugin = plugin | ||
@rules = conf.elements.select {|element| element.name == 'rule' }.map do |element| | ||
rule = {} | ||
element.keys.each do |key| | ||
# read and throw away to supress unread configuration warning | ||
rule[key] = element[key] | ||
end | ||
rule["regex"] = Regexp.new(element["pattern"]) if element.has_key?("pattern") | ||
rule | ||
end | ||
end | ||
|
||
def rewrite(tag=nil, record) | ||
@rules.each do |rule| | ||
tag, record, last = apply_rule(rule, tag, record) | ||
|
||
break if last | ||
if @plugin.is_a?(Fluent::Output) | ||
return if !tag && !record | ||
else | ||
return if !record | ||
end | ||
end | ||
|
||
return [record] if not @plugin.is_a?(Fluent::Output) | ||
return [tag, record] if @plugin.is_a?(Fluent::Output) | ||
end | ||
|
||
def apply_rule(rule, tag=nil, record) | ||
tag_prefix = tag && tag.length > 0 ? "." : "" | ||
key = rule["key"] | ||
pattern = rule["pattern"] | ||
last = nil | ||
|
||
return [tag, record] if !key || !record.has_key?(key) | ||
return [tag, record] unless pattern | ||
|
||
if matched = record[key].match(rule["regex"]) | ||
return if rule["ignore"] | ||
|
||
if rule["replace"] | ||
replace = rule["replace"] | ||
record[key] = record[key].gsub(rule["regex"], replace) | ||
end | ||
|
||
if rule["append_to_tag"] && @plugin.is_a?(Fluent::Output) | ||
if rule["tag"] | ||
tag += (tag_prefix + rule["tag"]) | ||
else | ||
matched.captures.each do |m| | ||
tag += (tag_prefix + "#{m}") | ||
end | ||
end | ||
end | ||
|
||
if rule["last"] | ||
last = true | ||
end | ||
else | ||
if rule["append_to_tag"] && rule["fallback"] && @plugin.is_a?(Fluent::Output) | ||
tag += (tag_prefix + rule["fallback"]) | ||
end | ||
end | ||
|
||
return [tag, record, last] | ||
end | ||
end | ||
end |
Oops, something went wrong.