Class: Fluent::GrepFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/fluent/plugin/filter_grep.rb

Constant Summary collapse

REGEXP_MAX_NUM =
20

Constants included from Configurable

Configurable::CONFIG_TYPE_REGISTRY

Instance Attribute Summary collapse

Attributes inherited from Filter

#router

Attributes included from PluginLoggerMixin

#log

Instance Method Summary collapse

Methods inherited from Filter

#filter_stream, #shutdown, #start

Methods included from PluginLoggerMixin

included

Methods included from PluginId

#plugin_id

Methods included from Configurable

#config, included, lookup_type, register_type

Constructor Details

#initializeGrepFilter

Returns a new instance of GrepFilter.



24
25
26
27
# File 'lib/fluent/plugin/filter_grep.rb', line 24

def initialize
  super
  require 'fluent/plugin/string_util'
end

Instance Attribute Details

#excludesObject (readonly)

Returns the value of attribute excludes.



36
37
38
# File 'lib/fluent/plugin/filter_grep.rb', line 36

def excludes
  @excludes
end

#regexpsObject (readonly)

for test



35
36
37
# File 'lib/fluent/plugin/filter_grep.rb', line 35

def regexps
  @regexps
end

Instance Method Details

#configure(conf) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fluent/plugin/filter_grep.rb', line 38

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
end

#filter(tag, time, record) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fluent/plugin/filter_grep.rb', line 60

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