Class: Fluent::Plugin::AmplifierFilter

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

Instance Method Summary collapse

Instance Method Details

#amp_with_floor(value) ⇒ Object



38
39
40
# File 'lib/fluent/plugin/filter_amplifier.rb', line 38

def amp_with_floor(value)
  (value.to_f * @ratio).floor
end

#amp_without_floor(value) ⇒ Object



34
35
36
# File 'lib/fluent/plugin/filter_amplifier.rb', line 34

def amp_without_floor(value)
  value.to_f * @ratio
end

#configure(conf) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fluent/plugin/filter_amplifier.rb', line 14

def configure(conf)
  super

  if @key_names.nil? && @key_pattern.nil?
    raise Fluent::ConfigError, "missing both of key_names and key_pattern"
  end
  if @key_names && @key_pattern
    raise Fluent::ConfigError, "cannot specify both of key_names and key_pattern"
  end
  if @key_pattern
    @key_pattern = Regexp.new(@key_pattern)
  end

  amp = @floor ? :amp_with_floor : :amp_without_floor
  self.define_singleton_method(:amp, method(amp))

  filter_method = @key_names ? :filter_with_names : :filter_with_patterns
  self.define_singleton_method(:filter, method(filter_method))
end

#filter(tag, time, record) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/fluent/plugin/filter_amplifier.rb', line 42

def filter(tag, time, record)
  if @key_names
    filter_with_names(tag, time, record)
  else
    filter_with_patterns(tag, time, record)
  end
end

#filter_with_names(tag, time, record) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fluent/plugin/filter_amplifier.rb', line 50

def filter_with_names(tag, time, record)
  updated = {}
  @key_names.each do |key|
    val = record[key]
    next unless val
    updated[key] = amp(val)
  end
  log.trace "amplifier", tag: tag, floor: @floor, ratio: @ratio, updated: updated, original: record
  if updated.size > 0
    record.merge(updated)
  else
    record
  end
end

#filter_with_patterns(tag, time, record) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fluent/plugin/filter_amplifier.rb', line 65

def filter_with_patterns(tag, time, record)
  updated = {}
  record.each_pair do |key, val|
    next unless val
    next unless @key_pattern.match(key)
    updated[key] = amp(val)
  end
  log.trace "amplifier", tag: tag, floor: @floor, ratio: @ratio, updated: updated, original: record
  if updated.size > 0
    record.merge(updated)
  else
    record
  end
end