Class: Fluent::NorikraPlugin::RecordFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/norikra/record_filter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(include = '', include_regexp = '', exclude = '', exclude_regexp = '') ⇒ RecordFilter

Returns a new instance of RecordFilter.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fluent/plugin/norikra/record_filter.rb', line 5

def initialize(include='', include_regexp='', exclude='', exclude_regexp='')
  include ||= ''
  include_regexp ||= ''
  exclude ||= ''
  exclude_regexp ||= ''

  @default_policy = nil
  if include == '*' && exclude == '*'
    raise Fluent::ConfigError, "invalid configuration, both of 'include' and 'exclude' are '*'"
  end
  if include.empty? && include_regexp.empty? && exclude.empty? && exclude_regexp.empty? # assuming "include *"
    @default_policy = :include
  elsif exclude.empty? && exclude_regexp.empty? || exclude == '*' # assuming "exclude *"
    @default_policy = :exclude
  elsif include.empty? && include_regexp.empty? || include == '*' # assuming "include *"
    @default_policy = :include
  else
    raise Fluent::ConfigError, "unknown default policy. specify 'include *' or 'exclude *'"
  end

  @include_fields = nil
  @include_regexp = nil
  @exclude_fields = nil
  @exclude_regexp = nil

  if @default_policy == :exclude
    @include_fields = include.split(',')
    @include_regexp = Regexp.new(include_regexp) unless include_regexp.empty?
    if @include_fields.empty? && @include_regexp.nil?
      raise Fluent::ConfigError, "no one fields specified. specify 'include' or 'include_regexp'"
    end
  else
    @exclude_fields = exclude.split(',')
    @exclude_regexp = Regexp.new(exclude_regexp) unless exclude_regexp.empty?
  end
end

Instance Attribute Details

#default_policyObject (readonly)

Returns the value of attribute default_policy.



3
4
5
# File 'lib/fluent/plugin/norikra/record_filter.rb', line 3

def default_policy
  @default_policy
end

#exclude_fieldsObject (readonly)

Returns the value of attribute exclude_fields.



3
4
5
# File 'lib/fluent/plugin/norikra/record_filter.rb', line 3

def exclude_fields
  @exclude_fields
end

#exclude_regexpObject (readonly)

Returns the value of attribute exclude_regexp.



3
4
5
# File 'lib/fluent/plugin/norikra/record_filter.rb', line 3

def exclude_regexp
  @exclude_regexp
end

#include_fieldsObject (readonly)

Returns the value of attribute include_fields.



3
4
5
# File 'lib/fluent/plugin/norikra/record_filter.rb', line 3

def include_fields
  @include_fields
end

#include_regexpObject (readonly)

Returns the value of attribute include_regexp.



3
4
5
# File 'lib/fluent/plugin/norikra/record_filter.rb', line 3

def include_regexp
  @include_regexp
end

Instance Method Details

#filter(record) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fluent/plugin/norikra/record_filter.rb', line 42

def filter(record)
  if @default_policy == :include
    if @exclude_fields.empty? && @exclude_regexp.nil?
      record
    else
      record = record.dup
      record.keys.each do |f|
        record.delete(f) if @exclude_fields.include?(f) || @exclude_regexp &&  @exclude_regexp.match(f)
      end
      record
    end
  else # default policy exclude
    data = {}
    record.keys.each do |f|
      data[f] = record[f] if @include_fields.include?(f) || @include_regexp && @include_regexp.match(f)
    end
    data
  end
end