Class: Fluent::FlattenOutput

Inherits:
Output
  • Object
show all
Includes:
HandleTagNameMixin
Defined in:
lib/fluent/plugin/out_flatten.rb

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fluent/plugin/out_flatten.rb', line 26

def configure(conf)
  super

  if (
      !remove_tag_prefix &&
      !remove_tag_suffix &&
      !add_tag_prefix    &&
      !add_tag_suffix
  )
    raise ConfigError, "out_flatten: At least one of remove_tag_prefix/remove_tag_suffix/add_tag_prefix/add_tag_suffix is required to be set"
  end
end

#emit(tag, es, chain) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fluent/plugin/out_flatten.rb', line 39

def emit(tag, es, chain)
  es.each do |time, record|
    flattened = flatten(record)

    flattened.each do |keypath, value|
      tag_with_keypath = [tag.clone, keypath].join('.')
      filter_record(tag_with_keypath, time, value)

      router.emit(tag_with_keypath, time, value)
    end
  end

  chain.next
end

#flatten(record) ⇒ Object



54
55
56
57
58
59
60
61
62
63
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
# File 'lib/fluent/plugin/out_flatten.rb', line 54

def flatten(record)
  flattened = {}

  if record.has_key?(key) && !record[key].empty?
    hash = nil

    begin
      if parse_json
        # XXX work-around
        # fluentd seems to escape json value excessively
        json = record[key].gsub(/\\"/, '"')
        hash = JSON.parse(json)
      else
        hash = record[key]
      end
    rescue JSON::ParserError
      return flattened
    end

    processor = lambda do |root, hash|
      flattened = {}
      return flattened unless hash.is_a?(Hash)

      hash.each do |path, value|
        keypath = [root, path].join('.')

        if value.is_a?(Hash)
          flattened = flattened.merge(processor.call(keypath, value))
        else
          flattened[keypath] = { inner_key => value }
        end
      end

      flattened
    end

    flattened  = processor.call(key, hash)
  end

  flattened
end