Class: Fluent::JSONTransformFilter

Inherits:
Filter
  • Object
show all
Includes:
Configurable
Defined in:
lib/fluent/plugin/filter_json_transform_ex.rb

Constant Summary collapse

DEFAULTS =
[ 'nothing', 'flatten' ]
DEFAULT_CLASS_NAME =
'JSONTransformer'

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



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
41
42
43
44
# File 'lib/fluent/plugin/filter_json_transform_ex.rb', line 13

def configure(conf)
  @transform_script = conf['transform_script']

  @params = {}
  $log.info("Searching for 'params'...")
  conf.elements.each do |element|
    if element.name == 'params'
      element.to_hash.each do |key, value|
        @params[key] = value
      end
      $log.info("'params' section found: #{@params}") if @params.length > 0
    end
  end
  $log.info("'params' is not found or passed nothing") if @params.empty?

  if DEFAULTS.include?(@transform_script)
    @transform_script = "#{__dir__}/../../transform/#{@transform_script}.rb"
    className = DEFAULT_CLASS_NAME
  elsif @transform_script == 'custom'
    @transform_script = conf['script_path']
    className = conf['class_name'] || DEFAULT_CLASS_NAME
  end

  require @transform_script
  begin
    @transformer = Object.const_get(className).new
    $log.debug("#{className} class is loaded.")
  rescue NameError
    @transformer = Object.const_get(DEFAULT_CLASS_NAME).new
    $log.debug("#{DEFAULT_CLASS_NAME} class is loaded.")
  end
end

#filter(tag, time, record) ⇒ Object



46
47
48
# File 'lib/fluent/plugin/filter_json_transform_ex.rb', line 46

def filter(tag, time, record)
  return @transformer.transform(record, @params)
end