class Mongo::Monitoring

The class defines behavior for the performance monitoring API.

@since 2.1.0

Constants

COMMAND

The command topic.

@since 2.1.0

CONNECTION_POOL

The connection pool topic.

@since 2.9.0

SERVER_CLOSED

Server closed topic.

@since 2.4.0

SERVER_DESCRIPTION_CHANGED

Server description changed topic.

@since 2.4.0

SERVER_HEARTBEAT

Server heartbeat started topic.

@since 2.7.0

SERVER_OPENING

Server opening topic.

@since 2.4.0

TOPOLOGY_CHANGED

Topology changed topic.

@since 2.4.0

TOPOLOGY_CLOSED

Topology closed topic.

@since 2.4.0

TOPOLOGY_OPENING

Topology opening topic.

@since 2.4.0

Attributes

options[R]

@api private

Public Class Methods

new(options = {}) click to toggle source

Initialize the monitoring.

@example Create the new monitoring.

Monitoring.new(:monitoring => true)

@param [ Hash ] options Options. Client constructor forwards its

options to Monitoring constructor, although Monitoring recognizes
only a subset of the options recognized by Client.

@option options [ true, false ] :monitoring If false is given, the

Monitoring instance is initialized without global monitoring event
subscribers and will not publish SDAM events. Command monitoring events
will still be published, and the driver will still perform SDAM and
monitor its cluster in order to perform server selection. Built-in
driver logging of SDAM events will be disabled because it is
implemented through SDAM event subscription. Client#subscribe will
succeed for all event types, but subscribers to SDAM events will
not be invoked. Values other than false result in default behavior
which is to perform normal SDAM event publication.

@since 2.1.0 @api private

# File lib/mongo/monitoring.rb, line 217
def initialize(options = {})
  @options = options
  if options[:monitoring] != false
    Global.subscribers.each do |topic, subscribers|
      subscribers.each do |subscriber|
        subscribe(topic, subscriber)
      end
    end
    subscribe(COMMAND, CommandLogSubscriber.new(options))
    # CMAP events are not logged by default because this will create
    # log entries for every operation performed by the driver.
    #subscribe(CONNECTION_POOL, CmapLogSubscriber.new(options))
    subscribe(SERVER_OPENING, ServerOpeningLogSubscriber.new(options))
    subscribe(SERVER_CLOSED, ServerClosedLogSubscriber.new(options))
    subscribe(SERVER_DESCRIPTION_CHANGED, ServerDescriptionChangedLogSubscriber.new(options))
    subscribe(TOPOLOGY_OPENING, TopologyOpeningLogSubscriber.new(options))
    subscribe(TOPOLOGY_CHANGED, TopologyChangedLogSubscriber.new(options))
    subscribe(TOPOLOGY_CLOSED, TopologyClosedLogSubscriber.new(options))
  end
end
next_operation_id() click to toggle source

Used for generating unique operation ids to link events together.

@example Get the next operation id.

Monitoring.next_operation_id

@return [ Integer ] The next operation id.

@since 2.1.0

# File lib/mongo/monitoring.rb, line 76
def self.next_operation_id
  self.next_id
end

Public Instance Methods

failed(topic, event) click to toggle source

Publish a failed event.

This method is used for event types which have the started/succeeded/failed events in them, such as command and heartbeat events.

@example Publish a failed event.

monitoring.failed(COMMAND, event)

@param [ String ] topic The event topic. @param [ Event ] event The event to publish.

@since 2.1.0

# File lib/mongo/monitoring.rb, line 303
def failed(topic, event)
  subscribers_for(topic).each{ |subscriber| subscriber.failed(event) }
end
monitoring?() click to toggle source

@api private

# File lib/mongo/monitoring.rb, line 242
def monitoring?
  options[:monitoring] != false
end
published(topic, event) click to toggle source

Publish an event.

This method is used for event types which only have a single event in them.

@param [ String ] topic The event topic. @param [ Event ] event The event to publish.

@since 2.9.0

# File lib/mongo/monitoring.rb, line 255
def published(topic, event)
  subscribers_for(topic).each{ |subscriber| subscriber.published(event) }
end
started(topic, event) click to toggle source

Publish a started event.

This method is used for event types which have the started/succeeded/failed events in them, such as command and heartbeat events.

@example Publish a started event.

monitoring.started(COMMAND, event)

@param [ String ] topic The event topic. @param [ Event ] event The event to publish.

@since 2.1.0

# File lib/mongo/monitoring.rb, line 271
def started(topic, event)
  subscribers_for(topic).each{ |subscriber| subscriber.started(event) }
end
succeeded(topic, event) click to toggle source

Publish a succeeded event.

This method is used for event types which have the started/succeeded/failed events in them, such as command and heartbeat events.

@example Publish a succeeded event.

monitoring.succeeded(COMMAND, event)

@param [ String ] topic The event topic. @param [ Event ] event The event to publish.

@since 2.1.0

# File lib/mongo/monitoring.rb, line 287
def succeeded(topic, event)
  subscribers_for(topic).each{ |subscriber| subscriber.succeeded(event) }
end

Private Instance Methods

initialize_copy(original) click to toggle source
# File lib/mongo/monitoring.rb, line 309
def initialize_copy(original)
  @subscribers = {}
  original.subscribers.each do |k, v|
    @subscribers[k] = v.dup
  end
end