Class: Fluent::AmplifierFilterOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_amplifier_filter.rb

Instance Method Summary collapse

Instance Method Details

#amp_with_floor(value) ⇒ Object



60
61
62
# File 'lib/fluent/plugin/out_amplifier_filter.rb', line 60

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

#amp_without_floor(value) ⇒ Object



56
57
58
# File 'lib/fluent/plugin/out_amplifier_filter.rb', line 56

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

#configure(conf) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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/out_amplifier_filter.rb', line 19

def configure(conf)
  super

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

  amp = if @floor
          method(:amp_with_floor)
        else
          method(:amp_without_floor)
        end
  (class << self; self; end).module_eval do
    define_method(:amp, amp)
  end

  if not @remove_prefix and not @add_prefix
    raise Fluent::ConfigError, "missing both of remove_prefix and add_prefix"
  end
  if @remove_prefix
    @removed_prefix_string = @remove_prefix + '.'
    @removed_length = @removed_prefix_string.length
  end
  if @add_prefix
    @added_prefix_string = @add_prefix + '.'
  end
end

#emit(tag, es, chain) ⇒ Object



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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/fluent/plugin/out_amplifier_filter.rb', line 64

def emit(tag, es, chain)
  if @remove_prefix and
      ( (tag.start_with?(@removed_prefix_string) and tag.length > @removed_length) or tag == @remove_prefix)
    tag = tag[@removed_length..-1]
  end
  if @add_prefix
    tag = if tag and tag.length > 0
            @added_prefix_string + tag
          else
            @add_prefix
          end
  end

  pairs = []
  if @key_names
    es.each {|time,record|
      updated = {}
      @key_names.each {|key|
        val = record[key]
        next unless val
        updated[key] = amp(val)
      }
      log.debug "amplifier tag:#{tag} floor:#{@floor} ratio:#{@ratio} updated:#{updated.to_json} record:#{record.to_json}"
      if updated.size > 0
        pairs.push [time, record.merge(updated)]
      else
        pairs.push [time, record.dup]
      end
    }
  else @key_pattern
    es.each {|time,record|
      updated = {}
      record.keys.each {|key|
        val = record[key]
        next unless val
        next unless @key_pattern.match(key)
        updated[key] = amp(val)
      }
      log.debug "amplifier tag:#{tag} floor:#{@floor} ratio:#{@ratio} updated:#{updated.to_json} record:#{record.to_json}"
      if updated.size > 0
        pairs.push [time, record.merge(updated)]
      else
        pairs.push [time, record.dup]
      end
    }
  end
  router.emit_array(tag, pairs)

  chain.next
end