logstash-filter-redis 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 77dfa528ca95df82aad46d238a1dcf466ed9475e
4
- data.tar.gz: ce51f3975f89e9f08b075f46a63787cde84015d8
3
+ metadata.gz: 380ebbff9adc91207fe0fad3aa9818e11378cc45
4
+ data.tar.gz: 6377159bf5e178ba6e68d91a4a02c062581ca170
5
5
  SHA512:
6
- metadata.gz: ad2a1e2cd6cfa0bfd2ded1cd5e56308249cf7f0c71696165732f1e3835eaea6dfa9c36794bdcf552e5649626a7eba0e34f9c6820abf28ba903f76eb6e8e1432f
7
- data.tar.gz: 969d23e7084ca18c701f79ccca26c2e15cc2b6fabfe3d6faa341451b17b42abb851f30695332b22e587ad6ccccf17d93e22ffaa81726ed5b1dfa16b4431971e0
6
+ metadata.gz: 8d934621faec917dfc1f2ab4e21a472e01f94b6e95ad5ca23a81cd073790764878abe6581dbcf16cab1afd72c50ae304b9b534ea7820c6cd96a4dd8d8626eeaf
7
+ data.tar.gz: 062615bbd46143c35b5c786a8cb48e166d6c85dd7adad450c18342fd6399e9cf0eb9c2716bc5a391a9fe4c36c10881878d6cb0f4c2f4310326e0e8dd53cb5db8
@@ -0,0 +1,7 @@
1
+ # 0.2.0
2
+ - Removed data store feature
3
+ - Renamed configuration option "key" into "field" to match configuration of translate plugin
4
+ - Changed main functionality and configuration similar to translate filter (logstash-plugins/logstash-filter-translate)
5
+ - Added field, destination and override configuration options and their handling from logstash-plugins/logstash-filter-translate/blob/master/lib/logstash/filters/translate.rb
6
+ # 0.1.0
7
+ - forked from meulop/logstash-filter-redis
@@ -4,6 +4,7 @@ reports, or in general have helped logstash along its way.
4
4
  Contributors:
5
5
  * Aaron Mildenstein (untergeek)
6
6
  * Pier-Hugues Pellerin (ph)
7
+ * Markus Paaso (make)
7
8
 
8
9
  Note: If you've sent us patches, bug reports, or otherwise contributed to
9
10
  Logstash, and you aren't on the list above and want to be, please let us know
data/README.md CHANGED
@@ -1,17 +1,15 @@
1
- # TODO: remove/alter Elasticsearch example text from this file and the licence/copyright/etc...
2
-
3
1
  # Logstash Plugin
4
2
 
5
- This is a plugin for [Logstash](https://github.com/elasticsearch/logstash).
3
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
6
4
 
7
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.
8
6
 
9
7
  ## Documentation
10
8
 
11
- 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.elasticsearch.org/guide/en/logstash/current/).
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/).
12
10
 
13
11
  - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
14
- - For more asciidoc formatting tips, see the excellent reference here https://github.com/elasticsearch/docs#asciidoc-guide
12
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
15
13
 
16
14
  ## Need Help?
17
15
 
@@ -55,7 +53,12 @@ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
55
53
  ```
56
54
  - Install plugin
57
55
  ```sh
56
+ # Logstash 2.3 and higher
57
+ bin/logstash-plugin install --no-verify
58
+
59
+ # Prior to Logstash 2.3
58
60
  bin/plugin install --no-verify
61
+
59
62
  ```
60
63
  - Run Logstash with your plugin
61
64
  ```sh
@@ -73,7 +76,12 @@ gem build logstash-filter-awesome.gemspec
73
76
  ```
74
77
  - Install the plugin from the Logstash home
75
78
  ```sh
76
- bin/plugin install /your/local/plugin/logstash-filter-awesome.gem
79
+ # Logstash 2.3 and higher
80
+ bin/logstash-plugin install --no-verify
81
+
82
+ # Prior to Logstash 2.3
83
+ bin/plugin install --no-verify
84
+
77
85
  ```
78
86
  - Start Logstash and proceed to test the plugin
79
87
 
@@ -85,4 +93,4 @@ Programming is not a required skill. Whatever you've seen about open source and
85
93
 
86
94
  It is more important to the community that you are able to contribute.
87
95
 
88
- For more information about contributing, see the [CONTRIBUTING](https://github.com/elasticsearch/logstash/blob/master/CONTRIBUTING.md) file.
96
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
@@ -2,6 +2,21 @@
2
2
  require "logstash/filters/base"
3
3
  require "logstash/namespace"
4
4
 
5
+ # A general search and replace tool which queries replacement values from a redis instance.
6
+ #
7
+ # This is actually a redis version of a translate plugin. <https://www.elastic.co/guide/en/logstash/current/plugins-filters-translate.html>
8
+ #
9
+ # Operationally, if the event field specified in the "field" configuration
10
+ # matches the EXACT contents of a redis key, the field's value will be substituted
11
+ # with the matched key's value from the redis GET <key> command.
12
+ #
13
+ # By default, the redis filter will replace the contents of the
14
+ # matching event field (in-place). However, by using the "destination"
15
+ # configuration item, you may also specify a target event field to
16
+ # populate with the new translated value.
17
+ #
18
+ # Alternatively, for simple string search and replacements for just a few values
19
+ # you might consider using the gsub function of the mutate filter.
5
20
 
6
21
  class LogStash::Filters::Redis < LogStash::Filters::Base
7
22
 
@@ -19,80 +34,63 @@ class LogStash::Filters::Redis < LogStash::Filters::Base
19
34
  # The Redis database number.
20
35
  config :db, :validate => :number, :default => 0
21
36
 
22
- # Tag to store key
23
- config :store_tag, :validate => :string, :required => true
24
-
25
- # Tag to retrieve key
26
- config :retrieve_tag, :validate => :string, :required => true
27
-
28
- # Redis key name
29
- config :key, :validate => :string, :required => true
30
-
31
- # Delete on retrieval
32
- config :delete, :validate => :boolean, :default => false
33
-
34
- # Fields to store in the Redis value
35
- config :fields, :validate => :array, :default => ["message"]
36
-
37
- # Field prefix
38
- config :prefix, :validate => :string, :default => "old_"
37
+ # The name of the logstash event field containing the value to be compared for a
38
+ # match by the translate filter (e.g. "message", "host", "response_code").
39
+ #
40
+ # If this field is an array, only the first value will be used.
41
+ config :field, :validate => :string, :required => true
42
+
43
+ # If the destination (or target) field already exists, this configuration item specifies
44
+ # whether the filter should skip translation (default) or overwrite the target field
45
+ # value with the new translation value.
46
+ config :override, :validate => :boolean, :default => false
47
+
48
+ # The destination field you wish to populate with the translated code. The default
49
+ # is a field named "redis". Set this to the same value as source if you want
50
+ # to do a substitution, in this case filter will allways succeed. This will clobber
51
+ # the old value of the source field!
52
+ config :destination, :validate => :string, :default => "redis"
53
+
54
+ # In case no translation occurs in the event (no matches), this will add a default
55
+ # translation string, which will always populate "field", if the match failed.
56
+ #
57
+ # For example, if we have configured `fallback => "no match"`, using this dictionary:
58
+ #
59
+ # foo: bar
60
+ #
61
+ # Then, if logstash received an event with the field `foo` set to "bar", the destination
62
+ # field would be set to "bar". However, if logstash received an event with `foo` set to "nope",
63
+ # then the destination field would still be populated, but with the value of "no match".
64
+ config :fallback, :validate => :string
39
65
 
40
66
  # Connection timeout
41
67
  config :timeout, :validate => :number, :required => false, :default => 5
42
68
 
43
- # Key expiry time
44
- config :expiry, :validate => :number, :default => 1800
45
-
46
69
  public
47
70
  def register
48
71
  require 'redis'
49
72
  require 'json'
50
73
  @redis = nil
51
- @redis_url = "redis://#{@password}@#{@host}:#{@port}/#{@db}"
52
74
  end # def register
53
75
 
54
76
  public
55
77
  def filter(event)
56
- return unless filter?(event)
57
-
58
- # Allow dynamic key names using fields etc
59
- key = event.sprintf(@key)
60
-
61
- relevant = [@store_tag, @retrieve_tag].select do |t|
62
- event["tags"].include?(t)
63
- end
64
-
65
- @logger.debug(relevant)
66
- return unless [] != relevant
67
-
68
- # Do we retrieve data from a prior event?
69
- # N.b. we retrieve before we store so that we can do both if we want!
70
- # (e.g. packet sequence numbers? ...)
71
- #
72
- if event["tags"].include?(@retrieve_tag)
73
- @logger.debug("Found retrieve tag %{retrieve_tag}")
74
- @redis ||= connect
75
- val = @redis.get(key)
76
- if val != nil
77
- @logger.debug("Found key in Redis")
78
- JSON.parse(val).each do |k,v|
79
- event[prefix + k] = v
80
- end
81
- if @delete
82
- @redis.del(key) && @logger.debug("Deleted key")
83
- end
84
- else
85
- @logger.debug("Key not found in Redis")
78
+ return unless event.include?(@field)
79
+ return if event.include?(@destination) and not @override
80
+
81
+ source = event[@field].is_a?(Array) ? event[@field].first.to_s : event[@field].to_s
82
+ @redis ||= connect
83
+ val = @redis.get(source)
84
+ if val
85
+ begin
86
+ event[@destination] = JSON.parse(val)
87
+ rescue JSON::ParserError => e
88
+ event[@destination] = val
86
89
  end
90
+ elsif @fallback
91
+ event[@destination] = @fallback
87
92
  end
88
- # Do we store data?
89
- if event["tags"].include?(@store_tag)
90
- @logger.debug("Found store tag %{store_tag}")
91
- val = event.to_hash().select { |name,value| fields.include?(name) }
92
- @redis ||= connect
93
- @redis.set(key, val.to_json) && @logger.debug("Stored key")
94
- @redis.expire(key, @expiry) && @logger.debug("Set expiry key")
95
- end
93
+
96
94
  # filter_matched should go in the last line of our successful code
97
95
  filter_matched(event)
98
96
  end # def filter
@@ -103,7 +101,7 @@ class LogStash::Filters::Redis < LogStash::Filters::Base
103
101
  :host => @host,
104
102
  :port => @port,
105
103
  :timeout => @timeout,
106
- :db => db,
104
+ :db => @db,
107
105
  :password => @password.nil? ? nil : @password.value
108
106
  )
109
107
  end #def connect
@@ -1,24 +1,28 @@
1
1
  Gem::Specification.new do |s|
2
+
2
3
  s.name = 'logstash-filter-redis'
3
- s.version = '0.1.2'
4
+ s.version = '0.2.0'
4
5
  s.licenses = ['Apache License (2.0)']
5
6
  s.summary = "This filter allows the storage of event fields in a redis key to be retrieved by a later event"
6
- s.description = "This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program"
7
+ 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
8
  s.authors = ["meulop","make"]
8
- s.email = '[info@meulop.uk]'
9
- s.homepage = "https://github.com/make/lostash-redis-filter"
9
+ s.email = 'markus.paaso@gmail.com'
10
+ s.homepage = "https://github.com/make/logstash-redis-filter"
10
11
  s.require_paths = ["lib"]
11
12
 
12
13
  # Files
13
14
  s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
14
- # Tests
15
+
16
+ # Tests
15
17
  s.test_files = s.files.grep(%r{^(test|spec|features)/})
16
18
 
17
19
  # Special flag to let us know this is actually a logstash plugin
18
20
  s.metadata = { "logstash_plugin" => "true", "logstash_group" => "filter" }
19
21
 
20
22
  # Gem dependencies
21
- s.add_runtime_dependency "logstash-core", '>= 1.4.0', '< 2.0.0'
22
- s.add_development_dependency 'logstash-devutils'
23
+ s.add_runtime_dependency "logstash-core-plugin-api", "~> 1.0"
23
24
  s.add_runtime_dependency "redis", '>= 3.0.0', '< 4.0.0'
25
+
26
+ s.add_development_dependency 'logstash-devutils'
24
27
  end
28
+
@@ -5,134 +5,58 @@ require "pp"
5
5
  require "json"
6
6
 
7
7
  describe LogStash::Filters::Redis do
8
+
8
9
  before(:all) do
9
10
  @redis = Redis.new()
11
+ @redis.set("somekey", "somevalue")
10
12
  end
11
13
 
12
- after(:each) do
13
- sleep(0.2)
14
- @redis.keys("logstash-filter-redis-test*").each do |k|
15
- @redis.del(k)
16
- end
17
- end
18
-
19
- describe "Ignores irrelevant tag" do
14
+ describe "Retrieves data from redis" do
20
15
  config <<-CONFIG
21
16
  filter {
22
17
  redis {
23
- add_tag => "STORED"
24
- store_tag => "BEGIN"
25
- retrieve_tag => "END"
26
- key => "logstash-filter-redis-test"
18
+ field => "redis-key"
19
+ destination => "redis-value"
27
20
  }
28
21
  }
29
22
  CONFIG
30
23
 
31
- sample({"message" => "Test message", "tags" => ["APACHE"]}) do
32
- insist { subject["tags"].include?("STORED") } == false
33
- insist { @redis.get("logstash-filter-redis-test") } == nil
24
+ sample({"message" => "Test message", "redis-key" => "somekey"}) do
25
+ insist { subject["redis-value"] } == "somevalue"
26
+ insist { @redis.get("somekey") } == "somevalue"
34
27
  end
35
-
36
28
  end
37
29
 
38
- describe "Stores key and values" do
30
+ describe "Retrieves data from redis when field is an array" do
39
31
  config <<-CONFIG
40
32
  filter {
41
33
  redis {
42
- add_tag => "STORED"
43
- store_tag => "BEGIN"
44
- retrieve_tag => "END"
45
- key => "logstash-filter-redis-test"
34
+ field => ["redis-key"]
35
+ destination => "redis-value"
46
36
  }
47
37
  }
48
38
  CONFIG
49
39
 
50
- sample({"message" => "Storing message", "tags" => ["BEGIN"]}) do
51
- # Did we add tag to the event?
52
- insist { subject["tags"].include?("STORED") } == true
53
- # Did the Redis value get set correctly?
54
- @stored = @redis.get("logstash-filter-redis-test")
55
- insist { JSON.parse(@stored)["message"] } == "Storing message"
56
- end
57
- end
58
-
59
- describe "Retrieves the stored data" do
60
- config <<-CONFIG
61
- filter{
62
- redis {
63
- store_tag => "BEGIN"
64
- retrieve_tag => "END"
65
- key => "logstash-filter-redis-test"
66
- }
67
- }
68
- CONFIG
69
-
70
- eventstore = {
71
- "message" => "Storing message",
72
- "tags" => ["BEGIN"]
73
- }
74
- eventretrieve = {
75
- "message" => "Retrieving message",
76
- "tags" => ["END"]
77
- }
78
-
79
- sample([eventstore, eventretrieve]) do
80
- insist{ subject[1]["old_message"] } == "Storing message"
81
- end
82
- end
83
-
84
- describe "Handles overlapping sets of events" do
85
- # Fill me in
86
-
87
- config <<-CONFIG
88
- filter {
89
- redis {
90
- store_tag => "BEGIN"
91
- retrieve_tag => "END"
92
- key => "logstash-filter-redis-test-%{message}"
93
- }
94
- }
95
- CONFIG
96
-
97
- eventsstore = []
98
- eventsretrieve = []
99
- 5.times do |i|
100
- eventsstore << {
101
- "message" => i.to_s,
102
- "tags" => ["BEGIN"]
103
- }
104
- eventsretrieve << {
105
- "message" => i.to_s,
106
- "tags" => ["END"]
107
- }
108
- end
109
- eventsretrieve.shuffle!
110
- sample(eventsstore + eventsretrieve) do
111
- insist {
112
- subject[5..9].select { |e| e["old_message"] == e["message"] }
113
- } != []
40
+ sample({"message" => "Test message", "redis-key" => "somekey"}) do
41
+ insist { subject["redis-value"] } == "somevalue"
42
+ insist { @redis.get("somekey") } == "somevalue"
114
43
  end
115
44
  end
116
45
 
117
-
118
- describe "Deletes after retrieval" do
46
+ describe "Skips non-existing key" do
119
47
  config <<-CONFIG
120
48
  filter {
121
49
  redis {
122
- store_tag => "BEGIN"
123
- retrieve_tag => "END"
124
- delete => true
125
- key => "logstash-filter-redis-test"
126
- }
50
+ field => "redis-key"
51
+ destination => "redis-value"
52
+ }
127
53
  }
128
54
  CONFIG
129
- estore = { "message" => "Store", "tags" => ["BEGIN"] }
130
- ereceive1 = { "message" => "Receive1", "tags" => ["END"] }
131
- ereceive2 = { "message" => "Receive2", "tags" => ["END"] }
132
55
 
133
- sample([estore,ereceive1,ereceive2]) do
134
- insist { subject[1]["old_message"] } == "Store"
135
- insist { subject[2]["old_message"] } == nil
56
+ sample({"message" => "Test message", "redis-key" => "notakey"}) do
57
+ insist { subject.include?("redis-value") } == false
136
58
  end
137
59
  end
60
+
61
+
138
62
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logstash-filter-redis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - meulop
@@ -9,42 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-06-03 00:00:00.000000000 Z
12
+ date: 2016-06-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: 1.4.0
20
- - - "<"
17
+ - - "~>"
21
18
  - !ruby/object:Gem::Version
22
- version: 2.0.0
23
- name: logstash-core
19
+ version: '1.0'
20
+ name: logstash-core-plugin-api
24
21
  prerelease: false
25
22
  type: :runtime
26
23
  version_requirements: !ruby/object:Gem::Requirement
27
24
  requirements:
28
- - - ">="
29
- - !ruby/object:Gem::Version
30
- version: 1.4.0
31
- - - "<"
32
- - !ruby/object:Gem::Version
33
- version: 2.0.0
34
- - !ruby/object:Gem::Dependency
35
- requirement: !ruby/object:Gem::Requirement
36
- requirements:
37
- - - ">="
38
- - !ruby/object:Gem::Version
39
- version: '0'
40
- name: logstash-devutils
41
- prerelease: false
42
- type: :development
43
- version_requirements: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
25
+ - - "~>"
46
26
  - !ruby/object:Gem::Version
47
- version: '0'
27
+ version: '1.0'
48
28
  - !ruby/object:Gem::Dependency
49
29
  requirement: !ruby/object:Gem::Requirement
50
30
  requirements:
@@ -65,15 +45,28 @@ dependencies:
65
45
  - - "<"
66
46
  - !ruby/object:Gem::Version
67
47
  version: 4.0.0
68
- description: This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program
69
- email: "[info@meulop.uk]"
48
+ - !ruby/object:Gem::Dependency
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ name: logstash-devutils
55
+ prerelease: false
56
+ type: :development
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ 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
63
+ email: markus.paaso@gmail.com
70
64
  executables: []
71
65
  extensions: []
72
66
  extra_rdoc_files: []
73
67
  files:
74
68
  - CHANGELOG.md
75
69
  - CONTRIBUTORS
76
- - DEVELOPER.md
77
70
  - Gemfile
78
71
  - LICENSE
79
72
  - NOTICE.TXT
@@ -82,7 +75,7 @@ files:
82
75
  - logstash-filter-redis.gemspec
83
76
  - spec/filters/redis_spec.rb
84
77
  - spec/spec_helper.rb
85
- homepage: https://github.com/make/lostash-redis-filter
78
+ homepage: https://github.com/make/logstash-redis-filter
86
79
  licenses:
87
80
  - Apache License (2.0)
88
81
  metadata:
@@ -1,2 +0,0 @@
1
- # logstash-filter-example
2
- Example filter plugin. This should help bootstrap your effort to write your own filter plugin!