logstash-filter-foreach 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a0886340fbbf9bc4309412b39c1027727228b89b
4
+ data.tar.gz: 70c2b1cb1e19aed3582ef413f1a13a160f680721
5
+ SHA512:
6
+ metadata.gz: d0d562ddc6f0f2e59dd478a8dc34a248fe2b1e39020664cdfdab50a73f2120fdbbc2a3f15db54ede121ac862da2b1df0283ed419163c3d7d7a5a80afd3a56155
7
+ data.tar.gz: e292a5fd0c3f74746a87da61d9574013f6337b234d6a3bb7c20f0adec949dc27cc540d2e37e32e4cd959aab020693b4af6592fb74bbd694f258c551dae5d1cdc
data/CHANGELOG.md ADDED
@@ -0,0 +1,2 @@
1
+ ## 0.1.0
2
+ - Plugin created with the logstash plugin generator
data/CONTRIBUTORS ADDED
@@ -0,0 +1,10 @@
1
+ The following is a list of people who have contributed ideas, code, bug
2
+ reports, or in general have helped logstash along its way.
3
+
4
+ Contributors:
5
+ * Alexander Shepel - al.iiieii@gmail.com
6
+
7
+ Note: If you've sent us patches, bug reports, or otherwise contributed to
8
+ Logstash, and you aren't on the list above and want to be, please let us know
9
+ and we'll make sure you're here. Contributions from folks like you are what make
10
+ open source awesome.
data/DEVELOPER.md ADDED
@@ -0,0 +1,2 @@
1
+ # logstash-filter-foreach
2
+ Example filter plugin. This should help bootstrap your effort to write your own filter plugin!
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
data/LICENSE ADDED
@@ -0,0 +1,11 @@
1
+ Licensed under the Apache License, Version 2.0 (the "License");
2
+ you may not use this file except in compliance with the License.
3
+ You may obtain a copy of the License at
4
+
5
+ http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software
8
+ distributed under the License is distributed on an "AS IS" BASIS,
9
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ See the License for the specific language governing permissions and
11
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Logstash Plugin
2
+
3
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
4
+
5
+ It is fully free and fully open source. The license is Apache 2.0, meaning you are pretty much free to use it however you want in whatever way.
6
+
7
+ ## Documentation
8
+
9
+ Logstash provides infrastructure to automatically generate documentation for this plugin. We use the asciidoc format to write documentation so any comments in the source code will be first converted into asciidoc and then into html. All plugin documentation are placed under one [central location](http://www.elastic.co/guide/en/logstash/current/).
10
+
11
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
12
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
13
+
14
+ ## Need Help?
15
+
16
+ Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
17
+
18
+ ## Developing
19
+
20
+ ### 1. Plugin Developement and Testing
21
+
22
+ #### Code
23
+ - To get started, you'll need JRuby with the Bundler gem installed.
24
+
25
+ - Create a new plugin or clone and existing from the GitHub [logstash-plugins](https://github.com/logstash-plugins) organization. We also provide [example plugins](https://github.com/logstash-plugins?query=example).
26
+
27
+ - Install dependencies
28
+ ```sh
29
+ bundle install
30
+ ```
31
+
32
+ #### Test
33
+
34
+ - Update your dependencies
35
+
36
+ ```sh
37
+ bundle install
38
+ ```
39
+
40
+ - Run tests
41
+
42
+ ```sh
43
+ bundle exec rspec
44
+ ```
45
+
46
+ ### 2. Running your unpublished Plugin in Logstash
47
+
48
+ #### 2.1 Run in a local Logstash clone
49
+
50
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
51
+ ```ruby
52
+ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
53
+ ```
54
+ - Install plugin
55
+ ```sh
56
+ bin/logstash-plugin install --no-verify
57
+ ```
58
+ - Run Logstash with your plugin
59
+ ```sh
60
+ bin/logstash -e 'filter {awesome {}}'
61
+ ```
62
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
63
+
64
+ #### 2.2 Run in an installed Logstash
65
+
66
+ You can use the same **2.1** method to run your plugin in an installed Logstash by editing its `Gemfile` and pointing the `:path` to your local plugin development directory or you can build the gem and install it using:
67
+
68
+ - Build your plugin gem
69
+ ```sh
70
+ gem build logstash-filter-awesome.gemspec
71
+ ```
72
+ - Install the plugin from the Logstash home
73
+ ```sh
74
+ bin/logstash-plugin install /your/local/plugin/logstash-filter-awesome.gem
75
+ ```
76
+ - Start Logstash and proceed to test the plugin
77
+
78
+ ## Contributing
79
+
80
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
81
+
82
+ Programming is not a required skill. Whatever you've seen about open source and maintainers or community members saying "send patches or die" - you will not see that here.
83
+
84
+ It is more important to the community that you are able to contribute.
85
+
86
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1,274 @@
1
+ # encoding: utf-8
2
+ require "logstash/filters/base"
3
+ require "logstash/namespace"
4
+
5
+ # This filter will split event by array field, and later join back
6
+ class LogStash::Filters::Foreach < LogStash::Filters::Base
7
+
8
+ FAILURE_TAG = '_foreach_failure'.freeze
9
+
10
+ #
11
+ # filter {
12
+ # foreach {
13
+ # task_id => "%{task_id}"
14
+ # array_field => "field_name"
15
+ # join_fields => ["join_field_name", "join_field_name2"]
16
+ # }
17
+ # }
18
+ #
19
+ # ... Process records
20
+ #
21
+ # filter {
22
+ # foreach {
23
+ # task_id => "%{task_id}"
24
+ # end => true
25
+ # }
26
+ # }
27
+ #
28
+ config_name "foreach"
29
+
30
+ config :task_id, :validate => :string, :required => true
31
+ config :array_field, :validate => :string
32
+ config :end, :validate => :boolean, :default => false
33
+ config :join_fields, :validate => :array
34
+ config :timeout, :validate => :number, :default => 60
35
+
36
+ @@configuration_data = {}
37
+ @@event_data = {}
38
+
39
+ @@mutex = Mutex.new
40
+
41
+
42
+ public
43
+ def register
44
+
45
+ # validate task_id option
46
+ if !@task_id.match(/%\{.+\}/)
47
+ raise LogStash::ConfigurationError, "Foreach plugin: task_id pattern '#{@task_id}' must contain a dynamic expression like '%{field}'"
48
+ end
49
+
50
+ if !@end
51
+ if @@configuration_data.has_key?(@task_id)
52
+ raise LogStash::ConfigurationError, "Foreach plugin: For task_id pattern '#{@task_id}', there are more than one filters defined. There should be only one `start` and one `end` filter with the same task_id."
53
+ end
54
+ if !@array_field.is_a?(String)
55
+ raise LogStash::ConfigurationError, "Foreach plugin: For task_id pattern '#{@task_id}': array_field should be a field name, but it is of type = #{@array_field.class}"
56
+ end
57
+ if !@join_fields.is_a?(Array)
58
+ raise LogStash::ConfigurationError, "Foreach plugin: For task_id pattern '#{@task_id}': join_fields should be an Array of fields, but it is of type = #{@join_fields.class}"
59
+ end
60
+ @@configuration_data[@task_id] = LogStash::Filters::Foreach::Configuration.new(@array_field, @join_fields, @timeout)
61
+ else
62
+ if !@@configuration_data.has_key?(@task_id)
63
+ raise LogStash::ConfigurationError, "Foreach plugin: For task_id pattern '#{@task_id}', there are no `start` filter. You should declare `start` filter before `end` filter."
64
+ elsif @@configuration_data[@task_id].end_filter_configured
65
+ raise LogStash::ConfigurationError, "Foreach plugin: For task_id pattern '#{@task_id}', there are more than one filters defined. There should be only one `start` and one `end` filter with the same task_id."
66
+ end
67
+ @@configuration_data[@task_id].end_filter_configured = true
68
+ end
69
+
70
+
71
+ end
72
+
73
+ # def register
74
+
75
+ public
76
+ def filter(event)
77
+
78
+ @logger.debug("Foreach plugin:", :task_id => @task_id, :array_field => @array_field, :join_fields => @join_fields, :end => @end, :timeout => @timeout, :event => event.to_hash)
79
+
80
+ passthrough = false
81
+
82
+ task_id = event.sprintf(@task_id)
83
+ if task_id.nil? || task_id == @task_id
84
+
85
+ @logger.trace("Foreach plugin: if task_id.nil? || task_id == @task_id");
86
+
87
+ @logger.warn("Foreach plugin: #{@task_id} should be calculated into value (not '#{task_id}'). Passing through")
88
+ event.tag(FAILURE_TAG)
89
+ passthrough = true
90
+
91
+ else
92
+
93
+ @logger.trace("Foreach plugin: else task_id.nil? || task_id == @task_id");
94
+
95
+ @@mutex.synchronize do
96
+
97
+ if !@@configuration_data.has_key?(@task_id) or !@@configuration_data[@task_id].end_filter_configured
98
+
99
+ @logger.trace("Foreach plugin: if !@@configuration_data.has_key?(@task_id) or !@@configuration_data[@task_id].end_filter_configured");
100
+
101
+ raise LogStash::ConfigurationError, "Foreach plugin: For task_id pattern '#{@task_id}', there should be one `start` and one `end` filter."
102
+ end
103
+
104
+ configuration = @@configuration_data[@task_id]
105
+
106
+ if !@end
107
+
108
+ @logger.trace("Foreach plugin: if !@end");
109
+
110
+ array_field = event.get(@array_field)
111
+
112
+ if !array_field.is_a?(Array)
113
+
114
+ @logger.trace("Foreach plugin: if !array_field.is_a?(Array)");
115
+
116
+ @logger.warn("Foreach plugin: Field should be of Array type. field:#{@array_field} is of type = #{array_field.class}. Passing through")
117
+ event.tag(FAILURE_TAG)
118
+ passthrough = true
119
+
120
+ elsif @@event_data.has_key?(task_id)
121
+
122
+ @logger.trace("Foreach plugin: elsif @@event_data.has_key?(task_id)");
123
+
124
+ @logger.warn("Foreach plugin: task_id whould be unique. Duplicate value found: '#{task_id}'. Passing through")
125
+ event.tag(FAILURE_TAG)
126
+ passthrough = true
127
+
128
+ else
129
+
130
+ @logger.trace("Foreach plugin: else !array_field.is_a?(Array)");
131
+
132
+ @@event_data[task_id] = LogStash::Filters::Foreach::Element.new(configuration, Time.now(), event.clone, configuration.join_fields)
133
+ event_data = @@event_data[task_id]
134
+
135
+ array_field.each do |value|
136
+
137
+ @logger.trace("Foreach plugin: array_field.each do |value|", :value => value);
138
+
139
+ next if value.nil? or value.empty?
140
+
141
+ event_split = event.clone
142
+ @logger.debug("Foreach plugin: Split event", :field => @array_field, :value => value)
143
+ event_split.set(@array_field, value)
144
+ event_data.counter += 1
145
+
146
+ filter_matched(event_split)
147
+ yield event_split
148
+ end
149
+
150
+ event.cancel
151
+
152
+ end
153
+
154
+ else
155
+
156
+ @logger.trace("Foreach plugin: else !@end");
157
+
158
+ if !@@event_data.has_key?(task_id)
159
+
160
+ @logger.trace("Foreach plugin: if !@@event_data.has_key?(task_id)");
161
+
162
+ @logger.warn("Foreach plugin: found `end` event fot task_id = '#{task_id}' without `start` event. Passing through")
163
+ event.tag(FAILURE_TAG)
164
+ passthrough = true
165
+
166
+ else
167
+
168
+ @logger.trace("Foreach plugin: else !@@event_data.has_key?(task_id)");
169
+
170
+ @logger.debug("Foreach plugin: Join event back", :field => configuration.array_field, :value => event.get(configuration.array_field))
171
+
172
+ event_data = @@event_data[task_id]
173
+ event_data.lastevent_timestamp = Time.now()
174
+
175
+ configuration.join_fields.each do |join_field|
176
+ event_data.join_fields[join_field] += [*event.get(join_field)]
177
+ end
178
+ event_data.counter -= 1
179
+
180
+ if event_data.counter == 0
181
+
182
+ @logger.trace("Foreach plugin: if event_data.counter == 0");
183
+
184
+ configuration.join_fields.each do |join_field|
185
+ event_data.initial_event.set(join_field, event_data.join_fields[join_field])
186
+ end
187
+ filter_matched(event_data.initial_event)
188
+ yield event_data.initial_event
189
+ @@event_data.delete(task_id)
190
+ end
191
+
192
+ event.cancel
193
+
194
+ end
195
+
196
+ end
197
+
198
+ end # @@mutex.synchronize
199
+
200
+ end # task_id.nil? || task_id == @task_id
201
+
202
+ if passthrough
203
+
204
+ @logger.trace("Foreach plugin: if passthrough");
205
+
206
+ filter_matched(event)
207
+ end
208
+
209
+ end
210
+
211
+ # def filter
212
+
213
+ def flush(options = {})
214
+ events_to_flush = []
215
+ if @end
216
+ @@mutex.synchronize do
217
+ @@event_data.each do |task_id, obj|
218
+ if obj.lastevent_timestamp < Time.now() - obj.configuration.timeout
219
+ if obj.counter < obj.sub_events_count
220
+ @logger.warn("Foreach plugin: Flushing partly processed event with task_id = '#{obj.initial_event.sprintf(@task_id)}' after timeout = '#{obj.configuration.timeout.to_s}'")
221
+ obj.configuration.join_fields.each do |join_field|
222
+ obj.initial_event.set(join_field, obj.join_fields[join_field])
223
+ end
224
+ events_to_flush << obj.initial_event
225
+ else
226
+ @logger.warn("Foreach plugin: Removing unprocessed event with task_id = '#{obj.initial_event.sprintf(@task_id)}' after timeout = '#{obj.configuration.timeout.to_s}'")
227
+ end
228
+ @@event_data.delete(task_id)
229
+ end
230
+ end
231
+ end # @@mutex.synchronize
232
+ end
233
+ return events_to_flush
234
+
235
+ end
236
+
237
+ # def flush
238
+
239
+ def periodic_flush
240
+ true
241
+ end
242
+
243
+ end # class LogStash::Filters::Foreach
244
+
245
+ # Element of "event_data"
246
+ class LogStash::Filters::Foreach::Configuration
247
+
248
+ attr_accessor :end_filter_configured, :array_field, :join_fields, :timeout
249
+
250
+ def initialize(array_field, join_fields, timeout)
251
+ @end_filter_configured = false
252
+ @array_field = array_field
253
+ @join_fields = join_fields
254
+ @timeout = timeout
255
+ end
256
+ end
257
+
258
+ class LogStash::Filters::Foreach::Element
259
+
260
+ attr_accessor :initial_event, :counter, :sub_events_count, :join_fields, :lastevent_timestamp, :configuration
261
+
262
+ def initialize(configuration, creation_timestamp, event, join_fields)
263
+ # @creation_timestamp = creation_timestamp
264
+ @configuration = configuration
265
+ @lastevent_timestamp = creation_timestamp
266
+ @initial_event = event
267
+ @counter = 0
268
+ @sub_events_count = event.get(configuration.array_field).length
269
+ @join_fields = {}
270
+ join_fields.each do |join_field|
271
+ @join_fields[join_field] = []
272
+ end
273
+ end
274
+ end
@@ -0,0 +1,25 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-filter-foreach'
3
+ s.version = '0.1.0'
4
+ s.licenses = ['Apache-2.0']
5
+ s.summary = 'Process filters for every array item'
6
+ s.description = 'Plugin splits event for every item in array, then you could process other filters for every item and then join event back'
7
+ s.homepage = 'https://github.com/IIIEII/logstash-filter-foreach'
8
+ s.authors = ['IIIEII']
9
+ s.email = 'al.iiieii@gmail.com'
10
+ s.require_paths = ['lib']
11
+
12
+ # Files
13
+ s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
14
+ # Tests
15
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
16
+
17
+ # Special flag to let us know this is actually a logstash plugin
18
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "filter" }
19
+
20
+ # Gem dependencies
21
+ s.add_runtime_dependency "logstash-core-plugin-api", "~> 2.0"
22
+ s.add_development_dependency 'logstash-filter-mutate'
23
+ s.add_development_dependency 'logstash-filter-drop'
24
+ s.add_development_dependency 'logstash-devutils', "~> 1.3", ">= 1.3.1"
25
+ end
@@ -0,0 +1,336 @@
1
+ # encoding: utf-8
2
+ require_relative '../spec_helper'
3
+ require "logstash/filters/foreach"
4
+ require "logstash/filters/mutate"
5
+ require "logstash/filters/drop"
6
+ require "rspec/wait"
7
+
8
+ java_import org.apache.logging.log4j.LogManager
9
+
10
+ describe LogStash::Filters::Foreach do
11
+
12
+ before(:each) do
13
+ # LogStash::Logging::Logger::configure_logging("DEBUG", "logstash.filters.foreach")
14
+ LogStash::Filters::Foreach.class_variable_get(:@@configuration_data).clear()
15
+ end
16
+
17
+ after(:each) do
18
+ # LogStash::Logging::Logger::configure_logging("ERROR", "logstash.filters.foreach")
19
+ end
20
+
21
+ context "In validation stage" do
22
+
23
+ describe "should throw exception without end filter" do
24
+ let(:config) do
25
+ <<-CONFIG
26
+ filter {
27
+ foreach {
28
+ task_id => "%{task_id}"
29
+ array_field => "array"
30
+ join_fields => ["join"]
31
+ }
32
+ }
33
+ CONFIG
34
+ end
35
+
36
+ sample("task_id" => 1, "array" => ["big", "bird", "sesame street"], "unchanged" => "unchanged_value") do
37
+ insist { subject }.raises(LogStash::ConfigurationError)
38
+ end
39
+ end
40
+
41
+ describe "should throw exception without start filter" do
42
+ let(:config) do
43
+ <<-CONFIG
44
+ filter {
45
+ foreach {
46
+ task_id => "%{task_id}"
47
+ end => true
48
+ }
49
+ }
50
+ CONFIG
51
+ end
52
+
53
+ sample("task_id" => 1, "array" => ["big", "bird", "sesame street"], "unchanged" => "unchanged_value") do
54
+ insist { subject }.raises(LogStash::ConfigurationError)
55
+ end
56
+ end
57
+
58
+ describe "should throw exception with two start filters" do
59
+ let(:config) do
60
+ <<-CONFIG
61
+ filter {
62
+ foreach {
63
+ task_id => "%{task_id}"
64
+ array_field => "array"
65
+ join_fields => ["join"]
66
+ }
67
+ }
68
+ filter {
69
+ foreach {
70
+ task_id => "%{task_id}"
71
+ array_field => "array"
72
+ join_fields => ["join"]
73
+ }
74
+ }
75
+ filter {
76
+ foreach {
77
+ task_id => "%{task_id}"
78
+ end => true
79
+ }
80
+ }
81
+ CONFIG
82
+ end
83
+
84
+ sample("task_id" => 1, "array" => ["big", "bird", "sesame street"], "unchanged" => "unchanged_value") do
85
+ insist { subject }.raises(LogStash::ConfigurationError)
86
+ end
87
+ end
88
+
89
+ describe "should throw exception with two end filters" do
90
+ let(:config) do
91
+ <<-CONFIG
92
+ filter {
93
+ foreach {
94
+ task_id => "%{task_id}"
95
+ array_field => "array"
96
+ join_fields => ["join"]
97
+ }
98
+ }
99
+ filter {
100
+ foreach {
101
+ task_id => "%{task_id}"
102
+ end => true
103
+ }
104
+ }
105
+ filter {
106
+ foreach {
107
+ task_id => "%{task_id}"
108
+ end => true
109
+ }
110
+ }
111
+ CONFIG
112
+ end
113
+
114
+ sample("task_id" => 1, "array" => ["big", "bird", "sesame street"], "unchanged" => "unchanged_value") do
115
+ insist { subject }.raises(LogStash::ConfigurationError)
116
+ end
117
+ end
118
+
119
+ end
120
+
121
+ context "Filtering" do
122
+
123
+ describe "should split and join correctly" do
124
+ let(:config) do
125
+ <<-CONFIG
126
+ filter {
127
+ foreach {
128
+ task_id => "%{task_id}"
129
+ array_field => "array"
130
+ join_fields => ["join"]
131
+ }
132
+
133
+ mutate {
134
+ add_field => { "join" => "%{array}_changed" }
135
+ }
136
+
137
+ foreach {
138
+ task_id => "%{task_id}"
139
+ end => true
140
+ }
141
+ }
142
+ CONFIG
143
+ end
144
+
145
+ sample("task_id" => 1, "array" => ["big", "bird", "sesame street"], "unchanged" => "unchanged_value") do
146
+ insist { subject.is_a?(LogStash::Event) } == true
147
+ insist { subject.get("array").is_a?(Array) } == true
148
+ insist { subject.get("array") } == ["big", "bird", "sesame street"]
149
+ insist { subject.get("join").is_a?(Array) } == true
150
+ insist { subject.get("join") } == ["big_changed", "bird_changed", "sesame street_changed"]
151
+ insist { subject.get("unchanged").is_a?(String) } == true
152
+ insist { subject.get("unchanged") } == "unchanged_value"
153
+ end
154
+ end
155
+
156
+ describe "should passthrough event with incorrect task_id" do
157
+ let(:config) do
158
+ <<-CONFIG
159
+ filter {
160
+ foreach {
161
+ task_id => "%{task_id}"
162
+ array_field => "array"
163
+ join_fields => ["join"]
164
+ }
165
+
166
+ mutate {
167
+ add_field => { "join" => "%{array}_changed" }
168
+ }
169
+
170
+ foreach {
171
+ task_id => "%{task_id}"
172
+ end => true
173
+ }
174
+ }
175
+ CONFIG
176
+ end
177
+
178
+ sample("unchanged" => "unchanged_value") do
179
+ insist { subject.is_a?(LogStash::Event) } == true
180
+ insist { subject.get("join").is_a?(String) } == true
181
+ insist { subject.get("join") } == "%{array}_changed"
182
+ insist { subject.get("unchanged").is_a?(String) } == true
183
+ insist { subject.get("unchanged") } == "unchanged_value"
184
+ end
185
+ end
186
+
187
+ describe "should passthrough event without array_field" do
188
+ let(:config) do
189
+ <<-CONFIG
190
+ filter {
191
+ foreach {
192
+ task_id => "%{task_id}"
193
+ array_field => "array"
194
+ join_fields => ["join"]
195
+ }
196
+
197
+ mutate {
198
+ add_field => { "join" => "%{array}_changed" }
199
+ }
200
+
201
+ foreach {
202
+ task_id => "%{task_id}"
203
+ end => true
204
+ }
205
+ }
206
+ CONFIG
207
+ end
208
+
209
+ sample("task_id" => 1, "unchanged" => "unchanged_value") do
210
+ insist { subject.is_a?(LogStash::Event) } == true
211
+ insist { subject.get("join").is_a?(String) } == true
212
+ insist { subject.get("join") } == "%{array}_changed"
213
+ insist { subject.get("unchanged").is_a?(String) } == true
214
+ insist { subject.get("unchanged") } == "unchanged_value"
215
+ end
216
+ end
217
+
218
+ describe "should split and join (partly) correctly" do
219
+ let(:config) do
220
+ <<-CONFIG
221
+ filter {
222
+ foreach {
223
+ task_id => "%{task_id}"
224
+ array_field => "array"
225
+ join_fields => ["join"]
226
+ }
227
+
228
+ if [array] != "bird" {
229
+ mutate {
230
+ add_field => { "join" => "%{array}_changed" }
231
+ }
232
+ }
233
+
234
+ foreach {
235
+ task_id => "%{task_id}"
236
+ end => true
237
+ }
238
+ }
239
+ CONFIG
240
+ end
241
+
242
+ sample("task_id" => 1, "array" => ["big", "bird", "sesame street"], "unchanged" => "unchanged_value") do
243
+ insist { subject.is_a?(LogStash::Event) } == true
244
+ insist { subject.get("array").is_a?(Array) } == true
245
+ insist { subject.get("array") } == ["big", "bird", "sesame street"]
246
+ insist { subject.get("join").is_a?(Array) } == true
247
+ insist { subject.get("join") } == ["big_changed", "sesame street_changed"]
248
+ insist { subject.get("unchanged").is_a?(String) } == true
249
+ insist { subject.get("unchanged") } == "unchanged_value"
250
+ end
251
+ end
252
+
253
+ describe "should clear data on timeout" do
254
+ let(:config) do
255
+ <<-CONFIG
256
+ filter {
257
+ foreach {
258
+ task_id => "%{task_id}"
259
+ array_field => "array"
260
+ join_fields => ["join"]
261
+ timeout => 3
262
+ }
263
+
264
+ drop {}
265
+
266
+ foreach {
267
+ task_id => "%{task_id}"
268
+ end => true
269
+ }
270
+ }
271
+ CONFIG
272
+ end
273
+
274
+ ["1", "2", "3"].each do |task_id|
275
+ sample("task_id" => task_id, "array" => ["big", "bird", "sesame street"], "unchanged" => "unchanged_value") do
276
+ insist { subject.nil? } == true
277
+ insist { LogStash::Filters::Foreach.class_variable_get(:@@event_data).has_key?(task_id) } == true
278
+ sleep 3
279
+ flushed_events = []
280
+ pipeline.flush_filters(:final => false) { |flushed_event| flushed_events << flushed_event }
281
+ insist { LogStash::Filters::Foreach.class_variable_get(:@@event_data).has_key?(task_id) } == false
282
+ insist { flushed_events.length } == 0
283
+ end
284
+ end
285
+
286
+ end
287
+
288
+ describe "should send partial data on timeout" do
289
+ let(:config) do
290
+ <<-CONFIG
291
+ filter {
292
+ foreach {
293
+ task_id => "%{task_id}"
294
+ array_field => "array"
295
+ join_fields => ["join"]
296
+ timeout => 3
297
+ }
298
+
299
+ if [array] == "bird" {
300
+ drop {}
301
+ }
302
+
303
+ mutate {
304
+ add_field => { "join" => "%{array}_changed" }
305
+ }
306
+
307
+ foreach {
308
+ task_id => "%{task_id}"
309
+ end => true
310
+ }
311
+ }
312
+ CONFIG
313
+ end
314
+
315
+ ["1", "2", "3"].each do |task_id|
316
+ sample("task_id" => task_id, "array" => ["big", "bird", "sesame street"], "unchanged" => "unchanged_value") do
317
+ insist { subject.nil? } == true
318
+ insist { LogStash::Filters::Foreach.class_variable_get(:@@event_data).has_key?(task_id) } == true
319
+ sleep 3
320
+ flushed_events = []
321
+ pipeline.flush_filters(:final => false) { |flushed_event| flushed_events << flushed_event }
322
+ insist { LogStash::Filters::Foreach.class_variable_get(:@@event_data).has_key?(task_id) } == false
323
+ insist { flushed_events.length } == 1
324
+ insist { flushed_events[0].get('task_id') } == task_id
325
+ insist { flushed_events[0].get("array").is_a?(Array) } == true
326
+ insist { flushed_events[0].get("array") } == ["big", "bird", "sesame street"]
327
+ insist { flushed_events[0].get("join").is_a?(Array) } == true
328
+ insist { flushed_events[0].get("join") } == ["big_changed", "sesame street_changed"]
329
+ insist { flushed_events[0].get("unchanged").is_a?(String) } == true
330
+ insist { flushed_events[0].get("unchanged") } == "unchanged_value"
331
+ end
332
+ end
333
+
334
+ end
335
+ end
336
+ end
@@ -0,0 +1,2 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-foreach
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - IIIEII
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logstash-core-plugin-api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: logstash-filter-mutate
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: logstash-filter-drop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: logstash-devutils
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ - - '>='
63
+ - !ruby/object:Gem::Version
64
+ version: 1.3.1
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ~>
70
+ - !ruby/object:Gem::Version
71
+ version: '1.3'
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: 1.3.1
75
+ description: Plugin splits event for every item in array, then you could process other
76
+ filters for every item and then join event back
77
+ email: al.iiieii@gmail.com
78
+ executables: []
79
+ extensions: []
80
+ extra_rdoc_files: []
81
+ files:
82
+ - CHANGELOG.md
83
+ - CONTRIBUTORS
84
+ - DEVELOPER.md
85
+ - Gemfile
86
+ - LICENSE
87
+ - README.md
88
+ - lib/logstash/filters/foreach.rb
89
+ - logstash-filter-foreach.gemspec
90
+ - spec/filters/foreach_spec.rb
91
+ - spec/spec_helper.rb
92
+ homepage: https://github.com/IIIEII/logstash-filter-foreach
93
+ licenses:
94
+ - Apache-2.0
95
+ metadata:
96
+ logstash_plugin: 'true'
97
+ logstash_group: filter
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.4.6
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Process filters for every array item
118
+ test_files:
119
+ - spec/filters/foreach_spec.rb
120
+ - spec/spec_helper.rb