logstash-output-mongodb_custom 0.1.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: 155093e771411be5b38edfaa8128bd1b2c1aa5e939df0502eae047bfe27ac02b
4
+ data.tar.gz: b453945bc7057acc243607535a610fb5978f0d702cc24e35fdffbc5f5b6a9ec5
5
+ SHA512:
6
+ metadata.gz: 9335d8a8ee2ded792a6ee87b2edb9898afc0a275690a436559e049bdd05ae9daf669fc690e4b25130d9b7a229ca7e3c4c8e3f0e1b195d5431c9d9c50acdb6f88
7
+ data.tar.gz: f7b74ff18f50992f9b13cdeeaabc79528ca896fc2d7639ff8c11af49181c33be0ee15581cf5413908c9a0e668905c8a3f5458d71f293f2e1dfbd13b7bb8377fd
@@ -0,0 +1,45 @@
1
+ ## 3.1.5
2
+ - Fixed @timestamp handling, BSON::ObjectId generation, close method [#57](https://github.com/logstash-plugins/logstash-output-mongodb/pull/57)
3
+
4
+ ## 3.1.4
5
+ - Docs: Set the default_codec doc attribute.
6
+
7
+ ## 3.1.3
8
+ - Update gemspec summary
9
+
10
+ ## 3.1.2
11
+ - Fix some documentation issues
12
+
13
+ ## 3.1.0
14
+ - Add support for bulk inserts to improve performance.
15
+
16
+ ## 3.0.1
17
+ - Docs: Fix doc generation issue by removing extraneous comments and adding a short description of the plugin
18
+
19
+ ## 3.0.0
20
+ - Breaking: Updated plugin to use new Java Event APIs
21
+ - relax logstash-core-plugin-api constrains
22
+ - update .travis.yml
23
+
24
+ ## 2.0.5
25
+ - Depend on logstash-core-plugin-api instead of logstash-core, removing the need to mass update plugins on major releases of logstash
26
+
27
+ ## 2.0.4
28
+ - New dependency requirements for logstash-core for the 5.0 release
29
+
30
+ ## 2.0.3
31
+ - Patch Timestamp and BigDecimal with to_bson method and register with BSON.
32
+
33
+ ## 2.0.0
34
+ - Plugins were updated to follow the new shutdown semantic, this mainly allows Logstash to instruct input plugins to terminate gracefully,
35
+ instead of using Thread.raise on the plugins' threads. Ref: https://github.com/elastic/logstash/pull/3895
36
+ - Dependency on logstash-core update to 2.0
37
+
38
+ ## 1.0.0
39
+ - Fixes the plugin to be in the 2.0 series
40
+ - Add integration and unit test to the project
41
+ - Adapt the codebase to be 2.0 compatible
42
+ - Make the internal logger in mongo to report to LS logger
43
+
44
+ ## 0.2.0
45
+ - Add basic registration test to the project
@@ -0,0 +1,23 @@
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
+ * Aaron Mildenstein (untergeek)
6
+ * Graham Bleach (bleach)
7
+ * John E. Vincent (lusis)
8
+ * Jordan Sissel (jordansissel)
9
+ * Kevin Amorin (kamorin)
10
+ * Kevin O'Connor (kjoconnor)
11
+ * Kurt Hurtado (kurtado)
12
+ * Mathias Gug (zimathias)
13
+ * Pete Fritchman (fetep)
14
+ * Pier-Hugues Pellerin (ph)
15
+ * Richard Pijnenburg (electrical)
16
+ * bitsofinfo (bitsofinfo)
17
+ * Guy Boertje (guyboertje)
18
+ * Colin Surprenant (colinsurprenant)
19
+
20
+ Note: If you've sent us patches, bug reports, or otherwise contributed to
21
+ Logstash, and you aren't on the list above and want to be, please let us know
22
+ and we'll make sure you're here. Contributions from folks like you are what make
23
+ open source awesome.
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,13 @@
1
+ Copyright (c) 2012-2018 Elasticsearch <http://www.elastic.co>
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.
@@ -0,0 +1,5 @@
1
+ Elasticsearch
2
+ Copyright 2012-2015 Elasticsearch
3
+
4
+ This product includes software developed by The Apache Software
5
+ Foundation (http://www.apache.org/).
@@ -0,0 +1,98 @@
1
+ # Logstash Plugin
2
+
3
+ [![Travis Build Status](https://travis-ci.org/logstash-plugins/logstash-output-mongodb.svg)](https://travis-ci.org/logstash-plugins/logstash-output-mongodb)
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
+ ## Documentation
10
+
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.elastic.co/guide/en/logstash/current/).
12
+
13
+ - 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/elastic/docs#asciidoc-guide
15
+
16
+ ## Need Help?
17
+
18
+ Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
19
+
20
+ ## Developing
21
+
22
+ ### 1. Plugin Developement and Testing
23
+
24
+ #### Code
25
+ - To get started, you'll need JRuby with the Bundler gem installed.
26
+
27
+ - 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).
28
+
29
+ - Install dependencies
30
+ ```sh
31
+ bundle install
32
+ ```
33
+
34
+ #### Test
35
+
36
+ - Update your dependencies
37
+
38
+ ```sh
39
+ bundle install
40
+ ```
41
+
42
+ - Run tests
43
+
44
+ ```sh
45
+ bundle exec rspec
46
+ ```
47
+
48
+ ### 2. Running your unpublished Plugin in Logstash
49
+
50
+ #### 2.1 Run in a local Logstash clone
51
+
52
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
53
+ ```ruby
54
+ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
55
+ ```
56
+ - Install plugin
57
+ ```sh
58
+ # Logstash 2.3 and higher
59
+ bin/logstash-plugin install --no-verify
60
+
61
+ # Prior to Logstash 2.3
62
+ bin/plugin install --no-verify
63
+
64
+ ```
65
+ - Run Logstash with your plugin
66
+ ```sh
67
+ bin/logstash -e 'filter {awesome {}}'
68
+ ```
69
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
70
+
71
+ #### 2.2 Run in an installed Logstash
72
+
73
+ 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:
74
+
75
+ - Build your plugin gem
76
+ ```sh
77
+ gem build logstash-filter-awesome.gemspec
78
+ ```
79
+ - Install the plugin from the Logstash home
80
+ ```sh
81
+ # Logstash 2.3 and higher
82
+ bin/logstash-plugin install --no-verify
83
+
84
+ # Prior to Logstash 2.3
85
+ bin/plugin install --no-verify
86
+
87
+ ```
88
+ - Start Logstash and proceed to test the plugin
89
+
90
+ ## Contributing
91
+
92
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
93
+
94
+ 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.
95
+
96
+ It is more important to the community that you are able to contribute.
97
+
98
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1,137 @@
1
+ :plugin: mongodb
2
+ :type: output
3
+ :default_codec: plain
4
+
5
+ ///////////////////////////////////////////
6
+ START - GENERATED VARIABLES, DO NOT EDIT!
7
+ ///////////////////////////////////////////
8
+ :version: %VERSION%
9
+ :release_date: %RELEASE_DATE%
10
+ :changelog_url: %CHANGELOG_URL%
11
+ :include_path: ../../../../logstash/docs/include
12
+ ///////////////////////////////////////////
13
+ END - GENERATED VARIABLES, DO NOT EDIT!
14
+ ///////////////////////////////////////////
15
+
16
+ [id="plugins-{type}s-{plugin}"]
17
+
18
+ === Mongodb output plugin
19
+
20
+ include::{include_path}/plugin_header.asciidoc[]
21
+
22
+ ==== Description
23
+
24
+ This output writes events to MongoDB.
25
+
26
+ [id="plugins-{type}s-{plugin}-options"]
27
+ ==== Mongodb Output Configuration Options
28
+
29
+ This plugin supports the following configuration options plus the <<plugins-{type}s-{plugin}-common-options>> described later.
30
+
31
+ [cols="<,<,<",options="header",]
32
+ |=======================================================================
33
+ |Setting |Input type|Required
34
+ | <<plugins-{type}s-{plugin}-bulk>> |<<boolean,boolean>>|No
35
+ | <<plugins-{type}s-{plugin}-bulk_interval>> |<<number,number>>|No
36
+ | <<plugins-{type}s-{plugin}-bulk_size>> |<<number,number>>|No
37
+ | <<plugins-{type}s-{plugin}-collection>> |<<string,string>>|Yes
38
+ | <<plugins-{type}s-{plugin}-database>> |<<string,string>>|Yes
39
+ | <<plugins-{type}s-{plugin}-generateId>> |<<boolean,boolean>>|No
40
+ | <<plugins-{type}s-{plugin}-isodate>> |<<boolean,boolean>>|No
41
+ | <<plugins-{type}s-{plugin}-retry_delay>> |<<number,number>>|No
42
+ | <<plugins-{type}s-{plugin}-uri>> |<<string,string>>|Yes
43
+ |=======================================================================
44
+
45
+ Also see <<plugins-{type}s-{plugin}-common-options>> for a list of options supported by all
46
+ output plugins.
47
+
48
+ &nbsp;
49
+
50
+ [id="plugins-{type}s-{plugin}-bulk"]
51
+ ===== `bulk`
52
+
53
+ * Value type is <<boolean,boolean>>
54
+ * Default value is `false`
55
+
56
+ Bulk insert flag, set to true to allow bulk insertion, else it will insert events one by one.
57
+
58
+ [id="plugins-{type}s-{plugin}-bulk_interval"]
59
+ ===== `bulk_interval`
60
+
61
+ * Value type is <<number,number>>
62
+ * Default value is `2`
63
+
64
+ Bulk interval, Used to insert events periodically if the "bulk" flag is activated.
65
+
66
+ [id="plugins-{type}s-{plugin}-bulk_size"]
67
+ ===== `bulk_size`
68
+
69
+ * Value type is <<number,number>>
70
+ * Default value is `900`
71
+
72
+ Bulk events number, if the number of events to insert into a collection raise that limit, it will be bulk inserted
73
+ whatever the bulk interval value (mongodb hard limit is 1000).
74
+
75
+ [id="plugins-{type}s-{plugin}-collection"]
76
+ ===== `collection`
77
+
78
+ * This is a required setting.
79
+ * Value type is <<string,string>>
80
+ * There is no default value for this setting.
81
+
82
+ The collection to use. This value can use `%{foo}` values to dynamically
83
+ select a collection based on data in the event.
84
+
85
+ [id="plugins-{type}s-{plugin}-database"]
86
+ ===== `database`
87
+
88
+ * This is a required setting.
89
+ * Value type is <<string,string>>
90
+ * There is no default value for this setting.
91
+
92
+ The database to use.
93
+
94
+ [id="plugins-{type}s-{plugin}-generateId"]
95
+ ===== `generateId`
96
+
97
+ * Value type is <<boolean,boolean>>
98
+ * Default value is `false`
99
+
100
+ If true, an "_id" field will be added to the document before insertion.
101
+ The "_id" field will use the timestamp of the event and overwrite an existing
102
+ "_id" field in the event.
103
+
104
+ [id="plugins-{type}s-{plugin}-isodate"]
105
+ ===== `isodate`
106
+
107
+ * Value type is <<boolean,boolean>>
108
+ * Default value is `false`
109
+
110
+ If true, store the @timestamp field in MongoDB as an ISODate type instead
111
+ of an ISO8601 string. For more information about this, see
112
+ http://www.mongodb.org/display/DOCS/Dates.
113
+
114
+ [id="plugins-{type}s-{plugin}-retry_delay"]
115
+ ===== `retry_delay`
116
+
117
+ * Value type is <<number,number>>
118
+ * Default value is `3`
119
+
120
+ The number of seconds to wait after failure before retrying.
121
+
122
+ [id="plugins-{type}s-{plugin}-uri"]
123
+ ===== `uri`
124
+
125
+ * This is a required setting.
126
+ * Value type is <<string,string>>
127
+ * There is no default value for this setting.
128
+
129
+ A MongoDB URI to connect to.
130
+ See http://docs.mongodb.org/manual/reference/connection-string/.
131
+
132
+
133
+
134
+ [id="plugins-{type}s-{plugin}-common-options"]
135
+ include::{include_path}/{type}.asciidoc[]
136
+
137
+ :default_codec!:
@@ -0,0 +1,66 @@
1
+ # Copyright (C) 2009-2014 MongoDB Inc.
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.
14
+
15
+ # Modified 2015 Elastic
16
+
17
+ module BSON
18
+
19
+ # Injects behaviour for encoding and decoding BigDecimal values
20
+ # to and from # raw bytes as specified by the BSON spec.
21
+ #
22
+ # @see http://bsonspec.org/#/specification
23
+ module BigDecimal
24
+
25
+ # A floating point is type 0x01 in the BSON spec.
26
+ BSON_TYPE = 1.chr.force_encoding(BINARY).freeze
27
+
28
+ # The pack directive is for 8 byte floating points.
29
+ PACK = "E".freeze
30
+
31
+ # Get the floating point as encoded BSON.
32
+ # @example Get the floating point as encoded BSON.
33
+ # 1.221311.to_bson
34
+ # @return [ String ] The encoded string.
35
+ # @see http://bsonspec.org/#/specification
36
+ def to_bson(encoded = ''.force_encoding(BINARY))
37
+ encoded << [ self ].pack(PACK)
38
+ end
39
+
40
+ module ClassMethods
41
+
42
+ # Deserialize an instance of a BigDecimal from a BSON double.
43
+ # @param [ BSON ] bson object from Mongo.
44
+ # @return [ BigDecimal ] The decoded BigDecimal.
45
+ # @see http://bsonspec.org/#/specification
46
+ def from_bson(bson)
47
+ from_bson_double(bson.read(8))
48
+ end
49
+
50
+ private
51
+
52
+ def from_bson_double(double)
53
+ new(double.unpack(PACK).first.to_s)
54
+ end
55
+ end
56
+
57
+ # Register this type when the module is loaded.
58
+ Registry.register(BSON_TYPE, ::BigDecimal)
59
+ end
60
+
61
+ # Enrich the core BigDecimal class with this module.
62
+ #
63
+ # @since 2.0.0
64
+ ::BigDecimal.send(:include, BigDecimal)
65
+ ::BigDecimal.send(:extend, BigDecimal::ClassMethods)
66
+ end
@@ -0,0 +1,76 @@
1
+ # Copyright (C) 2009-2014 MongoDB Inc.
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.
14
+
15
+ # Modified 2015 Elastic
16
+
17
+ module BSON
18
+
19
+ # Injects behaviour for encoding and decoding time values to
20
+ # and from raw bytes as specified by the BSON spec.
21
+ #
22
+ # @see http://bsonspec.org/#/specification
23
+ module LogStashEvent
24
+
25
+ # An Event is an embedded document is type 0x03 in the BSON spec..
26
+ BSON_TYPE = 3.chr.force_encoding(BINARY).freeze
27
+
28
+ # Get the event as encoded BSON.
29
+ # @example Get the hash as encoded BSON.
30
+ # Event.new("field" => "value").to_bson
31
+ # @return [ String ] The encoded string.
32
+ # @see http://bsonspec.org/#/specification
33
+ def to_bson(buffer = ByteBuffer.new)
34
+ position = buffer.length
35
+ buffer.put_int32(0)
36
+ to_hash.each do |field, value|
37
+ buffer.put_byte(value.bson_type)
38
+ buffer.put_cstring(field.to_bson_key)
39
+ value.to_bson(buffer)
40
+ end
41
+ buffer.put_byte(NULL_BYTE)
42
+ buffer.replace_int32(position, buffer.length - position)
43
+ end
44
+
45
+ # Converts the event to a normalized value in a BSON document.
46
+ # @example Convert the event to a normalized value.
47
+ # event.to_bson_normalized_value
48
+ # @return [ BSON::Document ] The normalized event.
49
+ def to_bson_normalized_value
50
+ Document.new(self)
51
+ end
52
+
53
+ module ClassMethods
54
+ # Deserialize the Event from BSON.
55
+ # @param [ ByteBuffer ] buffer The byte buffer.
56
+ # @return [ Event ] The decoded bson document.
57
+ # @see http://bsonspec.org/#/specification
58
+ def from_bson(buffer)
59
+ hash = Hash.new
60
+ buffer.get_int32 # Throw away the size.
61
+ while (type = buffer.get_byte) != NULL_BYTE
62
+ field = buffer.get_cstring
63
+ hash.store(field, BSON::Registry.get(type).from_bson(buffer))
64
+ end
65
+ new(hash)
66
+ end
67
+ end
68
+
69
+ # Register this type when the module is loaded.
70
+ Registry.register(BSON_TYPE, ::LogStash::Event)
71
+ end
72
+
73
+ # Enrich the core LogStash::Event class with this module.
74
+ ::LogStash::Event.send(:include, ::LogStashEvent)
75
+ ::LogStash::Event.send(:extend, ::LogStashEvent::ClassMethods)
76
+ end
@@ -0,0 +1,50 @@
1
+ # Copyright (C) 2009-2014 MongoDB Inc.
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.
14
+
15
+ # Modified 2015 Elastic
16
+
17
+ module BSON
18
+
19
+ # Injects behaviour for encoding and decoding time values to
20
+ # and from raw bytes as specified by the BSON spec.
21
+ #
22
+ # @see http://bsonspec.org/#/specification
23
+ module LogStashTimestamp
24
+
25
+ # A time is type 0x09 in the BSON spec.
26
+ BSON_TYPE = 9.chr.force_encoding(BINARY).freeze
27
+
28
+ def to_bson(encoded = ''.force_encoding(BINARY))
29
+ time.to_bson(encoded)
30
+ end
31
+
32
+ module ClassMethods
33
+ # Deserialize UTC time from BSON.
34
+ # @param [ BSON ] bson encoded time.
35
+ # @return [ ::LogStash::Timestamp ] The decoded UTC time as a ::LogStash::Timestamp.
36
+ # @see http://bsonspec.org/#/specification
37
+ def from_bson(bson)
38
+ seconds, fragment = BSON::Int64.from_bson(bson).divmod(1000)
39
+ new(::Time.at(seconds, fragment * 1000).utc)
40
+ end
41
+ end
42
+
43
+ # Register this type when the module is loaded.
44
+ Registry.register(BSON_TYPE, ::LogStash::Timestamp)
45
+ end
46
+
47
+ # Enrich the core LogStash::Timestamp class with this module.
48
+ ::LogStash::Timestamp.send(:include, LogStashTimestamp)
49
+ ::LogStash::Timestamp.send(:extend, LogStashTimestamp::ClassMethods)
50
+ end
@@ -0,0 +1,145 @@
1
+ # encoding: utf-8
2
+ require "logstash/outputs/base"
3
+ require "logstash/namespace"
4
+ require "mongo"
5
+ require_relative "bson/big_decimal"
6
+ require_relative "bson/logstash_timestamp"
7
+
8
+ # This output writes events to MongoDB.
9
+ class LogStash::Outputs::MongodbCustom < LogStash::Outputs::Base
10
+
11
+ config_name "mongodb_custom"
12
+
13
+ # A MongoDB URI to connect to.
14
+ # See http://docs.mongodb.org/manual/reference/connection-string/.
15
+ config :uri, :validate => :string, :required => true
16
+
17
+ # The database to use.
18
+ config :database, :validate => :string, :required => true
19
+
20
+ # The collection to use. This value can use `%{foo}` values to dynamically
21
+ # select a collection based on data in the event.
22
+ config :collection, :validate => :string, :required => true
23
+
24
+ # If true, store the @timestamp field in MongoDB as an ISODate type instead
25
+ # of an ISO8601 string. For more information about this, see
26
+ # http://www.mongodb.org/display/DOCS/Dates.
27
+ config :isodate, :validate => :boolean, :default => false
28
+
29
+ # The number of seconds to wait after failure before retrying.
30
+ config :retry_delay, :validate => :number, :default => 3, :required => false
31
+
32
+ # If true, an "_id" field will be added to the document before insertion.
33
+ # The "_id" field will use the timestamp of the event and overwrite an existing
34
+ # "_id" field in the event.
35
+ config :generateId, :validate => :boolean, :default => false
36
+
37
+
38
+ # Bulk insert flag, set to true to allow bulk insertion, else it will insert events one by one.
39
+ config :bulk, :validate => :boolean, :default => false
40
+ # Bulk interval, Used to insert events periodically if the "bulk" flag is activated.
41
+ config :bulk_interval, :validate => :number, :default => 2
42
+ # Bulk events number, if the number of events to insert into a collection raise that limit, it will be bulk inserted
43
+ # whatever the bulk interval value (mongodb hard limit is 1000).
44
+ config :bulk_size, :validate => :number, :default => 900, :maximum => 999, :min => 2
45
+
46
+ config :date_keys, :validate => :string, :default => nil
47
+
48
+ # Mutex used to synchronize access to 'documents'
49
+ @@mutex = Mutex.new
50
+
51
+ def register
52
+ if @bulk_size > 1000
53
+ raise LogStash::ConfigurationError, "Bulk size must be lower than '1000', currently '#{@bulk_size}'"
54
+ end
55
+
56
+ Mongo::Logger.logger = @logger
57
+ conn = Mongo::Client.new(@uri)
58
+ @db = conn.use(@database)
59
+
60
+ @closed = Concurrent::AtomicBoolean.new(false)
61
+ @documents = {}
62
+
63
+ @bulk_thread = Thread.new(@bulk_interval) do |bulk_interval|
64
+ while @closed.false? do
65
+ sleep(bulk_interval)
66
+
67
+ @@mutex.synchronize do
68
+ @documents.each do |collection, values|
69
+ if values.length > 0
70
+ @db[collection].insert_many(values)
71
+ @documents.delete(collection)
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+ def receive(event)
80
+ begin
81
+ # Our timestamp object now has a to_bson method, using it here
82
+ # {}.merge(other) so we don't taint the event hash innards
83
+ document = {}.merge(event.to_hash)
84
+
85
+ if !@isodate
86
+ timestamp = event.timestamp
87
+ if timestamp
88
+ # not using timestamp.to_bson
89
+ document["@timestamp"] = timestamp.to_json
90
+ else
91
+ @logger.warn("Cannot set MongoDB document `@timestamp` field because it does not exist in the event", :event => event)
92
+ end
93
+ end
94
+
95
+ if @date_keys
96
+ keys = date_keys.to_s.split(",")
97
+ document.each do |key, value|
98
+ if keys.index key
99
+ document[key] = LogStash::Timestamp.new(value)
100
+ end
101
+ end
102
+ end
103
+
104
+ if @generateId
105
+ document["_id"] = BSON::ObjectId.new
106
+ end
107
+
108
+ if @bulk
109
+ collection = event.sprintf(@collection)
110
+ @@mutex.synchronize do
111
+ if(!@documents[collection])
112
+ @documents[collection] = []
113
+ end
114
+ @documents[collection].push(document)
115
+
116
+ if(@documents[collection].length >= @bulk_size)
117
+ @db[collection].insert_many(@documents[collection])
118
+ @documents.delete(collection)
119
+ end
120
+ end
121
+ else
122
+ @db[event.sprintf(@collection)].insert_one(document)
123
+ end
124
+ rescue => e
125
+ if e.message =~ /^E11000/
126
+ # On a duplicate key error, skip the insert.
127
+ # We could check if the duplicate key err is the _id key
128
+ # and generate a new primary key.
129
+ # If the duplicate key error is on another field, we have no way
130
+ # to fix the issue.
131
+ @logger.warn("Skipping insert because of a duplicate key error", :event => event, :exception => e)
132
+ else
133
+ @logger.warn("Failed to send event to MongoDB, retrying in #{@retry_delay.to_s} seconds", :event => event, :exception => e)
134
+ sleep(@retry_delay)
135
+ retry
136
+ end
137
+ end
138
+ end
139
+
140
+ def close
141
+ @closed.make_true
142
+ @bulk_thread.wakeup
143
+ @bulk_thread.join
144
+ end
145
+ end
@@ -0,0 +1,28 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-output-mongodb_custom'
3
+ s.version = '0.1.0'
4
+ s.licenses = ['Apache-2.0']
5
+ s.summary = "Writes events to MongoDB"
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.authors = ["Ravi Shah"]
8
+ s.email = 'ravi.shah@softwareag.com'
9
+ s.homepage = "http://www.elastic.co/guide/en/logstash/current/index.html"
10
+ s.require_paths = ["lib"]
11
+
12
+ # Files
13
+ s.files = Dir["lib/**/*","spec/**/*","*.gemspec","*.md","CONTRIBUTORS","Gemfile","LICENSE","NOTICE.TXT", "vendor/jar-dependencies/**/*.jar", "vendor/jar-dependencies/**/*.rb", "VERSION", "docs/**/*"]
14
+
15
+ # Tests
16
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
17
+
18
+ # Special flag to let us know this is actually a logstash plugin
19
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "output" }
20
+
21
+ # Gem dependencies
22
+ s.add_runtime_dependency "logstash-core-plugin-api", ">= 1.60", "<= 2.99"
23
+ s.add_runtime_dependency 'logstash-codec-plain'
24
+ s.add_runtime_dependency 'mongo', '~> 2.0.6'
25
+
26
+ s.add_development_dependency 'logstash-devutils'
27
+ end
28
+
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+ require 'bigdecimal'
3
+ require_relative "../spec_helper"
4
+ require 'stringio'
5
+
6
+ describe ::BigDecimal do
7
+ let(:a_number) { "4321.1234" }
8
+ let(:bson_number) { 4321.1234.to_bson }
9
+
10
+ subject { described_class.new(a_number) }
11
+
12
+ it "responds to to_bson" do
13
+ expect(subject).to respond_to(:to_bson)
14
+ end
15
+
16
+ it "to_bson returns a binary encoded number" do
17
+ expect(subject.to_bson).to eq(4321.1234.to_bson)
18
+ end
19
+
20
+ it "bson_type returns a binary encoded 1" do
21
+ expect(subject.bson_type).to eq(12.34.bson_type)
22
+ end
23
+
24
+ describe "class methods" do
25
+ it "builds a new BigDecimal from BSON" do
26
+ decoded = described_class.from_bson(StringIO.new(4321.1234.to_bson))
27
+ expect(decoded).to eql(BigDecimal.new(a_number))
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+ require_relative "../spec_helper"
3
+ require 'stringio'
4
+
5
+ describe ::LogStash::Timestamp do
6
+ let(:time_array) { [1918,11,11,11,0,0, "+00:00"] }
7
+ let(:a_time) { Time.utc(*time_array) }
8
+ let(:bson_time) { Time.utc(*time_array).to_bson }
9
+
10
+ subject(:timestamp) { described_class.new(a_time) }
11
+
12
+ it "responds to to_bson" do
13
+ expect(subject).to respond_to(:to_bson)
14
+ end
15
+
16
+ it "to_bson returns a binary encoded timestamp" do
17
+ expect(timestamp.to_bson).to eq(bson_time)
18
+ end
19
+
20
+ it "bson_type returns a binary encoded 9" do
21
+ expect(subject.bson_type).to eq(a_time.bson_type)
22
+ end
23
+
24
+ describe "class methods" do
25
+ it "builds a new Timestamp from BSON" do
26
+ expected = ::LogStash::Timestamp.new(a_time)
27
+ decoded = ::LogStash::Timestamp.from_bson(StringIO.new(bson_time))
28
+ expect(decoded <=> expected).to eq(0)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+ require_relative "../spec_helper"
3
+
4
+ describe LogStash::Outputs::Mongodb, :integration => true do
5
+
6
+ let(:uri) { 'mongodb://localhost:27017' }
7
+ let(:database) { 'logstash' }
8
+ let(:collection) { 'logs' }
9
+ let(:uuid) { SecureRandom.uuid }
10
+
11
+ let(:config) do
12
+ { "uri" => uri, "database" => database,
13
+ "collection" => collection, "isodate" => true }
14
+ end
15
+
16
+ describe "#send" do
17
+
18
+ subject { LogStash::Outputs::Mongodb.new(config) }
19
+
20
+ let(:properties) { { "message" => "This is a message!",
21
+ "uuid" => uuid, "number" => BigDecimal.new("4321.1234"),
22
+ "utf8" => "żółć", "int" => 42,
23
+ "arry" => [42, "string", 4321.1234]} }
24
+ let(:event) { LogStash::Event.new(properties) }
25
+
26
+ before(:each) do
27
+ subject.register
28
+ end
29
+
30
+ it "should send the event to the database" do
31
+ subject.receive(event)
32
+ expect(subject).to have_received(event)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,86 @@
1
+ # encoding: utf-8
2
+ require_relative "../spec_helper"
3
+ require "logstash/plugin"
4
+
5
+ describe LogStash::Outputs::Mongodb do
6
+
7
+ let(:uri) { 'mongodb://localhost:27017' }
8
+ let(:database) { 'logstash' }
9
+ let(:collection) { 'logs' }
10
+
11
+ let(:config) {{
12
+ "uri" => uri,
13
+ "database" => database,
14
+ "collection" => collection
15
+ }}
16
+
17
+ it "should register and close" do
18
+ plugin = LogStash::Plugin.lookup("output", "mongodb").new(config)
19
+ expect {plugin.register}.to_not raise_error
20
+ plugin.close
21
+ end
22
+
23
+ describe "receive" do
24
+ subject! { LogStash::Outputs::Mongodb.new(config) }
25
+
26
+ let(:event) { LogStash::Event.new(properties) }
27
+ let(:connection) { double("connection") }
28
+ let(:client) { double("client") }
29
+ let(:collection) { double("collection") }
30
+
31
+ before(:each) do
32
+ allow(Mongo::Client).to receive(:new).and_return(connection)
33
+ allow(connection).to receive(:use).and_return(client)
34
+ allow(client).to receive(:[]).and_return(collection)
35
+ allow(collection).to receive(:insert_one)
36
+ subject.register
37
+ end
38
+
39
+ after(:each) do
40
+ subject.close
41
+ end
42
+
43
+ describe "#send" do
44
+ let(:properties) {{
45
+ "message" => "This is a message!",
46
+ "uuid" => SecureRandom.uuid,
47
+ "number" => BigDecimal.new("4321.1234"),
48
+ "utf8" => "żółć"
49
+ }}
50
+
51
+ it "should send the event to the database" do
52
+ expect(collection).to receive(:insert_one)
53
+ subject.receive(event)
54
+ end
55
+ end
56
+
57
+ describe "no event @timestamp" do
58
+ let(:properties) { { "message" => "foo" } }
59
+
60
+ it "should not contain a @timestamp field in the mongo document" do
61
+ expect(event).to receive(:timestamp).and_return(nil)
62
+ expect(event).to receive(:to_hash).and_return(properties)
63
+ expect(collection).to receive(:insert_one).with(properties)
64
+ subject.receive(event)
65
+ end
66
+ end
67
+
68
+ describe "generateId" do
69
+ let(:properties) { { "message" => "foo" } }
70
+ let(:config) {{
71
+ "uri" => uri,
72
+ "database" => database,
73
+ "collection" => collection,
74
+ "generateId" => true
75
+ }}
76
+
77
+ it "should contain a BSON::ObjectId as _id" do
78
+ expect(BSON::ObjectId).to receive(:new).and_return("BSON::ObjectId")
79
+ expect(event).to receive(:timestamp).and_return(nil)
80
+ expect(event).to receive(:to_hash).and_return(properties)
81
+ expect(collection).to receive(:insert_one).with(properties.merge("_id" => "BSON::ObjectId"))
82
+ subject.receive(event)
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
3
+ require "logstash/outputs/mongodb"
4
+
5
+ RSpec::Matchers.define :have_received do |event|
6
+ match do |subject|
7
+ client = subject.instance_variable_get("@db")
8
+ collection = subject.instance_variable_get("@collection")
9
+ client["#{collection}"].find("uuid" => event["uuid"]).count > 0
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-output-mongodb_custom
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ravi Shah
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-02-16 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.60'
19
+ - - "<="
20
+ - !ruby/object:Gem::Version
21
+ version: '2.99'
22
+ name: logstash-core-plugin-api
23
+ prerelease: false
24
+ type: :runtime
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '1.60'
30
+ - - "<="
31
+ - !ruby/object:Gem::Version
32
+ version: '2.99'
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.6
53
+ name: mongo
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.6
61
+ - !ruby/object:Gem::Dependency
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ name: logstash-devutils
68
+ prerelease: false
69
+ type: :development
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ description: This gem is a Logstash plugin required to be installed on top of the
76
+ Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This
77
+ gem is not a stand-alone program
78
+ email: ravi.shah@softwareag.com
79
+ executables: []
80
+ extensions: []
81
+ extra_rdoc_files: []
82
+ files:
83
+ - CHANGELOG.md
84
+ - CONTRIBUTORS
85
+ - Gemfile
86
+ - LICENSE
87
+ - NOTICE.TXT
88
+ - README.md
89
+ - docs/index.asciidoc
90
+ - lib/logstash/outputs/bson/big_decimal.rb
91
+ - lib/logstash/outputs/bson/logstash_event.rb
92
+ - lib/logstash/outputs/bson/logstash_timestamp.rb
93
+ - lib/logstash/outputs/mongodb_custom.rb
94
+ - logstash-output-mongodb_custom.gemspec
95
+ - spec/bson/big_decimal_spec.rb
96
+ - spec/bson/logstash_timestamp_spec.rb
97
+ - spec/integration/mongodb_spec.rb
98
+ - spec/outputs/mongodb_spec.rb
99
+ - spec/spec_helper.rb
100
+ homepage: http://www.elastic.co/guide/en/logstash/current/index.html
101
+ licenses:
102
+ - Apache-2.0
103
+ metadata:
104
+ logstash_plugin: 'true'
105
+ logstash_group: output
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.7.6
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Writes events to MongoDB
126
+ test_files:
127
+ - spec/bson/big_decimal_spec.rb
128
+ - spec/bson/logstash_timestamp_spec.rb
129
+ - spec/integration/mongodb_spec.rb
130
+ - spec/outputs/mongodb_spec.rb
131
+ - spec/spec_helper.rb