logstash-filter-memcached 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
+ SHA256:
3
+ metadata.gz: 9a5b909f6459b8a3d4ae405b3d9e076ed4c1bb7eab344dbc5f4a8a98c55268d0
4
+ data.tar.gz: 4dbf8017bcf03a1f6462da74df69b1ca8485b57df2c585f772cf38a158858c94
5
+ SHA512:
6
+ metadata.gz: bda94d2ee65f231e5a40bc8ffa4b32e8c04126694e74f66b1aee3e8a2e33cc56ca96707fb5cf2e1f29c8fd48e6a51d02e98ed70a2bcd5286cc9e2c7f2c1bc6a7
7
+ data.tar.gz: d094cebcab25b085d0f8aca5d6611fb7654e7d9f5ee56f7c64250998a427742b39c1ee35afdffdc0b6c05341b55d81329445423369096a68cf92f737fc62e07c
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
+ * Ry Biesemeyer - ry.biesemeyer@elastic.co
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-memcached
2
+ Example filter plugin. This should help bootstrap your effort to write your own filter plugin!
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ logstash_path = ENV["LOGSTASH_PATH"] || "../../logstash"
6
+ use_logstash_source = ENV["LOGSTASH_SOURCE"] && ENV["LOGSTASH_SOURCE"].to_s == "1"
7
+
8
+ if Dir.exist?(logstash_path) && use_logstash_source
9
+ gem 'logstash-core', :path => "#{logstash_path}/logstash-core"
10
+ gem 'logstash-core-plugin-api', :path => "#{logstash_path}/logstash-core-plugin-api"
11
+ 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,95 @@
1
+ # Logstash Plugin
2
+
3
+ [![Travis Build Status](https://travis-ci.org/logstash-plugins/logstash-filter-memcached.svg)](https://travis-ci.org/logstash-plugins/logstash-filter-memcached)
4
+
5
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
6
+
7
+ 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
+
9
+ ## CAVEAT: 0.x
10
+
11
+ This `logstash-filter-memcached` repository is a pre-release, 0.x, skunkwork proof-of-concept. It works, but as a 0.x release we can make no guaranees that upgrades to future versions will be trivial. The API may change within the 0.x series as we work to find the API that feels "right" for a full-featured 1.x release.
12
+
13
+ The Logstash team is in the process of bringing this project (or equivalent functionality) into the [logstash-plugins fold](https://github.com/elastic/logstash/issues/8930), but the exact shape of the resulting API is still being debated.
14
+
15
+ ## Documentation
16
+
17
+ Logstash provides infrastructure to automatically generate documentation for this plugin from its [index summary](./docs/index.asciidoc). 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/).
18
+
19
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
20
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
21
+
22
+ ## TODO:
23
+
24
+ - Automated tests that don't rely on memcached magically pre-existing on localhost:11211
25
+ - Better spec coverage
26
+
27
+ ## Developing
28
+
29
+ ### 1. Plugin Developement and Testing
30
+
31
+ #### Code
32
+ - To get started, you'll need JRuby with the Bundler gem installed.
33
+
34
+ - 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).
35
+
36
+ - Install dependencies
37
+ ```sh
38
+ bundle install
39
+ ```
40
+
41
+ #### Test
42
+
43
+ - Update your dependencies
44
+
45
+ ```sh
46
+ bundle install
47
+ ```
48
+
49
+ - Run tests
50
+
51
+ ```sh
52
+ bundle exec rspec
53
+ ```
54
+
55
+ ### 2. Running your unpublished Plugin in Logstash
56
+
57
+ #### 2.1 Run in a local Logstash clone
58
+
59
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
60
+ ```ruby
61
+ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
62
+ ```
63
+ - Install plugin
64
+ ```sh
65
+ bin/logstash-plugin install --no-verify
66
+ ```
67
+ - Run Logstash with your plugin
68
+ ```sh
69
+ bin/logstash -e 'filter {awesome {}}'
70
+ ```
71
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
72
+
73
+ #### 2.2 Run in an installed Logstash
74
+
75
+ 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:
76
+
77
+ - Build your plugin gem
78
+ ```sh
79
+ gem build logstash-filter-awesome.gemspec
80
+ ```
81
+ - Install the plugin from the Logstash home
82
+ ```sh
83
+ bin/logstash-plugin install /your/local/plugin/logstash-filter-awesome.gem
84
+ ```
85
+ - Start Logstash and proceed to test the plugin
86
+
87
+ ## Contributing
88
+
89
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
90
+
91
+ 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.
92
+
93
+ It is more important to the community that you are able to contribute.
94
+
95
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1,173 @@
1
+ # encoding: utf-8
2
+ require "logstash/filters/base"
3
+ require "logstash/namespace"
4
+
5
+ # This filter provides facilities to interact with Memcached.
6
+ class LogStash::Filters::Memcached < LogStash::Filters::Base
7
+
8
+ # This is how you configure this filter from your Logstash config.
9
+ #
10
+ # Given:
11
+ # - event with field `hostname` with value `example.com`
12
+ # - memcached with entry for `threats/hostname/example.com`
13
+ #
14
+ # The following config will inject the value from memcached into
15
+ # the nested field `[threats][host]`:
16
+ #
17
+ # filter {
18
+ # memcached {
19
+ # hosts => ["localhost:11211"]
20
+ # namespace => "threats"
21
+ # get => {
22
+ # "hostname:%{[hostname]}" => "[threats][host]"
23
+ # }
24
+ # }
25
+ # }
26
+ #
27
+ config_name "memcached"
28
+
29
+ # an array of memcached hosts to connect to
30
+ # valid forms:
31
+ # ipv4:
32
+ # - 127.0.0.1
33
+ # - 127.0.0.1:11211
34
+ # ipv6:
35
+ # - ::1
36
+ # - [::1]:11211
37
+ # fqdn:
38
+ # - your.fqdn.com
39
+ # - your.fqdn.com:11211
40
+ config :hosts, :validate => :array, :default => ["localhost"]
41
+
42
+ # if specified and non-empty, all keys will be prepended with this string and a colon (`:`)
43
+ config :namespace, :validate => :string, :required => false
44
+
45
+ # GET data from the given memcached keys to inject into the corresponding event fields.
46
+ # - memcached keys can reference event fields via sprintf
47
+ # - event fields can be deep references
48
+ #
49
+ # get {
50
+ # "memcached-key1" => "[path][to][field1]"
51
+ # "memcached-key2" => "[path][to][field2]"
52
+ # }
53
+ config :get, :validate => :hash, :required => false
54
+
55
+ # SET the given fields from the event to the corresponding keys in memcached
56
+ # - memcached keys can reference event fields via sprintf
57
+ # - event fields can be deep references
58
+ #
59
+ # set {
60
+ # "[path][to][field1]" => "memcached-key1"
61
+ # "[path][to][field2]" => "memcached-key2"
62
+ # }
63
+ config :set, :validate => :hash, :required => false
64
+
65
+
66
+ # if performing a setting operation to memcached, the time-to-live in seconds.
67
+ # NOTE: in Memcached, a value of 0 (default) means "never expire"
68
+ config :ttl, :validate => :number, :default => 0
69
+
70
+ public
71
+
72
+ attr_reader :cache
73
+
74
+ def register
75
+ if @ttl < 0
76
+ logger.error("ttl cannot be negative")
77
+ fail("invalid ttl: cannot be negative")
78
+ end
79
+
80
+ @cache = establish_connection
81
+ end # def register
82
+
83
+ def filter(event)
84
+ do_set(event)
85
+ do_get(event)
86
+
87
+ filter_matched(event)
88
+ end # def filter
89
+
90
+ def close
91
+ cache.close
92
+ end
93
+
94
+ private
95
+
96
+ def do_get(event)
97
+ return unless @get && !@get.empty?
98
+
99
+ event_fields_by_memcached_key = @get.each_with_object({}) do |(memcached_key_template, event_field), memo|
100
+ memcached_key = event.sprintf(memcached_key_template)
101
+ memo[memcached_key] = event_field
102
+ end
103
+
104
+ memcached_keys = event_fields_by_memcached_key.keys
105
+ cache_hits_by_memcached_key = cache.get_multi(memcached_keys)
106
+
107
+ event_fields_by_memcached_key.each do |memcached_key, event_field|
108
+ value = cache_hits_by_memcached_key[memcached_key]
109
+ if value.nil?
110
+ logger.trace("cache:get miss", context(key: memcached_key))
111
+ else
112
+ logger.trace("cache:get hit", context(key: memcached_key, value: value))
113
+ event.set(event_field, value)
114
+ end
115
+ end
116
+ end
117
+
118
+ def do_set(event)
119
+ return unless @set && !@set.empty?
120
+
121
+ values_by_memcached_key = @set.each_with_object({}) do |(event_field, memcached_key_template), memo|
122
+ memcached_key = event.sprintf(memcached_key_template)
123
+ value = event.get(event_field)
124
+
125
+ memo[memcached_key] = value unless value.nil?
126
+ end
127
+
128
+ cache.multi do
129
+ values_by_memcached_key.each do |memcached_key, value|
130
+ logger.trace("cache:set", context(key: memcached_key, value: value))
131
+ cache.set(memcached_key, value)
132
+ end
133
+ end
134
+ end
135
+
136
+ def establish_connection
137
+ require 'dalli'
138
+
139
+ hosts = validate_connection_hosts
140
+ options = validate_connection_options
141
+ logger.debug('connecting to memcached', context(hosts: hosts, options: options))
142
+ Dalli::Client.new(@hosts, options).tap do |client|
143
+ begin
144
+ client.alive!
145
+ rescue Dalli::RingError
146
+ logger.error("failed to connect", context(hosts: hosts, options: options))
147
+ fail("cannot connect to memcached")
148
+ end
149
+ end
150
+ end
151
+
152
+ def validate_connection_options
153
+ {}.tap do |options|
154
+ options[:ttl] = @ttl
155
+ options[:namespace] = @namespace unless @namespace.nil? || @namespace.empty?
156
+ end
157
+ end
158
+
159
+ def validate_connection_hosts
160
+ logger.error("configuration: hosts empty!") && fail if @hosts.empty?
161
+
162
+ @hosts.map(&:to_s)
163
+ end
164
+
165
+ def context(hash={})
166
+ @plugin_context ||= Hash.new.tap do |hash|
167
+ hash[:namespace] = @namespace unless @namespace.nil? or @namespace.empty?
168
+ end
169
+ return hash if @plugin_context.empty?
170
+
171
+ @plugin_context.merge(hash)
172
+ end
173
+ end # class LogStash::Filters::Memcached
@@ -0,0 +1,23 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-filter-memcached'
3
+ s.version = '0.1.0'
4
+ s.licenses = ['Apache-2.0']
5
+ s.summary = 'A Logstash filter plugin for interacting with memcached'
6
+ s.homepage = 'https://github.com/yaauie/logstash-filter-memcached'
7
+ s.authors = ['Ry Biesemeyer']
8
+ s.email = 'ry.biesemeyer@elastic.co'
9
+ s.require_paths = ['lib']
10
+
11
+ # Files
12
+ s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
13
+ # Tests
14
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
15
+
16
+ # Special flag to let us know this is actually a logstash plugin
17
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "filter" }
18
+
19
+ # Gem dependencies
20
+ s.add_runtime_dependency "logstash-core-plugin-api", "~> 2.0"
21
+ s.add_runtime_dependency "dalli", "~> 2.7"
22
+ s.add_development_dependency 'logstash-devutils'
23
+ end
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+ require_relative '../spec_helper'
3
+ require "logstash/filters/memcached"
4
+
5
+ LogStash::Logging::Logger::configure_logging("TRACE")
6
+
7
+ describe LogStash::Filters::Memcached do
8
+ subject { described_class.new(config) }
9
+ let(:cache) { double('memcached') }
10
+ before(:each) do
11
+ allow(subject).to receive(:establish_connection).and_return(cache)
12
+ allow(subject).to receive(:close)
13
+ subject.register
14
+ end
15
+
16
+ after(:each) do
17
+ subject.close
18
+ end
19
+
20
+ describe "#get" do
21
+ let(:event) { ::LogStash::Event.new(data) }
22
+ let(:config) do
23
+ {
24
+ "hosts" => ["localhost:11211"],
25
+ "get" => { "success/%{success}/answer" => "[ultimate][answer]" }
26
+ }
27
+ end
28
+ let(:data) { { "success" => "true" } }
29
+
30
+ it "retrieves data from memcache" do
31
+ expect(cache).to receive(:get_multi).with(["success/true/answer"]).and_return({"success/true/answer" => "42"})
32
+ subject.filter(event)
33
+ expect(event.get("ultimate")).to eq("answer" => "42")
34
+ end
35
+ end
36
+
37
+ describe "#set" do
38
+ let(:event) { ::LogStash::Event.new(data) }
39
+ let(:config) do
40
+ {
41
+ "hosts" => ["localhost:11211"],
42
+ "set" => { "[answer]" => "success/%{success}/answer" },
43
+ }
44
+ end
45
+ let(:data) { { "answer" => "42", "success" => "true" } }
46
+ before(:each) { allow(cache).to receive(:multi) {|&b| b.call } }
47
+
48
+ it "sets data on memcache" do
49
+ expect(cache).to receive(:set).with("success/true/answer", "42")
50
+ subject.filter(event)
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,2 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-memcached
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ry Biesemeyer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-12 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: '2.7'
33
+ name: dalli
34
+ prerelease: false
35
+ type: :runtime
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.7'
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
+ description:
56
+ email: ry.biesemeyer@elastic.co
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - CHANGELOG.md
62
+ - CONTRIBUTORS
63
+ - DEVELOPER.md
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - lib/logstash/filters/memcached.rb
68
+ - logstash-filter-memcached.gemspec
69
+ - spec/filters/memcached_spec.rb
70
+ - spec/spec_helper.rb
71
+ homepage: https://github.com/yaauie/logstash-filter-memcached
72
+ licenses:
73
+ - Apache-2.0
74
+ metadata:
75
+ logstash_plugin: 'true'
76
+ logstash_group: filter
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.6.13
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: A Logstash filter plugin for interacting with memcached
97
+ test_files:
98
+ - spec/filters/memcached_spec.rb
99
+ - spec/spec_helper.rb