logstash-output-mongodb_3.6 3.1.5

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: 723b46098f62b3c659a995e5b93b437182e00e51
4
+ data.tar.gz: 9d118c67bf1b49eaf5fc19f2077709af3838f6d5
5
+ SHA512:
6
+ metadata.gz: 87669c907e8c98839ab2b4b7d4148e7bb673c67ac65010b0eeac7080f4d82db6e5869747017b1f1401c06b3f15b8ef07199b252ede07ccccb4e77bb1bc7e94dc
7
+ data.tar.gz: 6bea5136bf2c688f927b46e87a659e4b1af0e8170181c6e0bac3cd49c9fe1b071a7a5cb577df9de949912b35ebe0d648ab1f47698b522bc34b57f9de5f080a95
data/CHANGELOG.md ADDED
@@ -0,0 +1,42 @@
1
+ ## 3.1.4
2
+ - Docs: Set the default_codec doc attribute.
3
+
4
+ ## 3.1.3
5
+ - Update gemspec summary
6
+
7
+ ## 3.1.2
8
+ - Fix some documentation issues
9
+
10
+ ## 3.1.0
11
+ - Add support for bulk inserts to improve performance.
12
+
13
+ ## 3.0.1
14
+ - Docs: Fix doc generation issue by removing extraneous comments and adding a short description of the plugin
15
+
16
+ ## 3.0.0
17
+ - Breaking: Updated plugin to use new Java Event APIs
18
+ - relax logstash-core-plugin-api constrains
19
+ - update .travis.yml
20
+
21
+ ## 2.0.5
22
+ - Depend on logstash-core-plugin-api instead of logstash-core, removing the need to mass update plugins on major releases of logstash
23
+
24
+ ## 2.0.4
25
+ - New dependency requirements for logstash-core for the 5.0 release
26
+
27
+ ## 2.0.3
28
+ - Patch Timestamp and BigDecimal with to_bson method and register with BSON.
29
+
30
+ ## 2.0.0
31
+ - Plugins were updated to follow the new shutdown semantic, this mainly allows Logstash to instruct input plugins to terminate gracefully,
32
+ instead of using Thread.raise on the plugins' threads. Ref: https://github.com/elastic/logstash/pull/3895
33
+ - Dependency on logstash-core update to 2.0
34
+
35
+ ## 1.0.0
36
+ - Fixes the plugin to be in the 2.0 series
37
+ - Add integration and unit test to the project
38
+ - Adapt the codebase to be 2.0 compatible
39
+ - Make the internal logger in mongo to report to LS logger
40
+
41
+ ## 0.2.0
42
+ - Add basic registration test to the project
data/CONTRIBUTORS ADDED
@@ -0,0 +1,22 @@
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
+
19
+ Note: If you've sent us patches, bug reports, or otherwise contributed to
20
+ Logstash, and you aren't on the list above and want to be, please let us know
21
+ and we'll make sure you're here. Contributions from folks like you are what make
22
+ 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.
data/NOTICE.TXT ADDED
@@ -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/).
data/README.md ADDED
@@ -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,118 @@
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::Mongodb < LogStash::Outputs::Base
10
+
11
+ config_name "mongodb"
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
+ # Mutex used to synchronize access to 'documents'
47
+ @@mutex = Mutex.new
48
+
49
+ public
50
+ def register
51
+ Mongo::Logger.logger = @logger
52
+ conn = Mongo::Client.new(@uri)
53
+ @db = conn.use(@database)
54
+
55
+ if @bulk_size > 1000
56
+ raise LogStash::ConfigurationError, "Bulk size must be lower than '1000', currently '#{@bulk_size}'"
57
+ end
58
+ @documents = {}
59
+ Thread.new do
60
+ loop do
61
+ sleep @bulk_interval
62
+ @@mutex.synchronize do
63
+ @documents.each do |collection, values|
64
+ if values.length > 0
65
+ @db[collection].insert_many(values)
66
+ @documents.delete(collection)
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end # def register
73
+
74
+ def receive(event)
75
+ begin
76
+ # Our timestamp object now has a to_bson method, using it here
77
+ # {}.merge(other) so we don't taint the event hash innards
78
+ document = {}.merge(event.to_hash)
79
+ if !@isodate
80
+ # not using timestamp.to_bson
81
+ document["@timestamp"] = event.timestamp.to_json
82
+ end
83
+ if @generateId
84
+ document["_id"] = BSON::ObjectId.new(nil, event.timestamp)
85
+ end
86
+ if @bulk
87
+ @@mutex.synchronize do
88
+ collection = event.sprintf(@collection)
89
+ if(!@documents[collection])
90
+ @documents[collection] = []
91
+ end
92
+ @documents[collection].push(document)
93
+
94
+ if(@documents[collection].length >= @bulk_size)
95
+ @db[collection].insert_many(@documents[collection])
96
+ @documents.delete(collection)
97
+ end
98
+ end
99
+ else
100
+ @db[event.sprintf(@collection)].insert_one(document)
101
+ end
102
+
103
+ rescue => e
104
+ @logger.warn("Failed to send event to MongoDB", :event => event, :exception => e,
105
+ :backtrace => e.backtrace)
106
+ if e.message =~ /^E11000/
107
+ # On a duplicate key error, skip the insert.
108
+ # We could check if the duplicate key err is the _id key
109
+ # and generate a new primary key.
110
+ # If the duplicate key error is on another field, we have no way
111
+ # to fix the issue.
112
+ else
113
+ sleep @retry_delay
114
+ retry
115
+ end
116
+ end
117
+ end # def receive
118
+ end # class LogStash::Outputs::Mongodb
@@ -0,0 +1,28 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-output-mongodb_3.6'
3
+ s.version = '3.1.5'
4
+ s.licenses = ['Apache License (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 = ["mhussainm"]
8
+ s.email = 'info@elastic.co'
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.5.1'
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,47 @@
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) do
12
+ { "uri" => uri, "database" => database, "collection" => collection }
13
+ end
14
+
15
+ it "should register" do
16
+ plugin = LogStash::Plugin.lookup("output", "mongodb").new(config)
17
+ expect {plugin.register}.to_not raise_error
18
+ end
19
+
20
+ describe "#send" do
21
+
22
+ subject! { LogStash::Outputs::Mongodb.new(config) }
23
+
24
+ let(:properties) { { "message" => "This is a message!",
25
+ "uuid" => SecureRandom.uuid,
26
+ "number" => BigDecimal.new("4321.1234"),
27
+ "utf8" => "żółć"} }
28
+ let(:event) { LogStash::Event.new(properties) }
29
+ let(:connection) { double("connection") }
30
+ let(:client) { double("client") }
31
+ let(:collection) { double("collection") }
32
+
33
+ before(:each) do
34
+ allow(Mongo::Client).to receive(:new).and_return(connection)
35
+ allow(connection).to receive(:use).and_return(client)
36
+ allow(client).to receive(:[]).and_return(collection)
37
+ allow(collection).to receive(:insert_one)
38
+ subject.register
39
+ end
40
+
41
+ it "should send the event to the database" do
42
+ expect(collection).to receive(:insert_one)
43
+ subject.receive(event)
44
+ end
45
+ end
46
+
47
+ 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_3.6
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.1.5
5
+ platform: ruby
6
+ authors:
7
+ - mhussainm
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-04-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logstash-core-plugin-api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.60'
20
+ - - "<="
21
+ - !ruby/object:Gem::Version
22
+ version: '2.99'
23
+ type: :runtime
24
+ prerelease: false
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
+ name: logstash-codec-plain
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: mongo
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 2.5.1
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: 2.5.1
61
+ - !ruby/object:Gem::Dependency
62
+ name: logstash-devutils
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
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: info@elastic.co
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.rb
94
+ - logstash-output-mongodb.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 License (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.5.2
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