Class: LogStash::Outputs::Mongodb

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/outputs/mongodb.rb

Overview

require_relative “bson/logstash_event”

Instance Method Summary collapse

Instance Method Details

#receive(event) ⇒ Object

def register



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/logstash/outputs/mongodb.rb', line 44

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.timestamp.to_json
    end
    if @generateId
      document["_id"] = BSON::ObjectId.new(nil, event.timestamp)
    end
    @db[event.sprintf(@collection)].insert_one(document)
  rescue => e
    @logger.warn("Failed to send event to MongoDB", :event => event, :exception => e,
                 :backtrace => e.backtrace)
    if e.message =~ /^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

#registerObject



38
39
40
41
42
# File 'lib/logstash/outputs/mongodb.rb', line 38

def register
  Mongo::Logger.logger = @logger
  conn = Mongo::Client.new(@uri)
  @db = conn.use(@database)
end