Class: LogStash::Outputs::Mongodb

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

Constant Summary

Constants included from Config::Mixin

Config::Mixin::CONFIGSORT

Instance Attribute Summary

Attributes included from Config::Mixin

#config, #original_params

Attributes inherited from Plugin

#logger, #params

Instance Method Summary collapse

Methods inherited from Base

#handle, #handle_worker, #initialize, #worker_setup, #workers_not_supported

Methods included from Config::Mixin

#config_init, included

Methods inherited from Plugin

#eql?, #finished, #finished?, #hash, #initialize, #inspect, lookup, #reload, #running?, #shutdown, #teardown, #terminating?, #to_s

Constructor Details

This class inherits a constructor from LogStash::Outputs::Base

Instance Method Details

#receive(event) ⇒ Object



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
# File 'lib/logstash/outputs/mongodb.rb', line 51

def receive(event)
  return unless output?(event)

  begin
    if @isodate
      # the mongodb driver wants time values as a ruby Time object.
      # set the @timestamp value of the document to a ruby Time object, then.
      document = event.to_hash
    else
      document = event.to_hash.merge("@timestamp" => event["@timestamp"].to_json)
    end
    if @generateId
      document['_id'] = BSON::ObjectId.new(nil, event["@timestamp"])
    end
    @db.collection(event.sprintf(@collection)).insert(document)
  rescue => e
    @logger.warn("Failed to send event to MongoDB", :event => event, :exception => e,
                 :backtrace => e.backtrace)
    if e.error_code == 11000
        # 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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/logstash/outputs/mongodb.rb', line 35

def register
  require "mongo"
  uriParsed=Mongo::URIParser.new(@uri)
  conn = uriParsed.connection({})
  if uriParsed.auths.length > 0
    uriParsed.auths.each do |auth|
      if !auth['db_name'].nil?
        conn.add_auth(auth['db_name'], auth['username'], auth['password'], nil)
      end 
    end
    conn.apply_saved_authentication()
  end
  @db = conn.db(@database)
end