fluent-plugin-condition-checker 1.0.0 → 2.0.0

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
  SHA256:
3
- metadata.gz: 14c83969fa6928655e996db271f6dab4483b32e7eb2c8282455a4664c96ab904
4
- data.tar.gz: e437510a11585f4d31f69adca9bd9a714cf4d084eec45e89081565a44c9bfb47
3
+ metadata.gz: 7b410013cd3ab83a4d90088d6391d9fdd6534120326fa35380e58801ab5d76dc
4
+ data.tar.gz: f0f6bc64a9133418e5da83404927d9eb1ba966d4ce9a5d1750cd30939cccc3af
5
5
  SHA512:
6
- metadata.gz: eed247f918a990606d53ab539cc11d0c266bc263e3385a34b3db33caa13b60ef6d1f94958ace69debad63ac6355fa1785cf0515692a7029e4d5afb7d518a530a
7
- data.tar.gz: 17d4ffaa4def66e7baf802da002ef14f70c4e1007d30aab3451b8e949814cdcdbbb5f365ed2c54cef3748ccda3998663fdd89e1af686cc3bc7d372da424c1449
6
+ metadata.gz: 736f29dc6aea324e2834c97b101b0ebd9bc239f7b80e6f82d8133bceedf69da0a31e213886e052133031f049e4bf49ede2190855e9fe278641b05ebdd0b6e772
7
+ data.tar.gz: 8e2b6542d02431f4e37fd75bd3aa524aff0c05b3f4781285d97e48244249721fe7cdc3e53a9d1974ae8f91b4365b24d9149bc4894af57d06030e1a36293c929a
data/.gitignore CHANGED
@@ -1 +1 @@
1
- pkg/
1
+ .DS_Store
@@ -0,0 +1,5 @@
1
+ build:
2
+ gem build fluent-plugin-condition-checker.gemspec
3
+
4
+ release:
5
+ rake release
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
 
4
4
  Gem::Specification.new do |spec|
5
5
  spec.name = "fluent-plugin-condition-checker"
6
- spec.version = "1.0.0"
6
+ spec.version = "2.0.0"
7
7
  spec.authors = ["wally"]
8
8
  spec.email = ["u.str.gm@gmail.com"]
9
9
 
@@ -20,7 +20,7 @@ require 'pp'
20
20
  module Fluent
21
21
  module Plugin
22
22
  class ConditionCheckerOutput < Fluent::Plugin::Output
23
- Fluent::Plugin.register_output("condition_checker", self)
23
+ Fluent::Plugin.register_output('condition_checker', self)
24
24
 
25
25
  helpers :event_emitter
26
26
  PATTERN_MAX_NUM = 20
@@ -39,47 +39,79 @@ module Fluent
39
39
  # it should always be true
40
40
  config_param :auto_typecast, :bool, :default => true, # false for lower version compatibility
41
41
  :desc => 'Automatically cast the field types.'
42
- config_param :tag, :string, :default => 'checked_tag', :desc => "new record tag."
43
-
44
- config_param(
45
- "condition".to_sym,
46
- :hash,
47
- default: nil,
48
- desc: 'It is Conditions.'
49
- )
50
42
 
51
- BUILTIN_CONFIGURATIONS = %W(@id @type @label type output_tag remove_keys renew_record keep_keys enable_ruby renew_time_key auto_typecast tag_else record_else tag)
43
+ # 調整中のconfig_param
44
+ # これで、何個もconditionを設定して、そして処理を決めることができる。
45
+ config_param :tag1, :string,
46
+ :desc => 'Specify the output tag name when the condition1 is true.'
47
+ config_param :condition1, :string,
48
+ :desc => 'Specify the condition1 to evaluate'
49
+ (2..PATTERN_MAX_NUM).each do |i|
50
+ config_param ('tag' + i.to_s).to_sym, :string, default: nil,
51
+ desc: 'Specify tag'+i.to_s+' (not necessary)'
52
+ config_param ('condition' + i.to_s).to_sym, :string, default: nil, # NAME REGEXP
53
+ desc: 'Specify the condition'+i.to_s+' to evaluate (not necessary)'
54
+ end
55
+
56
+ config_param :tag_else, :string,
57
+ :desc => 'Specify the output tag name when the no conditions are true.'
58
+
59
+
60
+ BUILTIN_CONFIGURATIONS = %W(@id @type @label type output_tag remove_keys renew_record keep_keys enable_ruby renew_time_key auto_typecast tag_else record_else)
52
61
 
53
62
  def configure(conf)
54
63
  super
55
64
  # ここで、BUILTIN_CONFIGURATIONS に入っていないものがあった場合はerrorをraise
56
65
  conf.each_pair { |k, v|
66
+ # print k.match('^/tag/\d\d?$')
57
67
  next if BUILTIN_CONFIGURATIONS.include?(k) || k.match(/^condition\d\d?$/) || k.match(/^tag\d\d?$/)
58
68
 
59
69
  raise Fluent::ConfigError, 'out_condition_checker: some weird config is set {'+k.to_s+':'+v.to_s+'}'
60
70
  }
61
71
 
62
- # conditionsを読み込む
72
+ #tagを読み込み
73
+ tags = []
74
+ (1..PATTERN_MAX_NUM).each do |i|
75
+ next unless conf["tag#{i}"]
76
+ tags.push(conf["tag#{i}"]) # tags[i+1] で、欲しいtagにアクセスできる
77
+ end
78
+
79
+ #conditionを読み込み
63
80
  @conditions = []
64
- conf.elements.select { |element| element.name.match(/^condition?$/) }
65
- .each do |param|
66
- condition = {}
67
- data_record = {}
68
-
69
- param.elements.select { |element| element.name.match(/^data?$/) }.each do |data|
70
- data.each_pair do |key, value|
71
- data_record.merge!(key => convert_value(value))
72
- end
73
- end
81
+ (1..PATTERN_MAX_NUM).each do |i|
82
+ next unless conf["condition#{i}"]
83
+ @conditions.push(conf["condition#{i}"])
84
+ end
85
+ if tags.size != @conditions.size
86
+ raise Fluent::ConfigError, 'match the numbers of tags and conditions; number of tags: '+tags.size.to_s+', number of conditions: '+@conditions.size.to_s
87
+ end
74
88
 
75
- param.each_pair do |key, value|
76
- condition.merge!(key => convert_value(value))
77
- end
78
89
 
79
- condition.merge!("data" => data_record)
80
- @conditions.push(condition)
90
+ # maps[i] で、欲しいconditionに対応するrecordにアクセスすることができる。
91
+ # FIXME: conf.elements.forEachでいい感じに回せそう
92
+ maps = []
93
+ (1..PATTERN_MAX_NUM).each do |i|
94
+ next unless conf["condition#{i}"] # 対応するcondition{i}が定義されているものだけ読み込む
95
+ conf.elements.select { |element| element.name == 'record'+i.to_s }.each { |element|
96
+ recordTmp = {}
97
+ element.each_pair { |k, v|
98
+ element.has_key?(k) # to suppress unread configuration warning
99
+ recordTmp.merge!({k => parse_value(v)})
100
+ # map_if_false[k] = parse_value(v)
101
+ }
102
+ maps[i] = recordTmp
103
+ }
81
104
  end
82
105
 
106
+ map_else ={}
107
+ conf.elements.select { |element| element.name == 'record_else' }.each { |element|
108
+ recordTmp = {}
109
+ element.each_pair { |k, v|
110
+ element.has_key?(k) # to suppress unread configuration warning
111
+ recordTmp.merge!({k => parse_value(v)})
112
+ }
113
+ map_else = recordTmp
114
+ }
83
115
 
84
116
  if @remove_keys
85
117
  @remove_keys = @remove_keys.split(',')
@@ -107,6 +139,11 @@ module Fluent
107
139
  end
108
140
 
109
141
 
142
+ @maps = @placeholder_expander.preprocess_map(maps)
143
+ @tags = @placeholder_expander.preprocess_map(tags)
144
+ @tag_else = @placeholder_expander.preprocess_map(conf['tag_else'])
145
+ @map_else = @placeholder_expander.preprocess_map(map_else)
146
+
110
147
  @hostname = Socket.gethostname
111
148
  end
112
149
 
@@ -129,15 +166,23 @@ module Fluent
129
166
  })
130
167
 
131
168
  # TODO: ここの処理よくないって evaluate
132
- matched_conditions, aditional_data = evaluate_condition(@conditions, placeholder_values )
169
+ result, idx = evaluate_condition(@conditions, placeholder_values)
170
+ placeholder_values.merge!({ 'result' => result })
133
171
 
134
- if matched_conditions.size > 0
135
- new_record = reform(record, aditional_data, placeholder_values)
172
+ if idx
173
+ new_tag, new_record = reform(@tags[idx], @maps[idx+1], record, placeholder_values)
174
+ else
175
+ # TODO: tag_elseは使えなくするoption作る"
176
+ new_tag, new_record = reform(@tag_else, @map_else, record, placeholder_values)
177
+ # return
178
+ end
179
+
180
+ if new_tag
136
181
  if @renew_time_key && new_record.has_key?(@renew_time_key)
137
182
  time = new_record[@renew_time_key].to_i
138
183
  end
139
184
  @remove_keys.each {|k| new_record.delete(k) } if @remove_keys
140
- router.emit(@tag, time, new_record)
185
+ router.emit(new_tag, time, new_record)
141
186
  end
142
187
  }
143
188
  rescue => e
@@ -147,16 +192,11 @@ module Fluent
147
192
  private
148
193
 
149
194
  def evaluate_condition(conditions, placeholders)
150
- matched_conditions = []
151
- aditional_data = {}
152
195
  conditions.each_with_index{ |condition, idx|
153
- result = expand_placeholders(condition["rule"], placeholders)
154
- if result
155
- matched_conditions.push(condition["condition"])
156
- aditional_data.merge!(condition["data"])
157
- end
196
+ result = expand_placeholders(condition, placeholders)
197
+ if result then return [result, idx] end
158
198
  }
159
- return matched_conditions, aditional_data
199
+ [@map_else, nil]
160
200
  end
161
201
 
162
202
  def parse_value(value_str)
@@ -170,24 +210,23 @@ module Fluent
170
210
  value_str # emit as string
171
211
  end
172
212
 
173
- def reform(record, aditional_data, placeholder_values)
213
+ def reform(tag, map, record, placeholder_values)
214
+ placeholders = @placeholder_expander.prepare_placeholders(placeholder_values)
215
+ new_tag = expand_placeholders(tag, placeholders)
174
216
  new_record = @renew_record ? {} : record.dup
175
- new_record.merge!(create_record(aditional_data, placeholder_values))
176
217
  @keep_keys.each {|k| new_record[k] = record[k]} if @keep_keys and @renew_record
177
- return new_record
218
+ new_record.merge!(create_record(map, placeholders)) unless map.nil?
219
+ [new_tag, new_record]
178
220
  end
179
221
 
180
222
  def create_record(map, placeholders)
181
223
  new_record = {}
182
- map.each do |k, v|
183
- value = @placeholder_expander.expand(v, placeholders, true)
184
- if value.nil?
185
- new_record.merge!({ k => convert_num(v) })
186
- else
187
- new_record.merge!({ k => convert_num(value) })
188
- end
224
+
225
+ map.each_pair do |k, v|
226
+ new_key = @placeholder_expander.expand(k, placeholders, true)
227
+
228
+ new_record.merge!({ new_key => convert_num(@placeholder_expander.expand(v, placeholders, true)) })
189
229
  end
190
- pp new_record
191
230
 
192
231
  new_record
193
232
  end
@@ -207,19 +246,6 @@ module Fluent
207
246
  end
208
247
  end
209
248
 
210
- def convert_value(value)
211
- # Booleanがチェック
212
- return true if value == 'true'
213
-
214
- return false if value == 'false'
215
-
216
- # 数値データなら数値で返す
217
- return value.to_i if value.to_i.to_s == value.to_s
218
- return value.to_f if value.to_f.to_s == value.to_s
219
-
220
- value
221
- end
222
-
223
249
  def expand_placeholders(value, placeholders)
224
250
  if value.is_a?(String)
225
251
  new_value = @placeholder_expander.expand(value, placeholders)
@@ -14,7 +14,9 @@ class ConditionCheckerOutputTest < Test::Unit::TestCase
14
14
  conf = config_element(
15
15
  'ROOT',
16
16
  '',
17
- {'tag' => "speak_chekc"},
17
+ {'tag1' => "speak_chekc",
18
+ 'tag_else' => 'a',
19
+ 'condition1' => 'b'},
18
20
  [
19
21
  config_element(
20
22
  'condition',
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-condition-checker
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - wally
@@ -84,12 +84,13 @@ files:
84
84
  - Gemfile
85
85
  - Gemfile.lock
86
86
  - LICENSE
87
+ - Makefile
87
88
  - README.md
88
89
  - Rakefile
89
- - example/fluent.conf
90
- - example/scenario.json
91
90
  - fluent-plugin-condition-checker.gemspec
92
91
  - lib/fluent/plugin/out_condition_checker.rb
92
+ - pkg/fluent-plugin-condition-checker-0.1.0.gem
93
+ - pkg/fluent-plugin-condition-checker-1.0.0.gem
93
94
  - test/helper.rb
94
95
  - test/plugin/test_out_condition_checker.rb
95
96
  homepage: https://github.com/NumaoLab/condition-checker-plugin
@@ -1,31 +0,0 @@
1
- <source>
2
- @type forward
3
- port 24224
4
- </source>
5
-
6
- <match **>
7
- @type copy
8
- <store>
9
- @type stdout
10
- </store>
11
- <store>
12
- @type condition-checker
13
- tag "speak_condition"
14
- <condition>
15
- rule record["action"] == "speak"
16
- condition "speak_action"
17
- <data>
18
- word record["word"] + "こんにちは"
19
- aditiona_data 1
20
- </data>
21
- </condition>
22
- <condition>
23
- rule record["action"] == "alert"
24
- condition "alert_action"
25
- <data>
26
- word record["word"] + "はじめまして"
27
- </data>
28
- </condition>
29
- </store>
30
- </match>
31
-
@@ -1 +0,0 @@
1
- {"label":"greeting","priority":2,"limit":30,"action":"greet"}