class Fluent::GrepFilter

Constants

REGEXP_MAX_NUM

Attributes

_excludes[R]
_regexps[R]

for test

Public Class Methods

new() click to toggle source
Calls superclass method Fluent::Filter.new
# File lib/fluent/plugin/filter_grep.rb, line 24
def initialize
  super
  require 'fluent/plugin/string_util'
end

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method Fluent::Filter#configure
# File lib/fluent/plugin/filter_grep.rb, line 56
def configure(conf)
  super

  @_regexps = {}
  (1..REGEXP_MAX_NUM).each do |i|
    next unless conf["regexp#{i}"]
    key, regexp = conf["regexp#{i}"].split(/ /, 2)
    raise ConfigError, "regexp#{i} does not contain 2 parameters" unless regexp
    raise ConfigError, "regexp#{i} contains a duplicated key, #{key}" if @_regexps[key]
    @_regexps[key] = Regexp.compile(regexp)
  end

  @_excludes = {}
  (1..REGEXP_MAX_NUM).each do |i|
    next unless conf["exclude#{i}"]
    key, exclude = conf["exclude#{i}"].split(/ /, 2)
    raise ConfigError, "exclude#{i} does not contain 2 parameters" unless exclude
    raise ConfigError, "exclude#{i} contains a duplicated key, #{key}" if @_excludes[key]
    @_excludes[key] = Regexp.compile(exclude)
  end

  @regexps.each do |e|
    raise Fluent::ConfigError, "Duplicate key: #{e.key}" if @_regexps.key?(e.key)
    @_regexps[e.key] = e.pattern
  end
  @excludes.each do |e|
    raise Fluent::ConfigError, "Duplicate key: #{e.key}" if @_excludes.key?(e.key)
    @_excludes[e.key] = e.pattern
  end
end
filter(tag, time, record) click to toggle source
# File lib/fluent/plugin/filter_grep.rb, line 87
def filter(tag, time, record)
  result = nil
  begin
    catch(:break_loop) do
      @_regexps.each do |key, regexp|
        throw :break_loop unless ::Fluent::StringUtil.match_regexp(regexp, record[key].to_s)
      end
      @_excludes.each do |key, exclude|
        throw :break_loop if ::Fluent::StringUtil.match_regexp(exclude, record[key].to_s)
      end
      result = record
    end
  rescue => e
    log.warn "failed to grep events", error_class: e.class, error: e.message
    log.warn_backtrace
  end
  result
end