Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow bulk upsert and set custom field used for _id field in mongo, fix BigDecimal.new is deprecated #87

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ source 'https://rubygems.org'

gemspec

logstash_path = ENV["LOGSTASH_PATH"] || "../../logstash"
logstash_path = ENV["LOGSTASH_PATH"] || "."
use_logstash_source = ENV["LOGSTASH_SOURCE"] && ENV["LOGSTASH_SOURCE"].to_s == "1"

if Dir.exist?(logstash_path) && use_logstash_source
Expand Down
2 changes: 1 addition & 1 deletion lib/logstash/outputs/bson/big_decimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def from_bson(bson)
private

def from_bson_double(double)
new(double.unpack(PACK).first.to_s)
BigDecimal(double.unpack(PACK).first.to_s)
end
end

Expand Down
41 changes: 38 additions & 3 deletions lib/logstash/outputs/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ class LogStash::Outputs::Mongodb < LogStash::Outputs::Base
# "_id" field in the event.
config :generateId, :validate => :boolean, :default => false

# The field that will be used for the _id field
# This can be for example the ID column of a SQL table when using JDBC.
config :idField, :validate => :string, :required => false

# Upsert documents flag, set to true to use replace_one instead of insert_one.
config :upsert, :validate => :boolean, :default => false

# Bulk insert flag, set to true to allow bulk insertion, else it will insert events one by one.
config :bulk, :validate => :boolean, :default => false
Expand Down Expand Up @@ -65,7 +71,14 @@ def register
@@mutex.synchronize do
@documents.each do |collection, values|
if values.length > 0
@db[collection].insert_many(values)
if @upsert
bulk_operations = values.map do |doc|
{ replace_one: { filter: { _id: doc["_id"] }, replacement: doc, upsert: true } }
end
@db[collection].bulk_write(bulk_operations)
else
@db[collection].insert_many(values)
end
@documents.delete(collection)
end
end
Expand Down Expand Up @@ -94,6 +107,17 @@ def receive(event)
document["_id"] = BSON::ObjectId.new
end

if @idField
field_name = event.sprintf(@idField)
if event.include?(field_name) && !event.get(field_name).nil?
document["_id"] = event.get(field_name)
else
@logger.warn("Cannot set MongoDB document `_id` field because it does not exist in the event", :event => event)
document["_id"] = BSON::ObjectId.new
end
end


if @bulk
collection = event.sprintf(@collection)
@@mutex.synchronize do
Expand All @@ -103,12 +127,23 @@ def receive(event)
@documents[collection].push(document)

if(@documents[collection].length >= @bulk_size)
@db[collection].insert_many(@documents[collection])
if @upsert && document.key?("_id")
bulk_operations = @documents[collection].map do |doc|
{ replace_one: { filter: { _id: doc["_id"] }, replacement: doc, upsert: true } }
end
@db[collection].bulk_write(bulk_operations)
else
@db[collection].insert_many(@documents[collection])
end
@documents.delete(collection)
end
end
else
@db[event.sprintf(@collection)].insert_one(document)
if @upsert && document.key?("_id")
@db[event.sprintf(@collection)].replace_one({ _id: document["_id"] }, document, { upsert: true })
else
@db[event.sprintf(@collection)].insert_one(document)
end
end
rescue => e
if e.message =~ /^E11000/
Expand Down
3 changes: 1 addition & 2 deletions logstash-output-mongodb.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ Gem::Specification.new do |s|
s.add_runtime_dependency 'mongo', '~> 2.6'

s.add_development_dependency 'logstash-devutils'
end

end
2 changes: 1 addition & 1 deletion spec/bson/big_decimal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
describe "class methods" do
it "builds a new BigDecimal from BSON" do
decoded = described_class.from_bson(4321.1234.to_bson)
expect(decoded).to eql(BigDecimal.new(a_number))
expect(decoded).to eql(BigDecimal(a_number))
end
end
end
2 changes: 1 addition & 1 deletion spec/integration/mongodb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
subject { LogStash::Outputs::Mongodb.new(config) }

let(:properties) { { "message" => "This is a message!",
"uuid" => uuid, "number" => BigDecimal.new("4321.1234"),
"uuid" => uuid, "number" => BigDecimal("4321.1234"),
"utf8" => "żółć", "int" => 42,
"arry" => [42, "string", 4321.1234]} }
let(:event) { LogStash::Event.new(properties) }
Expand Down
2 changes: 1 addition & 1 deletion spec/outputs/mongodb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
let(:properties) {{
"message" => "This is a message!",
"uuid" => SecureRandom.uuid,
"number" => BigDecimal.new("4321.1234"),
"number" => BigDecimal("4321.1234"),
"utf8" => "żółć"
}}

Expand Down