Class: Fluent::TextParser::JSONTransformParser

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

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#call(text) ⇒ Object



43
44
45
46
# File 'lib/fluent/plugin/parser_json_transform_ex.rb', line 43

def call(text)
  raw_json = JSON.parse(text)
  return nil, @transformer.transform(raw_json, @params)
end

#configure(conf) ⇒ Object



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
41
# File 'lib/fluent/plugin/parser_json_transform_ex.rb', line 12

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
  rescue NameError
    @transformer = Object.const_get(DEFAULT_CLASS_NAME).new
  end
end

#parse(text) ⇒ Object



48
49
50
51
# File 'lib/fluent/plugin/parser_json_transform_ex.rb', line 48

def parse(text)
  raw_json = JSON.parse(text)
  return nil, @transformer.transform(raw_json, @params)
end