logstash-input-rethinkdb 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: ee3f3933a77ac91a562be9e4a09a70663a456382
4
+ data.tar.gz: a8170147fd7d527903a6bb6cc46ca40518973bbb
5
+ SHA512:
6
+ metadata.gz: da1a6acba6a759f9533c3ae53473a80f1be1a2a4e0370ca0e69c3f420d6c06caf31c5b1b51f4ef2126e99b7b65c20b317a5ceb5208b6fdb00d4814595f58ddfd
7
+ data.tar.gz: fe318a16b281d0558bf08aa3e504ed40ef437ab9c4282d050c230397465ca129dbfce5977f99905292de3334fab2ae333c204f3dfce073b0044b035284685d0d
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ Gemfile.lock
3
+ Gemfile.bak
4
+ .bundle
5
+ vendor
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+ gem "eventmachine"
3
+ gem "rethinkdb", :path => "/home/josh/Code/rethinkdb/drivers/ruby"
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2012-2015 Elasticsearch <http://www.elasticsearch.org>
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # RethinkDB Logstash input plugin
2
+
3
+ This is a plugin for [Logstash](https://github.com/elasticsearch/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.elasticsearch.org/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/elasticsearch/docs#asciidoc-guide
13
+
14
+ ## Need Help?
15
+
16
+ Need help? Try #logstash on freenode IRC or the logstash-users@googlegroups.com mailing list.
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/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/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/elasticsearch/logstash/blob/master/CONTRIBUTING.md) file.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "logstash/devutils/rake"
@@ -0,0 +1,230 @@
1
+ # encoding: utf-8
2
+
3
+ require "logstash/inputs/base"
4
+ require "logstash/namespace"
5
+ require "eventmachine"
6
+ require "rethinkdb"
7
+
8
+ class LogStash::Inputs::RethinkDB < LogStash::Inputs::Base
9
+ config_name "rethinkdb"
10
+ default :codec, "json_lines"
11
+ attr_accessor :logger
12
+
13
+ include RethinkDB::Shortcuts
14
+
15
+ # Hostname of RethinkDB server
16
+ config :host, :validate => :string, :default => "localhost"
17
+ # Driver connection port of RethinkDB server
18
+ config :port, :validate => :number, :default => 28015
19
+ # Auth key of RethinkDB server (don't provide if nil)
20
+ config :auth_key, :validate => :string, :default => ""
21
+ # Time period to squash changefeeds on. Defaults to no squashing.
22
+ config :squash, :default => false
23
+ # Which tables to watch for changes
24
+ config :watch_tables, :validate => :array, :default => []
25
+ # Which databases to watch for changes. Tables added or removed from
26
+ # these databases will be watched or unwatched accordingly
27
+ config :watch_dbs, :validate => :array, :default => []
28
+
29
+
30
+ # Part of the logstash input interface
31
+ def register
32
+ # {db => {table => QueryHandle}}
33
+ @table_feeds = Hash.new { |hsh, key| hsh[key] = {} }
34
+ # {db => QueryHandle}
35
+ @db_feeds = {}
36
+ @queue = nil
37
+ end
38
+
39
+ # # Part of the logstash input interface
40
+ def run(queue)
41
+ @queue = queue
42
+ @conn = r.connect(
43
+ :host => @host,
44
+ :port => @port,
45
+ :auth_key => @auth_key,
46
+ )
47
+ EM.run do
48
+ @logger.log "Eventmachine loop started"
49
+ @watch_dbs.uniq.each do |db|
50
+ create_db_feed(db, DBHandler.new(db, self))
51
+ end
52
+ @watch_tables.uniq.each do |db_table|
53
+ db, table = db_table.split '.'
54
+ db, table = "test", db if table.nil?
55
+ update_db_tables(nil, {'db' => db, 'name' => table})
56
+ end
57
+ end
58
+ end
59
+
60
+ def send(db, table, old_val, new_val)
61
+ event = LogStash::Event.new(
62
+ "db" => db,
63
+ "table" => table,
64
+ "old_val" => old_val,
65
+ "new_val" => new_val
66
+ )
67
+ decorate(event)
68
+ @queue << event
69
+ end
70
+
71
+ def update_db_tables(old_val, new_val)
72
+ unless new_val.nil?
73
+ handler = TableHandler.new(new_val['db'], new_val['name'], self)
74
+ create_table_feed(new_val['db'], new_val['name'], handler)
75
+ end
76
+ unless old_val.nil?
77
+ unregister_table(old_val['db'], old_val['name'], nil)
78
+ end
79
+ end
80
+
81
+ def register_table(db, table, qhandle)
82
+ # Add a table feed to the registry
83
+ unless @table_feeds.has_key?(db) &&
84
+ @table_feeds[db].has_key?(table)
85
+ @logger.log("Watching table #{db}.#{table}")
86
+ @table_feeds[db][table] = qhandle
87
+ else
88
+ qhandle.close
89
+ end
90
+ end
91
+
92
+ def unregister_table(db, table, qhandle)
93
+ # Remove a table from the registry.
94
+ if @table_feeds.has_key?(db) &&
95
+ @table_feeds[db].has_key?(table) &&
96
+ # If a duplicate feed comes in for the same table and needs to
97
+ # be unregistered, we need to check if the handle is the same
98
+ (qhandle.nil? || @table_feeds[db][table].equal?(qhandle))
99
+ @logger.log("Unregistering table #{db}.#{table}")
100
+ @table_feeds[db].delete(table).close
101
+ end
102
+ end
103
+
104
+ def register_db(db, qhandle)
105
+ # Add a db to the registry to watch it for updates to which tables
106
+ # are listed in it.
107
+ unless @db_feeds.has_key? db
108
+ @db_feeds[db] = qhandle
109
+ @logger.log "Feed for db '#{db}' registered"
110
+ end
111
+ end
112
+
113
+ def unregister_db(db)
114
+ # Remove a db from the registry, close all of its feeds, and
115
+ # remove its entry in @db_admin_tables
116
+ if @table_feeds.has_key?(db)
117
+ @logger.log("Unregistering feed for db '#{db}'")
118
+ @table_feeds[db].keys.each do |table|
119
+ unregister_table(db, table, nil)
120
+ end
121
+ @table_feeds.delete(db)
122
+ @db_feeds.delete(db).close
123
+ end
124
+ end
125
+
126
+ def create_db_feed(db, handler)
127
+ r.db('rethinkdb').
128
+ table('table_status').
129
+ filter {|row| row['status']['all_replicas_ready']}.
130
+ pluck('db', 'name').
131
+ changes(:squash => @squash, :include_states => true).
132
+ em_run(@conn, handler)
133
+ end
134
+
135
+ def create_table_feed(db, table, handler)
136
+ r.db(db).
137
+ table(table).
138
+ changes(:squash => @squash, :include_states => true).
139
+ em_run(@conn, handler)
140
+ end
141
+
142
+ def teardown
143
+ # Goes through all existing handles and closes them, then clears
144
+ # out the registry and closes the connection to RethinkDB
145
+ @table_feeds.values.each do |tables|
146
+ tables.values.each { |qhandle| qhandle.close }
147
+ end
148
+ @db_feeds.values.each { |qhandle| qhandle.close }
149
+ @table_feeds.clear
150
+ @db_feeds.clear
151
+ @conn.close
152
+ EM.stop
153
+ @queue = nil
154
+ end
155
+
156
+ end
157
+
158
+ # This handles feeds listening for changes to documents in a table
159
+ class TableHandler < RethinkDB::Handler
160
+ attr_accessor :db
161
+ attr_accessor :table
162
+ def initialize(db, table, plugin)
163
+ super()
164
+ @db = db
165
+ @table = table
166
+ @plugin = plugin
167
+ end
168
+
169
+ def on_initial(val)
170
+ @plugin.send(@db, @table, nil, val)
171
+ end
172
+
173
+ def on_change(old_val, new_val)
174
+ @plugin.send(@db, @table, old_val, new_val)
175
+ end
176
+
177
+ def on_open(qhandle)
178
+ @plugin.register_table(@db, @table, qhandle)
179
+ end
180
+
181
+ def on_close(qhandle)
182
+ @plugin.unregister_table(@db, @table, qhandle)
183
+ end
184
+
185
+ def on_error(err, qhandle)
186
+ @plugin.logger.error(err.to_s)
187
+ @plugin.unregister_table(@db, @table, qhandle)
188
+ end
189
+
190
+ def on_change_error(err_str)
191
+ @plugin.logger.warn(err.to_s)
192
+ end
193
+ end
194
+
195
+ # Handler for changes to the tables in a database
196
+ class DBHandler < RethinkDB::Handler
197
+
198
+ attr_accessor :db
199
+
200
+ def initialize(db, plugin)
201
+ super()
202
+ @db = db
203
+ @plugin = plugin
204
+ end
205
+
206
+ def on_open(qhandle)
207
+ @plugin.register_db(@db, qhandle)
208
+ end
209
+
210
+ def on_close(qhandle)
211
+ @plugin.unregister_db(@db)
212
+ end
213
+
214
+ def on_error(err, qhandle)
215
+ @plugin.logger.error(err.to_s)
216
+ @plugin.unregister_db(@db)
217
+ end
218
+
219
+ def on_change_error(err_str)
220
+ @plugin.logger.warn(err_str)
221
+ end
222
+
223
+ def on_initial_val(val)
224
+ @plugin.update_db_tables(nil, val)
225
+ end
226
+
227
+ def on_change(old_val, new_val)
228
+ @plugin.update_db_tables(old_val, new_val)
229
+ end
230
+ end
@@ -0,0 +1,26 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-input-rethinkdb'
3
+ s.version = '0.1.0'
4
+ s.licenses = ['Apache License (2.0)']
5
+ s.summary = "Streams changes from RethinkDB tables into LogStash."
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.authors = ["RethinkDB"]
8
+ s.email = 'josh@rethinkdb.com'
9
+ s.homepage = "http://www.rethinkdb.com/docs/logstash-plugins"
10
+ s.require_paths = ["lib"]
11
+
12
+ # Files
13
+ s.files = `git ls-files`.split($\)
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" => "input" }
19
+
20
+ # Gem dependencies
21
+ s.add_runtime_dependency "logstash-core", '>= 1.4.0', '< 2.0.0'
22
+ s.add_runtime_dependency 'logstash-codec-plain'
23
+ s.add_runtime_dependency 'rethinkdb', '~>2.0.0'
24
+ s.add_runtime_dependency 'eventmachine', '~>1.0.7'
25
+ s.add_development_dependency 'logstash-devutils'
26
+ end
@@ -0,0 +1 @@
1
+ require "logstash/devutils/rspec/spec_helper"
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-input-rethinkdb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - RethinkDB
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-15 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: 1.4.0
19
+ - - <
20
+ - !ruby/object:Gem::Version
21
+ version: 2.0.0
22
+ name: logstash-core
23
+ prerelease: false
24
+ type: :runtime
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.4.0
30
+ - - <
31
+ - !ruby/object:Gem::Version
32
+ version: 2.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ name: logstash-codec-plain
40
+ prerelease: false
41
+ type: :runtime
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ~>
51
+ - !ruby/object:Gem::Version
52
+ version: 2.0.0
53
+ name: rethinkdb
54
+ prerelease: false
55
+ type: :runtime
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ version: 2.0.0
61
+ - !ruby/object:Gem::Dependency
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ~>
65
+ - !ruby/object:Gem::Version
66
+ version: 1.0.7
67
+ name: eventmachine
68
+ prerelease: false
69
+ type: :runtime
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ~>
73
+ - !ruby/object:Gem::Version
74
+ version: 1.0.7
75
+ - !ruby/object:Gem::Dependency
76
+ requirement: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ name: logstash-devutils
82
+ prerelease: false
83
+ type: :development
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ 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
90
+ email: josh@rethinkdb.com
91
+ executables: []
92
+ extensions: []
93
+ extra_rdoc_files: []
94
+ files:
95
+ - .gitignore
96
+ - Gemfile
97
+ - LICENSE
98
+ - README.md
99
+ - Rakefile
100
+ - lib/logstash/inputs/rethinkdb.rb
101
+ - logstash-input-rethinkdb.gemspec
102
+ - spec/inputs/test_input.rb
103
+ homepage: http://www.rethinkdb.com/docs/logstash-plugins
104
+ licenses:
105
+ - Apache License (2.0)
106
+ metadata:
107
+ logstash_plugin: 'true'
108
+ logstash_group: input
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.1.9
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Streams changes from RethinkDB tables into LogStash.
129
+ test_files:
130
+ - spec/inputs/test_input.rb