Class: Fluent::Plugin::JsonByName

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

Instance Method Summary collapse

Instance Method Details

#combine_path(path, element) ⇒ Object



18
19
20
21
# File 'lib/fluent/plugin/filter_json_by_name.rb', line 18

def combine_path(path, element)
  return element if path.nil?
  return "#{path}.#{element}"
end

#configure(conf) ⇒ Object



13
14
15
16
# File 'lib/fluent/plugin/filter_json_by_name.rb', line 13

def configure(conf)
  super
  validate_config(fields_to_index)
end

#filter(tag, time, record) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fluent/plugin/filter_json_by_name.rb', line 49

def filter(tag, time, record)
  return record if record["message"] == nil
  begin
    record_as_json = JSON.parse(record["message"])
  rescue JSON::ParserError
    return record
  end

  reshape(record, fields_to_index, record_as_json)

  return record
end

#reshape(record, fields_to_index_remaining, data, path = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fluent/plugin/filter_json_by_name.rb', line 35

def reshape(record, fields_to_index_remaining, data, path=nil)
  return if data == nil
  if fields_to_index_remaining.class == Hash and data.class == Hash then
    common_keys = Set.new(fields_to_index_remaining.keys) & Set.new(data.keys)
    common_keys.each do |key|
      reshape(record, fields_to_index_remaining[key], data[key], combine_path(path, key))
    end
  elsif fields_to_index_remaining.nil? then
    record[path] = data
  elsif fields_to_index_remaining.class == String then
    record[fields_to_index_remaining] = data
  end
end

#validate_config(fields_to_index_remaining, path = nil) ⇒ Object

Raises:

  • (Fluent::ConfigError)


23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fluent/plugin/filter_json_by_name.rb', line 23

def validate_config(fields_to_index_remaining, path=nil)
  if fields_to_index_remaining.class == Hash then
    fields_to_index_remaining.keys.each do |key|
      validate_config(fields_to_index_remaining[key], combine_path(path, key))
    end
    return
  elsif fields_to_index_remaining.nil? or fields_to_index_remaining.class == String then
    return
  end
  raise Fluent::ConfigError.new("value at \"#{path}\" in config is not either object, string, or null")
end