Class: LogStash::Outputs::Mongo
- Inherits:
-
Base
- Object
- Base
- LogStash::Outputs::Mongo
- Defined in:
- lib/logstash/outputs/mongo.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
Instance Method Details
#buildStatement(document) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/logstash/outputs/mongo.rb', line 101 def buildStatement(document) statement = {} @attributes.each do |attr| # filter attributes for not null validation if document[attr] != nil statement[attr] = document[attr] end end return statement end |
#close ⇒ Object
116 117 118 |
# File 'lib/logstash/outputs/mongo.rb', line 116 def close @closed.make_true end |
#receive(event) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 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 |
# File 'lib/logstash/outputs/mongo.rb', line 50 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) @logger.debug("Type=[#{@type}] Collection=[#{@collection}] _id=[#{document["_id"]}]") case @type when "upsert" statement = buildStatement(document) @db[event.sprintf(@collection)].update_one( { '_id' => document["_id"]}, { '$set' => statement}, :upsert => true ) when "update" statement = buildStatement(document) @db[event.sprintf(@collection)].update_one( { '_id' => document["_id"]}, { '$set' => statement} ) when "insert" @db[event.sprintf(@collection)].insert_one(document) when "delete" @db[event.sprintf(@collection)].delete_one({ '_id' => document["_id"]}) end rescue => e 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. @logger.warn("Skipping insert because of a duplicate key error", :event => event, :exception => e) else @logger.warn("Failed to send event to MongoDB, retrying in #{@retry_delay.to_s} seconds", :event => event, :exception => e) sleep(@retry_delay) retry end end end |