fluent-plugin-json_expander 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d583201256a49ab2c2ca48747f882c55aa3397fe
4
- data.tar.gz: b91660f95dedaf77e44ba9b2490c0d245bc828f6
3
+ metadata.gz: 7a35f9e721811377cd62973f2f5cf3577d564b64
4
+ data.tar.gz: 92cf9d852f7c38c992bb472eb1d9b56510928af1
5
5
  SHA512:
6
- metadata.gz: 69e2106ffe9668eadcc54f011cbfec61d250c6d4d1bccc280f9743ccf4b3e7eb20918616a87f24b35e806938bfc09b08e834c8208ea4bd2d25e2ecf4f3013546
7
- data.tar.gz: 4137b0ec45ec23e2a9c39ca6e81c6808c1a91065091621c13de31145009539e5e9ee5ffd6bab85763449301f01a2cf02c6334404b2fd26b59b395e50827c22ff
6
+ metadata.gz: eaa5671117602bf5d421e1ba48ed0e1c1e3acebce2cd451658ea2e53d710be2329275cc0c58672e299a13ae8bf1025b94d296f44dd5914d3eeafb2eccccf7dd4
7
+ data.tar.gz: 3948c8542fcc827267a476023c7dde83e5e956fb435f500eaa2c1eee4ba3c83a6cd8477dd03798fbdda7c770f72ee7ab856e950aafaab84bb78b684f6a910329
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "fluent-plugin-json_expander"
7
- spec.version = "0.0.1"
7
+ spec.version = "0.0.2"
8
8
  spec.authors = ["Uchio KONDO"]
9
9
  spec.email = ["udzura@udzura.jp"]
10
10
 
@@ -28,6 +28,8 @@ class Fluent::JsonExpanderOutput < Fluent::MultiOutput
28
28
 
29
29
  @outputs = []
30
30
  @mutex = Mutex.new
31
+ @mappings = {}
32
+ @invalid_mapping_keys = []
31
33
 
32
34
  templates = conf.elements.select{|e| e.name == 'template' }
33
35
  if templates.size != 1
@@ -35,6 +37,7 @@ class Fluent::JsonExpanderOutput < Fluent::MultiOutput
35
37
  end
36
38
 
37
39
  @template = templates.first
40
+ @expand_target_keys = scan_keys(@template)
38
41
  end
39
42
 
40
43
  def emit(tag, es, chain)
@@ -57,24 +60,60 @@ class Fluent::JsonExpanderOutput < Fluent::MultiOutput
57
60
  chain.next
58
61
  end
59
62
 
63
+ def shutdown
64
+ super
65
+ @mappings.values.each do |output|
66
+ output.shutdown
67
+ end
68
+ @mappings.clear
69
+ end
70
+
60
71
  private
61
72
 
73
+ SCAN_DATA_RE = /\$\{data\[([_a-zA-Z][_a-zA-Z0-9]*)\]\}/
74
+ def scan_keys(elm)
75
+ elm.inject([]) { |dst, (attr, value)|
76
+ dst.concat(value.scan(SCAN_DATA_RE).flatten)
77
+ }.sort.uniq
78
+ end
79
+
80
+ def to_mapping_key(data)
81
+ data.select{|k, _| @expand_target_keys.include? k }
82
+ .to_a
83
+ .sort_by(&:first)
84
+ .flatten
85
+ .join("::")
86
+ end
87
+
62
88
  def new_output(data)
63
89
  o = nil
64
90
  t = @template
91
+ map_key = to_mapping_key(data)
92
+ if @invalid_mapping_keys.include?(map_key)
93
+ return o, data
94
+ end
95
+
65
96
  begin
66
- @mutex.synchronize do
67
- e, data = expand_elm(t, data)
68
- if e
69
- o = Fluent::Plugin.new_output(@subtype)
70
- o.configure(e)
71
- o.start
72
-
73
- @outputs.push(o)
97
+ o = @mappings[map_key]
98
+ if o
99
+ if @delete_used_key
100
+ @expand_target_keys.each{|k| data.delete(k) }
101
+ end
102
+ else
103
+ @mutex.synchronize do
104
+ e, data = expand_elm(t, data)
105
+ if e
106
+ o = Fluent::Plugin.new_output(@subtype)
107
+ o.configure(e)
108
+ o.start
109
+
110
+ @outputs.push(o)
111
+ @mappings[map_key] = o
112
+ end
74
113
  end
75
- end
76
114
 
77
- log.info "[out_json_expand] Expanded new output: #{@subtype}"
115
+ log.info "[out_json_expand] Expanded new output: #{@subtype}"
116
+ end
78
117
  rescue Fluent::ConfigError => e
79
118
  log.error "failed to configure sub output #{@subtype}: #{e.message}"
80
119
  log.error e.backtrace.join("\n")
@@ -87,16 +126,20 @@ class Fluent::JsonExpanderOutput < Fluent::MultiOutput
87
126
  o = nil
88
127
  end
89
128
 
129
+ unless o
130
+ @invalid_mapping_keys << map_key
131
+ end
132
+
90
133
  return o, data
91
134
  end
92
135
 
93
- SCAN_DATA_RE = /\$\{data\[(?:[_a-zA-Z][_a-zA-Z0-9]*)\]\}/
136
+ GSUB_DATA_RE = /\$\{data\[(?:[_a-zA-Z][_a-zA-Z0-9]*)\]\}/
94
137
  SCAN_KEY_NAME_RE = /\[([_a-zA-Z][_a-zA-Z0-9]*)\]/
95
138
 
96
139
  def expand_elm(template, data)
97
140
  attr = {}
98
141
  template.each do |k, v|
99
- v = v.gsub(SCAN_DATA_RE) do |matched|
142
+ v = v.gsub(GSUB_DATA_RE) do |matched|
100
143
  key_matched = matched.scan(SCAN_KEY_NAME_RE)[0]
101
144
  if !key_matched or !key_matched[0]
102
145
  raise(Fluent::ConfigError, "[BUG] data matched in template, but could not find key name")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-json_expander
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Uchio KONDO