Class: Fluent::Plugin::GrepFilter

Inherits:
Filter 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

#has_filter_with_time

Attributes included from Fluent::PluginLoggerMixin

#log

Attributes inherited from Base

#under_plugin_development

Instance Method Summary collapse

Methods inherited from Filter

#filter_stream, #filter_with_time, #initialize

Methods included from Fluent::PluginHelper::Mixin

included

Methods included from Fluent::PluginLoggerMixin

included, #initialize, #start, #terminate

Methods included from Fluent::PluginId

#plugin_id, #plugin_id_configured?, #plugin_id_for_test?

Methods inherited from Base

#after_shutdown, #after_shutdown?, #after_start, #after_started?, #before_shutdown, #before_shutdown?, #close, #closed?, #configured?, #has_router?, #initialize, #inspect, #shutdown, #shutdown?, #start, #started?, #stop, #stopped?, #terminate, #terminated?

Methods included from SystemConfig::Mixin

#system_config, #system_config_override

Methods included from Configurable

#config, included, #initialize, lookup_type, register_type

Constructor Details

This class inherits a constructor from Fluent::Plugin::Filter

Instance Attribute Details

#excludesObject (readonly)

Returns the value of attribute excludes.



32
33
34
# File 'lib/fluent/plugin/filter_grep.rb', line 32

def excludes
  @excludes
end

#regexpsObject (readonly)

for test



31
32
33
# File 'lib/fluent/plugin/filter_grep.rb', line 31

def regexps
  @regexps
end

Instance Method Details

#configure(conf) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fluent/plugin/filter_grep.rb', line 34

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 Fluent::ConfigError, "regexp#{i} does not contain 2 parameters" unless regexp
    raise Fluent::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 Fluent::ConfigError, "exclude#{i} does not contain 2 parameters" unless exclude
    raise Fluent::ConfigError, "exclude#{i} contains a duplicated key, #{key}" if @excludes[key]
    @excludes[key] = Regexp.compile(exclude)
  end
end

#filter(tag, time, record) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fluent/plugin/filter_grep.rb', line 56

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: e
    log.warn_backtrace
  end
  result
end