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

Methods included from Fluent::PluginHelper::Mixin

included

Methods included from Fluent::PluginLoggerMixin

included, #terminate

Methods included from Fluent::PluginId

#plugin_id, #plugin_id_configured?, #plugin_id_for_test?, #plugin_root_dir

Methods inherited from Base

#after_shutdown, #after_shutdown?, #after_start, #after_started?, #before_shutdown, #before_shutdown?, #close, #closed?, #configured?, #context_router, #context_router=, #fluentd_worker_id, #has_router?, #inspect, #multi_workers_ready?, #plugin_root_dir, #shutdown, #shutdown?, #start, #started?, #stop, #stopped?, #string_safe_encoding, #terminate, #terminated?

Methods included from SystemConfig::Mixin

#system_config, #system_config_override

Methods included from Configurable

#config, #configure_proxy_generate, #configured_section_create, included, lookup_type, register_type

Constructor Details

#initializeGrepFilter

Returns a new instance of GrepFilter.



25
26
27
28
29
30
# File 'lib/fluent/plugin/filter_grep.rb', line 25

def initialize
  super

  @_regexps = {}
  @_excludes = {}
end

Instance Attribute Details

#_excludesObject (readonly)

Returns the value of attribute _excludes.



59
60
61
# File 'lib/fluent/plugin/filter_grep.rb', line 59

def _excludes
  @_excludes
end

#_regexpsObject (readonly)

for test



58
59
60
# File 'lib/fluent/plugin/filter_grep.rb', line 58

def _regexps
  @_regexps
end

Instance Method Details

#configure(conf) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/fluent/plugin/filter_grep.rb', line 61

def configure(conf)
  super

  rs = {}
  (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 rs[key]
    rs[key] = Regexp.compile(regexp)
  end

  es = {}
  (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 es[key]
    es[key] = Regexp.compile(exclude)
  end

  @regexps.each do |e|
    raise Fluent::ConfigError, "Duplicate key: #{e.key}" if rs.key?(e.key)
    rs[e.key] = e.pattern
  end
  @excludes.each do |e|
    raise Fluent::ConfigError, "Duplicate key: #{e.key}" if es.key?(e.key)
    es[e.key] = e.pattern
  end

  rs.each_pair do |k, v|
    @_regexps[record_accessor_create(k)] = v
  end
  es.each_pair do |k, v|
    @_excludes[record_accessor_create(k)] = v
  end
end

#filter(tag, time, record) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/fluent/plugin/filter_grep.rb', line 99

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, key.call(record).to_s)
      end
      @_excludes.each do |key, exclude|
        throw :break_loop if ::Fluent::StringUtil.match_regexp(exclude, key.call(record).to_s)
      end
      result = record
    end
  rescue => e
    log.warn "failed to grep events", error: e
    log.warn_backtrace
  end
  result
end