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

filter_grep: Improve the performance #1940

Merged
merged 2 commits into from
Apr 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 20 additions & 24 deletions lib/fluent/plugin/filter_grep.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class GrepFilter < Filter
def initialize
super

@_regexp_and_conditions = []
@_exclude_and_conditions = []
@_regexp_or_conditions = []
@_exclude_or_conditions = []
@_regexp_and_conditions = nil
@_exclude_and_conditions = nil
@_regexp_or_conditions = nil
@_exclude_or_conditions = nil
end

# for test
Expand Down Expand Up @@ -153,35 +153,31 @@ def configure(conf)
end
end

@_regexp_and_conditions = regexp_and_conditions.values
@_exclude_and_conditions = exclude_and_conditions.values
@_regexp_or_conditions = regexp_or_conditions.values
@_exclude_or_conditions = exclude_or_conditions.values
@_regexp_and_conditions = regexp_and_conditions.values unless regexp_and_conditions.empty?
@_exclude_and_conditions = exclude_and_conditions.values unless exclude_and_conditions.empty?
@_regexp_or_conditions = regexp_or_conditions.values unless regexp_or_conditions.empty?
@_exclude_or_conditions = exclude_or_conditions.values unless exclude_or_conditions.empty?
end

def filter(tag, time, record)
result = nil
begin
catch(:break_loop) do
@_regexp_and_conditions.each do |expression|
throw :break_loop unless expression.match?(record)
end
if !@_regexp_or_conditions.empty? && @_regexp_or_conditions.none? {|expression| expression.match?(record) }
throw :break_loop
end
if !@_exclude_and_conditions.empty? && @_exclude_and_conditions.all? {|expression| expression.match?(record) }
throw :break_loop
end
@_exclude_or_conditions.each do |expression|
throw :break_loop if expression.match?(record)
end
result = record
if @_regexp_and_conditions && @_regexp_and_conditions.any? { |expression| !expression.match?(record) }
return nil
end
if @_regexp_or_conditions && @_regexp_or_conditions.none? { |expression| expression.match?(record) }
return nil
end
if @_exclude_and_conditions && @_exclude_and_conditions.all? { |expression| expression.match?(record) }
return nil
end
if @_exclude_or_conditions && @_exclude_or_conditions.any? { |expression| expression.match?(record) }
return nil
end
rescue => e
log.warn "failed to grep events", error: e
log.warn_backtrace
end
result
record
end

Expression = Struct.new(:key, :pattern) do
Expand Down