123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Monitoring

Relationships & Source Files
Namespace Children
Modules:
Classes:
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Subscribable, Id
Inherits: Object
Defined in: lib/mongo/monitoring.rb,
lib/mongo/monitoring/cmap_log_subscriber.rb,
lib/mongo/monitoring/command_log_subscriber.rb,
lib/mongo/monitoring/publishable.rb,
lib/mongo/monitoring/sdam_log_subscriber.rb,
lib/mongo/monitoring/server_closed_log_subscriber.rb,
lib/mongo/monitoring/server_description_changed_log_subscriber.rb,
lib/mongo/monitoring/server_opening_log_subscriber.rb,
lib/mongo/monitoring/topology_changed_log_subscriber.rb,
lib/mongo/monitoring/topology_closed_log_subscriber.rb,
lib/mongo/monitoring/topology_opening_log_subscriber.rb,
lib/mongo/monitoring/unified_sdam_log_subscriber.rb,
lib/mongo/monitoring/event/command_failed.rb,
lib/mongo/monitoring/event/command_started.rb,
lib/mongo/monitoring/event/command_succeeded.rb,
lib/mongo/monitoring/event/secure.rb,
lib/mongo/monitoring/event/server_closed.rb,
lib/mongo/monitoring/event/server_description_changed.rb,
lib/mongo/monitoring/event/server_heartbeat_failed.rb,
lib/mongo/monitoring/event/server_heartbeat_started.rb,
lib/mongo/monitoring/event/server_heartbeat_succeeded.rb,
lib/mongo/monitoring/event/server_opening.rb,
lib/mongo/monitoring/event/topology_changed.rb,
lib/mongo/monitoring/event/topology_closed.rb,
lib/mongo/monitoring/event/topology_opening.rb,
lib/mongo/monitoring/event/cmap/base.rb,
lib/mongo/monitoring/event/cmap/connection_check_out_failed.rb,
lib/mongo/monitoring/event/cmap/connection_check_out_started.rb,
lib/mongo/monitoring/event/cmap/connection_checked_in.rb,
lib/mongo/monitoring/event/cmap/connection_checked_out.rb,
lib/mongo/monitoring/event/cmap/connection_closed.rb,
lib/mongo/monitoring/event/cmap/connection_created.rb,
lib/mongo/monitoring/event/cmap/connection_ready.rb,
lib/mongo/monitoring/event/cmap/pool_cleared.rb,
lib/mongo/monitoring/event/cmap/pool_closed.rb,
lib/mongo/monitoring/event/cmap/pool_created.rb,
lib/mongo/monitoring/event/cmap/pool_ready.rb

Overview

The class defines behavior for the performance monitoring API.

Since:

  • 2.1.0

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Subscribable - Included

#subscribe

Subscribe a listener to an event topic.

#subscribers

Get all the subscribers.

#subscribers?

Determine if there are any subscribers for a particular event.

#unsubscribe

Unsubscribe a listener from an event topic.

#subscribers_for

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

[ GitHub ]

  
# File 'lib/mongo/monitoring.rb', line 79

def self.next_operation_id
  self.next_id
end

Instance Attribute Details

#monitoring?Boolean (readonly)

This method is for internal use only.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/monitoring.rb', line 245

def monitoring?
  options[:monitoring] != false
end

#options (readonly)

This method is for internal use only.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/monitoring.rb', line 242

attr_reader :options

Instance Method Details

#failed(topic, event)

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.

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

[ GitHub ]

  
# File 'lib/mongo/monitoring.rb', line 306

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

#initialize_copy(original) (private)

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/monitoring.rb', line 359

def initialize_copy(original)
  @subscribers = {}
  original.subscribers.each do |k, v|
    @subscribers[k] = v.dup
  end
end

#publish_heartbeat(server, awaited: false)

This method is for internal use only.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/monitoring.rb', line 311

def publish_heartbeat(server, awaited: false)
  if monitoring?
    started_event = Event::ServerHeartbeatStarted.new(
      server.address, awaited: awaited)
    started(SERVER_HEARTBEAT, started_event)
  end

  # The duration we publish in heartbeat succeeded/failed events is
  # the time spent on the entire heartbeat. This could include time
  # to connect the socket (including TLS handshake), not just time
  # spent on hello call itself.
  # The spec at https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring-monitoring.rst
  # requires that the duration exposed here start from "sending the
  # message" (hello). This requirement does not make sense if,
  # for example, we were never able to connect to the server at all
  # and thus hello was never sent.
  start_time = Utils.monotonic_time

  begin
    result = yield
  rescue => exc
    if monitoring?
      event = Event::ServerHeartbeatFailed.new(
        server.address,
        Utils.monotonic_time - start_time,
        exc,
        awaited: awaited,
        started_event: started_event,
      )
      failed(SERVER_HEARTBEAT, event)
    end
    raise
  else
    if monitoring?
      event = Event::ServerHeartbeatSucceeded.new(
        server.address,
        Utils.monotonic_time - start_time,
        awaited: awaited,
        started_event: started_event,
      )
      succeeded(SERVER_HEARTBEAT, event)
    end
    result
  end
end

#published(topic, event)

Publish an event.

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

Parameters:

  • topic (String)

    The event topic.

  • event (Event)

    The event to publish.

Since:

  • 2.9.0

[ GitHub ]

  
# File 'lib/mongo/monitoring.rb', line 258

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

#started(topic, event)

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.

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

[ GitHub ]

  
# File 'lib/mongo/monitoring.rb', line 274

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

#succeeded(topic, event)

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.

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

[ GitHub ]

  
# File 'lib/mongo/monitoring.rb', line 290

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