logstash-filter-memoize 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c65bd92d7752bac24f3628bd6e8e1cf6602c86f93352e7407b1af93a74460387
4
+ data.tar.gz: f00ff3c23edb2e739a856df0ccc75b7ca72a4529aa07967399dc0df9caf3230c
5
+ SHA512:
6
+ metadata.gz: fcbd837e0783fbb51b6bfd01054db16dfa66da3dc414e8e15a8c4551d80b99587b4581c1c3a6d4d5c131397dc5467278faa4ca9475242aa2d1cdb678ec0c71db
7
+ data.tar.gz: 9312c84637543d6361d487fc5b85da96aad8e24726507b101d3a19f5e1a3046b07fcff8c5098d8ac42529358f4abcac2f3341196667d3ceab48e78d08d61662a
@@ -0,0 +1,4 @@
1
+ ## 1.0.0
2
+ - Plugin created with the logstash plugin generator
3
+ - Implemented memoization using [lru_redux](https://github.com/SamSaffron/lru_redux).
4
+ - Following options are supported: `key`, `fields`, `filter_name`, `filter_options`, `cache_size`, `ttl`
@@ -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
+ * sw.jung - kjss10@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.
@@ -0,0 +1,2 @@
1
+ # logstash-filter-memoize
2
+ Example filter plugin. This should help bootstrap your effort to write your own filter plugin!
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
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.
@@ -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,79 @@
1
+ # encoding: utf-8
2
+ require "logstash/filters/base"
3
+ require "logstash/namespace"
4
+ require "lru_redux"
5
+
6
+ # This filter provides https://en.wikipedia.org/wiki/Memoization[memoization] to wrapped filter.
7
+ # Internally, It based on https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU[LRU] cache algorithm.
8
+ #
9
+ # See below an example of how this filter might be used.
10
+ # [source,ruby]
11
+ # --------------------------------------------------
12
+ # filter {
13
+ # memoize {
14
+ # key => "%{host}" <1>
15
+ # fields => ["host_owner", "host_location"] <2>
16
+ # filter_name => "elasticsearch" <3>
17
+ # filter_options => { <4>
18
+ # query => "host:%{host}"
19
+ # index => "known_host"
20
+ # fields => {
21
+ # "host_owner" => "host_owner"
22
+ # "host_location" => "host_location"
23
+ # }
24
+ # }
25
+ # }
26
+ # }
27
+ # --------------------------------------------------
28
+ #
29
+ # * When an event with a new <1> key comes in, execute wrapped <3> <4> filter and caches the <2> fields value.
30
+ # * When an event with a same <1> key comes in, sets cached value to target <2> fields without wrapped <3> <4> filter execution.
31
+ #
32
+ class LogStash::Filters::Memoize < LogStash::Filters::Base
33
+
34
+ config_name "memoize"
35
+
36
+ # The key to use caching and retrieving values. It can be dynamic and include parts of the event using the %{field}.
37
+ config :key, :validate => :string, :required => true
38
+
39
+ # The fields to be cached from result, or to be set cached value.
40
+ config :fields, :validate => :array, :required => true
41
+
42
+ # The filter name what you want to use.
43
+ config :filter_name, :validate => :string, :required => true
44
+
45
+ # The filter options what you want to use. You can use all of options in the `filter_name` filter.
46
+ config :filter_options, :validate => :hash, :default => {}
47
+
48
+ # Maximum size of cache.
49
+ config :cache_size, :validate => :number, :default => 1000
50
+
51
+ # The TTL(Time To Live) of cached value.
52
+ config :ttl, :validate => :number
53
+
54
+ public
55
+ def register
56
+ @filter = LogStash::Plugin.lookup("filter", @filter_name).new(@filter_options)
57
+ @filter.register
58
+ @cache = ::LruRedux::TTL::ThreadSafeCache.new(@cache_size, @ttl)
59
+ end # def register
60
+
61
+ public
62
+ def filter(event)
63
+ formattedKey = event.sprintf(@key);
64
+ result = @cache[formattedKey]
65
+
66
+ if !result.nil?
67
+ @logger.debug("Cached value found.", :key => formattedKey, :value => result) if @logger.debug?
68
+ @fields.each { |field| event.set(field, result[field]) }
69
+ else
70
+ @logger.debug("Cached value not found. Do filter.", :key => formattedKey, :filter => @filter) if @logger.debug?
71
+ @filter.filter(event)
72
+ @fields.each { |field| (result ||= {})[field] = event.get(field) }
73
+ @cache[formattedKey] = result
74
+ end
75
+
76
+ # filter_matched should go in the last line of our successful code
77
+ filter_matched(event)
78
+ end # def filter
79
+ end # class LogStash::Filters::Memoize
@@ -0,0 +1,26 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-filter-memoize'
3
+ s.version = '1.0.0'
4
+ s.licenses = ['Apache License (2.0)']
5
+ s.summary = 'This filter-plugin provides memoization to any filter you want.'
6
+ s.description = 'This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program'
7
+ s.homepage = 'https://github.com/sw-jung/logstash-filter-memoize'
8
+ s.authors = ['sw.jung']
9
+ s.email = 'kjss10@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_runtime_dependency 'lru_redux'
23
+ s.add_development_dependency 'logstash-devutils'
24
+ s.add_development_dependency 'logstash-filter-ruby'
25
+ s.add_development_dependency 'logstash-filter-sleep'
26
+ end
@@ -0,0 +1,109 @@
1
+ # encoding: utf-8
2
+ require_relative '../spec_helper'
3
+ require "logstash/filters/memoize"
4
+
5
+ describe LogStash::Filters::Memoize do
6
+ describe "basic work" do
7
+ let(:config) do <<-CONFIG
8
+ filter {
9
+ ruby {
10
+ code => "event.set('time', Time.now.to_f)"
11
+ }
12
+
13
+ memoize {
14
+ key => "%{key}"
15
+ fields => ["result"]
16
+ filter_name => "ruby"
17
+ filter_options => {
18
+ code => "event.set('result', event.get('time'))"
19
+ }
20
+ }
21
+ }
22
+ CONFIG
23
+ end
24
+
25
+ sample([
26
+ {"key" => "cache_key"},
27
+ {"key" => "cache_key2"},
28
+ {"key" => "cache_key"}
29
+ ]) do
30
+ # If key is different, result different also.
31
+ expect(subject[0].get("time")).to eq(subject[0].get("result"))
32
+ expect(subject[1].get("time")).to eq(subject[1].get("result"))
33
+ expect(subject[0].get("time")).not_to eq(subject[1].get("result"))
34
+
35
+ # If key is same, cache works
36
+ expect(subject[0].get("time")).to eq(subject[0].get("result"))
37
+ expect(subject[2].get("time")).not_to eq(subject[2].get("result"))
38
+ expect(subject[0].get("time")).to eq(subject[2].get("result"))
39
+ end
40
+ end
41
+
42
+ describe "ttl work" do
43
+ let(:config) do <<-CONFIG
44
+ filter {
45
+ ruby {
46
+ code => "event.set('time', Time.now.to_f)"
47
+ }
48
+
49
+ memoize {
50
+ key => "%{key}"
51
+ fields => ["result"]
52
+ filter_name => "ruby"
53
+ filter_options => {
54
+ code => "event.set('result', event.get('time'))"
55
+ }
56
+ ttl => 1
57
+ }
58
+
59
+ sleep {
60
+ time => "2"
61
+ }
62
+ }
63
+ CONFIG
64
+ end
65
+
66
+ sample([
67
+ {"key" => "cache_key"},
68
+ {"key" => "cache_key"}
69
+ ]) do
70
+ # After ttl seconds, cached value are delete
71
+ expect(subject[0].get("time")).to eq(subject[0].get("result"))
72
+ expect(subject[1].get("time")).to eq(subject[1].get("result"))
73
+ expect(subject[0].get("time")).not_to eq(subject[1].get("result"))
74
+ end
75
+ end
76
+
77
+ describe "cache_size work" do
78
+ let(:config) do <<-CONFIG
79
+ filter {
80
+ ruby {
81
+ code => "event.set('time', Time.now.to_f)"
82
+ }
83
+
84
+ memoize {
85
+ key => "%{key}"
86
+ fields => ["result"]
87
+ filter_name => "ruby"
88
+ filter_options => {
89
+ code => "event.set('result', event.get('time'))"
90
+ }
91
+ cache_size => 1
92
+ }
93
+ }
94
+ CONFIG
95
+ end
96
+
97
+ sample([
98
+ {"key" => "cache_key"},
99
+ {"key" => "cache_key2"},
100
+ {"key" => "cache_key"}
101
+ ]) do
102
+ # If cache over cache size, old cached value are delete first
103
+ expect(subject[0].get("time")).to eq(subject[0].get("result"))
104
+ expect(subject[1].get("time")).to eq(subject[1].get("result"))
105
+ expect(subject[2].get("time")).to eq(subject[2].get("result"))
106
+ expect(subject[0].get("time")).not_to eq(subject[2].get("result"))
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,2 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-memoize
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - sw.jung
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '2.0'
19
+ name: logstash-core-plugin-api
20
+ prerelease: false
21
+ type: :runtime
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
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ name: lru_redux
34
+ prerelease: false
35
+ type: :runtime
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ name: logstash-devutils
48
+ prerelease: false
49
+ type: :development
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ name: logstash-filter-ruby
62
+ prerelease: false
63
+ type: :development
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ name: logstash-filter-sleep
76
+ prerelease: false
77
+ type: :development
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: This gem is a Logstash plugin required to be installed on top of the
84
+ Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This
85
+ gem is not a stand-alone program
86
+ email: kjss10@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - CHANGELOG.md
92
+ - CONTRIBUTORS
93
+ - DEVELOPER.md
94
+ - Gemfile
95
+ - LICENSE
96
+ - README.md
97
+ - lib/logstash/filters/memoize.rb
98
+ - logstash-filter-memoize.gemspec
99
+ - spec/filters/memoize_spec.rb
100
+ - spec/spec_helper.rb
101
+ homepage: https://github.com/sw-jung/logstash-filter-memoize
102
+ licenses:
103
+ - Apache License (2.0)
104
+ metadata:
105
+ logstash_plugin: 'true'
106
+ logstash_group: filter
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.6.13
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: This filter-plugin provides memoization to any filter you want.
127
+ test_files:
128
+ - spec/filters/memoize_spec.rb
129
+ - spec/spec_helper.rb