Class: LogStash::Outputs::Mongodb
- Inherits:
-
Base
- Object
- Base
- LogStash::Outputs::Mongodb
- Defined in:
- lib/logstash/outputs/mongodb.rb
Overview
This output writes events to MongoDB.
Constant Summary collapse
- @@mutex =
Mutex used to synchronize access to ‘documents’
Mutex.new
Instance Method Summary collapse
-
#receive(event) ⇒ Object
def register.
- #register ⇒ Object
Instance Method Details
#receive(event) ⇒ Object
def register
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/logstash/outputs/mongodb.rb', line 74 def receive(event) begin # Our timestamp object now has a to_bson method, using it here # {}.merge(other) so we don't taint the event hash innards document = {}.merge(event.to_hash) if !@isodate # not using timestamp.to_bson document["@timestamp"] = event..to_json end if @generateId document["_id"] = BSON::ObjectId.new(nil, event.) end if @bulk @@mutex.synchronize do collection = event.sprintf(@collection) if(!@documents[collection]) @documents[collection] = [] end @documents[collection].push(document) if(@documents[collection].length >= @bulk_size) @db[collection].insert_many(@documents[collection]) @documents.delete(collection) end end else @db[event.sprintf(@collection)].insert_one(document) end rescue => e @logger.warn("Failed to send event to MongoDB", :event => event, :exception => e, :backtrace => e.backtrace) if e. =~ /^E11000/ # On a duplicate key error, skip the insert. # We could check if the duplicate key err is the _id key # and generate a new primary key. # If the duplicate key error is on another field, we have no way # to fix the issue. else sleep @retry_delay retry end end end |
#register ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/logstash/outputs/mongodb.rb', line 50 def register Mongo::Logger.logger = @logger conn = Mongo::Client.new(@uri) @db = conn.use(@database) if @bulk_size > 1000 raise LogStash::ConfigurationError, "Bulk size must be lower than '1000', currently '#{@bulk_size}'" end @documents = {} Thread.new do loop do sleep @bulk_interval @@mutex.synchronize do @documents.each do |collection, values| if values.length > 0 @db[collection].insert_many(values) @documents.delete(collection) end end end end end end |