logstash-filter-lrucache 1.0.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
+ SHA256:
3
+ metadata.gz: 13f2d298a5c8e847bbe8459dcec6eabfdd4cf0028ece4598cc5daf3fe7601299
4
+ data.tar.gz: 59fabc950eaf36864f48d9c9cfbcdf8938c22119d9355214461294be17bb92d0
5
+ SHA512:
6
+ metadata.gz: a5deceda36715a0cd8ab21a1116d25427757f05fb326065cf73c87822db5aa8b32c0ace06e3c24d56bfd968881b8febaf35ab030618d4200267360f6a4627013
7
+ data.tar.gz: 9c7d8159d515edaa24b6d7623115fa12fc311f91a998bf44379f883af9d39f4b9b06424b0e6bf72c58c52dacba3f6a048da6f54a446323a746d7034cb70f20bb
data/CHANGELOG.md ADDED
File without changes
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
+ * 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.
data/DEVELOPER.md ADDED
@@ -0,0 +1,2 @@
1
+ # logstash-filter-lrucache
2
+ Example filter plugin. This should help bootstrap your effort to write your own filter plugin!
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+ logstash_path = ENV["LOGSTASH_PATH"] || "../../logstash"
5
+ use_logstash_source = ENV["LOGSTASH_SOURCE"] && ENV["LOGSTASH_SOURCE"].to_s == "1"
6
+
7
+ if Dir.exist?(logstash_path) && use_logstash_source
8
+ gem 'logstash-core', :path => "#{logstash_path}/logstash-core"
9
+ gem 'logstash-core-plugin-api', :path => "#{logstash_path}/logstash-core-plugin-api"
10
+ end
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,117 @@
1
+ # encoding: utf-8
2
+ require "logstash/filters/base"
3
+ require "logstash/namespace"
4
+ require "lru_redux"
5
+
6
+ # The lrucache filter provides caching using the LRU(Least Recently Used) algorithm.
7
+ #
8
+ # If you are use filter that retrieve values ​​dynamically from external data sources,
9
+ # you may experience performance problems when handling many logs.
10
+ # This plugin designed to solve these problems.
11
+ #
12
+ # See below an example of how this filter might be used.
13
+ # [source,ruby]
14
+ # --------------------------------------------------
15
+ # filter {
16
+ # # Find `blacklisted_at` value for particular `host` in cache.
17
+ # lrucache {
18
+ # namespace => "blacklisted_at"
19
+ # action => "get"
20
+ # key => "%{host}"
21
+ # target => "blacklisted_at"
22
+ # }
23
+ #
24
+ # if ![blacklisted_at] {
25
+ # # If no value is found in cache, Query to elasticsearch.
26
+ # elasticsearch {
27
+ # query => "host:%{host}"
28
+ # index => "blacklist"
29
+ # fields => { "@timestamp" => "blacklisted_at" }
30
+ # }
31
+ #
32
+ # if [blacklisted_at] {
33
+ # # Store query result into cache.
34
+ # lrucache {
35
+ # namespace => "blacklisted_at"
36
+ # action => "set"
37
+ # key => "%{host}"
38
+ # value => "%{blacklisted_at}"
39
+ # }
40
+ # }
41
+ # }
42
+ #
43
+ # if [blacklisted_at] {
44
+ # # Drop incoming log from blacklisted host.
45
+ # drop {}
46
+ # }
47
+ # }
48
+ # --------------------------------------------------
49
+ #
50
+ # NOTE: If the filter to use already has a cache function, consider it first.
51
+ #
52
+ class LogStash::Filters::Lrucache < LogStash::Filters::Base
53
+
54
+ config_name "lrucache"
55
+
56
+ # Each namespace has own cache storage.
57
+ config :namespace, :validate => :string, :default => "default"
58
+
59
+ # The action to perform.
60
+ config :action, :validate => [ "get", "set", "delete", "clear" ], :required => true
61
+
62
+ # The key to access cache. It can be dynamic and include parts of the event using the %{field}. Not used in `clear` action.
63
+ config :key, :validate => :string
64
+
65
+ # Store the value found in cache into the given target field. Field is overwritten if exists. Used only in `get` action.
66
+ config :target, :validate => :string, :default => "cached_value"
67
+
68
+ # Default value used when no value is found in cache. Used only in `get` action.
69
+ config :default_value, :validate => :string
70
+
71
+ # The value to set. It can be dynamic and include parts of the event using the %{field}. Used only in `set` action.
72
+ config :value, :validate => :string
73
+
74
+ # Maximum size for cache storage. Used once per namespace in `set` action.
75
+ config :max_size, :validate => :number, :default => 1000
76
+
77
+ # The TTL(Time To Live) of value stored in cache storage. Used once per namespace in `set` action.
78
+ config :ttl, :validate => :number, :default => 5.0
79
+
80
+ @@cache_storage = {}
81
+
82
+ public
83
+ def register
84
+ # Do nothing
85
+ end # def register
86
+
87
+ public
88
+ def filter(event)
89
+ cache = lookupCacheStorage(@namespace)
90
+ formattedKey = event.sprintf(@key);
91
+
92
+ case @action
93
+ when "get"
94
+ value = (cache[formattedKey] if cache) || @default_value
95
+ event.set(@target, value)
96
+ when "set"
97
+ value = event.sprintf(@value)
98
+ cache[formattedKey] = value
99
+ when "delete"
100
+ cache.delete(formattedKey) if cache
101
+ when "clear"
102
+ cache.clear() if cache
103
+ end
104
+
105
+ @logger.debug("Do `#{@action}`.", :namespace => @namespace, :key => formattedKey, :value => value) if @logger.debug?
106
+
107
+ # filter_matched should go in the last line of our successful code
108
+ filter_matched(event)
109
+ end # def filter
110
+
111
+ def lookupCacheStorage(namespace)
112
+ if !@@cache_storage.has_key?(namespace) && @action == "set"
113
+ @@cache_storage[namespace] = ::LruRedux::TTL::ThreadSafeCache.new(@max_size, @ttl)
114
+ end
115
+ @@cache_storage[namespace]
116
+ end
117
+ end # class LogStash::Filters::Lrucache
@@ -0,0 +1,25 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-filter-lrucache'
3
+ s.version = '1.0.0'
4
+ s.licenses = ['Apache-2.0']
5
+ s.summary = 'You can use this filter to cache values using LRU algorithm.'
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-lrucache'
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-sleep'
25
+ end
@@ -0,0 +1,254 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
3
+ require "logstash/filters/lrucache"
4
+ require "logstash/filters/sleep"
5
+
6
+ describe LogStash::Filters::Lrucache do
7
+ describe "basic get-set" do
8
+ let(:config) do <<-CONFIG
9
+ filter {
10
+ # get non-cache value
11
+ lrucache {
12
+ namespace => "basic_get-set"
13
+ action => "get"
14
+ key => "%{key}"
15
+ target => "empty_result"
16
+ }
17
+
18
+ # get default value
19
+ lrucache {
20
+ namespace => "basic_get-set"
21
+ action => "get"
22
+ key => "%{key}"
23
+ target => "default_value"
24
+ default_value => "NotFound"
25
+ }
26
+
27
+ # set value into cache
28
+ lrucache {
29
+ namespace => "basic_get-set"
30
+ action => "set"
31
+ key => "%{key}"
32
+ value => "%{value}"
33
+ }
34
+
35
+ # get cached value
36
+ lrucache {
37
+ namespace => "basic_get-set"
38
+ action => "get"
39
+ key => "%{key}"
40
+ target => "successful_result"
41
+ }
42
+ }
43
+ CONFIG
44
+ end
45
+
46
+ sample("key" => "cache_key", "value" => "cached_result") do
47
+ expect(subject.get("empty_result").nil?)
48
+ expect(subject.get("default_value")).to eq("NotFound")
49
+ expect(subject.get("successful_result")).to eq("cached_result")
50
+ end
51
+ end
52
+
53
+ describe "delete, clear" do
54
+ let(:config) do <<-CONFIG
55
+ filter {
56
+ # Initialize cache
57
+ lrucache {
58
+ namespace => "delete_clear"
59
+ action => "set"
60
+ key => "a"
61
+ value => "a"
62
+ }
63
+
64
+ lrucache {
65
+ namespace => "delete_clear"
66
+ action => "set"
67
+ key => "b"
68
+ value => "b"
69
+ }
70
+
71
+ # Delete one
72
+ lrucache {
73
+ namespace => "delete_clear"
74
+ action => "delete"
75
+ key => "a"
76
+ }
77
+
78
+ # Check values
79
+ lrucache {
80
+ namespace => "delete_clear"
81
+ action => "get"
82
+ key => "a"
83
+ target => "deleted_a"
84
+ }
85
+
86
+ lrucache {
87
+ namespace => "delete_clear"
88
+ action => "get"
89
+ key => "b"
90
+ target => "not_deleted_b"
91
+ }
92
+
93
+ # Clear all
94
+ lrucache {
95
+ namespace => "delete_clear"
96
+ action => "clear"
97
+ }
98
+
99
+ # Check values
100
+ lrucache {
101
+ namespace => "delete_clear"
102
+ action => "get"
103
+ key => "b"
104
+ target => "cleared_b"
105
+ }
106
+ }
107
+ CONFIG
108
+ end
109
+
110
+ sample("message" => "dummy") do
111
+ expect(subject.get("deleted_a").nil?)
112
+ expect(subject.get("not_deleted_b")).to eq("b")
113
+ expect(subject.get("cleared_b").nil?)
114
+ end
115
+ end
116
+
117
+ describe "get-set with different namespaces" do
118
+ let(:config) do <<-CONFIG
119
+ filter {
120
+ # init values into different namespaces
121
+ lrucache {
122
+ namespace => "a"
123
+ action => "set"
124
+ key => "a"
125
+ value => "a"
126
+ }
127
+
128
+ lrucache {
129
+ namespace => "b"
130
+ action => "set"
131
+ key => "b"
132
+ value => "b"
133
+ }
134
+
135
+ # get values
136
+ lrucache {
137
+ namespace => "a"
138
+ action => "get"
139
+ key => "a"
140
+ target => "[a][a]"
141
+ }
142
+
143
+ lrucache {
144
+ namespace => "a"
145
+ action => "get"
146
+ key => "b"
147
+ target => "[a][b]"
148
+ }
149
+
150
+ lrucache {
151
+ namespace => "b"
152
+ action => "get"
153
+ key => "b"
154
+ target => "[b][b]"
155
+ }
156
+ }
157
+ CONFIG
158
+ end
159
+
160
+ sample("message" => "dummy") do
161
+ expect(subject.get("[a][a]")).to eq("a")
162
+ expect(subject.get("[a][b]").nil?)
163
+ expect(subject.get("[b][b]")).to eq("b")
164
+ end
165
+ end
166
+
167
+ describe "with ttl option" do
168
+ let(:config) do <<-CONFIG
169
+ filter {
170
+ lrucache {
171
+ namespace => "ttl_1s"
172
+ action => "set"
173
+ key => "a"
174
+ value => "a"
175
+ ttl => 1
176
+ }
177
+
178
+ lrucache {
179
+ namespace => "ttl_5s"
180
+ action => "set"
181
+ key => "a"
182
+ value => "a"
183
+ ttl => 5
184
+ }
185
+
186
+ sleep {
187
+ time => "2"
188
+ }
189
+
190
+ lrucache {
191
+ namespace => "ttl_1s"
192
+ action => "get"
193
+ key => "a"
194
+ target => "expired_a"
195
+ }
196
+
197
+ lrucache {
198
+ namespace => "ttl_5s"
199
+ action => "get"
200
+ key => "a"
201
+ target => "alive_a"
202
+ }
203
+ }
204
+ CONFIG
205
+ end
206
+
207
+ sample("message" => "dummy") do
208
+ expect(subject.get("expired_a").nil?)
209
+ expect(subject.get("alive_a")).to eq("a")
210
+ end
211
+ end
212
+
213
+ describe "with max_size option" do
214
+ let(:config) do <<-CONFIG
215
+ filter {
216
+ lrucache {
217
+ namespace => "size_limited"
218
+ action => "set"
219
+ max_size => 1
220
+ key => "a"
221
+ value => "a"
222
+ }
223
+
224
+ lrucache {
225
+ namespace => "size_limited"
226
+ action => "set"
227
+ max_size => 1
228
+ key => "b"
229
+ value => "b"
230
+ }
231
+
232
+ lrucache {
233
+ namespace => "size_limited"
234
+ action => "get"
235
+ key => "a"
236
+ target => "flushed_a"
237
+ }
238
+
239
+ lrucache {
240
+ namespace => "size_limited"
241
+ action => "get"
242
+ key => "b"
243
+ target => "remained_b"
244
+ }
245
+ }
246
+ CONFIG
247
+ end
248
+
249
+ sample("message" => "dummy") do
250
+ expect(subject.get("flushed_a").nil?)
251
+ expect(subject.get("remained_b")).to eq("b")
252
+ end
253
+ end
254
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-lrucache
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-25 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-sleep
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
+ description: This gem is a Logstash plugin required to be installed on top of the
70
+ Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This
71
+ gem is not a stand-alone program
72
+ email: kjss10@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - CHANGELOG.md
78
+ - CONTRIBUTORS
79
+ - DEVELOPER.md
80
+ - Gemfile
81
+ - LICENSE
82
+ - README.md
83
+ - lib/logstash/filters/lrucache.rb
84
+ - logstash-filter-lrucache.gemspec
85
+ - spec/filters/lrucache_spec.rb
86
+ homepage: https://github.com/sw-jung/logstash-filter-lrucache
87
+ licenses:
88
+ - Apache-2.0
89
+ metadata:
90
+ logstash_plugin: 'true'
91
+ logstash_group: filter
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.6.13
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: You can use this filter to cache values using LRU algorithm.
112
+ test_files:
113
+ - spec/filters/lrucache_spec.rb