Class: Fluent::Plugin::GrepFilter

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

Defined Under Namespace

Classes: Expression

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

#emit_records, #emit_size, #filter_stream, #filter_with_time, #measure_metrics, #statistics

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, #stop

Methods inherited from Base

#acquire_worker_lock, #after_shutdown, #after_shutdown?, #after_start, #after_started?, #before_shutdown, #before_shutdown?, #called_in_test?, #close, #closed?, #configured?, #context_router, #context_router=, #fluentd_worker_id, #get_lock_path, #has_router?, #inspect, #multi_workers_ready?, #plugin_root_dir, #reloadable_plugin?, #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
31
32
# File 'lib/fluent/plugin/filter_grep.rb', line 25

def initialize
  super

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

Instance Attribute Details

#_exclude_and_conditionsObject (readonly)

for test



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

def _exclude_and_conditions
  @_exclude_and_conditions
end

#_exclude_or_conditionsObject (readonly)

for test



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

def _exclude_or_conditions
  @_exclude_or_conditions
end

#_regexp_and_conditionsObject (readonly)

for test



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

def _regexp_and_conditions
  @_regexp_and_conditions
end

#_regexp_or_conditionsObject (readonly)

for test



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

def _regexp_or_conditions
  @_regexp_or_conditions
end

Instance Method Details

#configure(conf) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/fluent/plugin/filter_grep.rb', line 88

def configure(conf)
  super

  regexp_and_conditions = {}
  regexp_or_conditions = {}
  exclude_and_conditions = {}
  exclude_or_conditions = {}

  (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 regexp_and_conditions[key]
    regexp_and_conditions[key] = Expression.new(record_accessor_create(key), Regexp.compile(regexp))
  end

  (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 exclude_or_conditions[key]
    exclude_or_conditions[key] = Expression.new(record_accessor_create(key), Regexp.compile(exclude))
  end

  if @regexps.size > 1
    log.info "Top level multiple <regexp> is interpreted as 'and' condition"
  end
  @regexps.each do |e|
    raise Fluent::ConfigError, "Duplicate key: #{e.key}" if regexp_and_conditions.key?(e.key)
    regexp_and_conditions[e.key] = Expression.new(record_accessor_create(e.key), e.pattern)
  end

  if @excludes.size > 1
    log.info "Top level multiple <exclude> is interpreted as 'or' condition"
  end
  @excludes.each do |e|
    raise Fluent::ConfigError, "Duplicate key: #{e.key}" if exclude_or_conditions.key?(e.key)
    exclude_or_conditions[e.key] = Expression.new(record_accessor_create(e.key), e.pattern)
  end

  @and_conditions.each do |and_condition|
    if !and_condition.regexps.empty? && !and_condition.excludes.empty?
      raise Fluent::ConfigError, "Do not specify both <regexp> and <exclude> in <and>"
    end
    and_condition.regexps.each do |e|
      raise Fluent::ConfigError, "Duplicate key in <and>: #{e.key}" if regexp_and_conditions.key?(e.key)
      regexp_and_conditions[e.key] = Expression.new(record_accessor_create(e.key), e.pattern)
    end
    and_condition.excludes.each do |e|
      raise Fluent::ConfigError, "Duplicate key in <and>: #{e.key}" if exclude_and_conditions.key?(e.key)
      exclude_and_conditions[e.key] = Expression.new(record_accessor_create(e.key), e.pattern)
    end
  end

  @or_conditions.each do |or_condition|
    if !or_condition.regexps.empty? && !or_condition.excludes.empty?
      raise Fluent::ConfigError, "Do not specify both <regexp> and <exclude> in <or>"
    end
    or_condition.regexps.each do |e|
      raise Fluent::ConfigError, "Duplicate key in <or>: #{e.key}" if regexp_or_conditions.key?(e.key)
      regexp_or_conditions[e.key] = Expression.new(record_accessor_create(e.key), e.pattern)
    end
    or_condition.excludes.each do |e|
      raise Fluent::ConfigError, "Duplicate key in <or>: #{e.key}" if exclude_or_conditions.key?(e.key)
      exclude_or_conditions[e.key] = Expression.new(record_accessor_create(e.key), e.pattern)
    end
  end

  @_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

#filter(tag, time, record) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/fluent/plugin/filter_grep.rb', line 162

def filter(tag, time, record)
  begin
    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
  record
end