Class: Mongo::Monitoring

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/monitoring.rb,
lib/mongo/monitoring/publishable.rb,
lib/mongo/monitoring/event/secure.rb,
lib/mongo/monitoring/event/command_failed.rb,
lib/mongo/monitoring/event/command_started.rb,
lib/mongo/monitoring/command_log_subscriber.rb,
lib/mongo/monitoring/event/command_succeeded.rb

Overview

The class defines behaviour for the performance monitoring API.

Since:

  • 2.1.0

Defined Under Namespace

Modules: Event, Global, Publishable Classes: CommandLogSubscriber

Constant Summary collapse

COMMAND =

The command topic.

Since:

  • 2.1.0

'Command'.freeze
@@operation_id =

Since:

  • 2.1.0

0
@@operation_id_lock =

Since:

  • 2.1.0

Mutex.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Monitoring

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize the monitoring.

Examples:

Create the new monitoring.

Monitoring.new(:monitoring => true)

Parameters:

  • options (Hash) (defaults to: {})

    The options.

Since:

  • 2.1.0



96
97
98
99
100
101
102
103
104
105
# File 'lib/mongo/monitoring.rb', line 96

def initialize(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))
  end
end

Class Method Details

.next_operation_idInteger

Used for generating unique operation ids to link events together.

Examples:

Get the next operation id.

Monitoring.next_operation_id

Returns:

  • (Integer)

    The next operation id.

Since:

  • 2.1.0



42
43
44
45
46
# File 'lib/mongo/monitoring.rb', line 42

def self.next_operation_id
  @@operation_id_lock.synchronize do
    @@operation_id += 1
  end
end

Instance Method Details

#failed(topic, event) ⇒ Object

Publish a failed event.

Examples:

Publish a failed event.

monitoring.failed(COMMAND, event)

Parameters:

  • topic (String)

    The event topic.

  • event (Event)

    The event to publish.

Since:

  • 2.1.0



142
143
144
# File 'lib/mongo/monitoring.rb', line 142

def failed(topic, event)
  subscribers_for(topic).each{ |subscriber| subscriber.failed(event) }
end

#started(topic, event) ⇒ Object

Publish a started event.

Examples:

Publish a started event.

monitoring.started(COMMAND, event)

Parameters:

  • topic (String)

    The event topic.

  • event (Event)

    The event to publish.

Since:

  • 2.1.0



116
117
118
# File 'lib/mongo/monitoring.rb', line 116

def started(topic, event)
  subscribers_for(topic).each{ |subscriber| subscriber.started(event) }
end

#subscribe(topic, subscriber) ⇒ Object

Subscribe a listener to an event topic.

Examples:

Subscribe to the topic.

monitoring.subscribe(QUERY, subscriber)

Parameters:

  • topic (String)

    The event topic.

  • subscriber (Object)

    The subscriber to handle the event.

Since:

  • 2.1.0



155
156
157
# File 'lib/mongo/monitoring.rb', line 155

def subscribe(topic, subscriber)
  subscribers_for(topic).push(subscriber)
end

#subscribersHash<String, Object>

Get all the subscribers.

Examples:

Get all the subscribers.

monitoring.subscribers

Returns:

  • (Hash<String, Object>)

    The subscribers.

Since:

  • 2.1.0



167
168
169
# File 'lib/mongo/monitoring.rb', line 167

def subscribers
  @subscribers ||= {}
end

#subscribers?(topic) ⇒ true, false

Determine if there are any subscribers for a particular event.

Examples:

Are there subscribers?

monitoring.subscribers?(COMMAND)

Parameters:

  • topic (String)

    The event topic.

Returns:

  • (true, false)

    If there are subscribers for the topic.

Since:

  • 2.1.0



181
182
183
# File 'lib/mongo/monitoring.rb', line 181

def subscribers?(topic)
  !subscribers_for(topic).empty?
end

#succeeded(topic, event) ⇒ Object

Publish a succeeded event.

Examples:

Publish a succeeded event.

monitoring.succeeded(COMMAND, event)

Parameters:

  • topic (String)

    The event topic.

  • event (Event)

    The event to publish.

Since:

  • 2.1.0



129
130
131
# File 'lib/mongo/monitoring.rb', line 129

def succeeded(topic, event)
  subscribers_for(topic).each{ |subscriber| subscriber.succeeded(event) }
end