Class: Mongo::Monitoring
Overview
The class defines behavior for the performance monitoring API.
Constant Summary
-
COMMAND =
The command topic.
'Command'.freeze
-
CONNECTION_POOL =
The connection pool topic.
'ConnectionPool'.freeze
-
SERVER_CLOSED =
Server
closed topic.'ServerClosed'.freeze
-
SERVER_DESCRIPTION_CHANGED =
Server
description changed topic.'ServerDescriptionChanged'.freeze
-
SERVER_HEARTBEAT =
Server
heartbeat started topic.'ServerHeartbeat'.freeze
-
SERVER_OPENING =
Server
opening topic.'ServerOpening'.freeze
-
TOPOLOGY_CHANGED =
Topology changed topic.
'TopologyChanged'.freeze
-
TOPOLOGY_CLOSED =
Topology closed topic.
'TopologyClosed'.freeze
-
TOPOLOGY_OPENING =
Topology opening topic.
'TopologyOpening'.freeze
Class Method Summary
-
.new(options = {}) ⇒ Monitoring
constructor
Internal use only
Internal use only
Initialize the monitoring.
-
.next_operation_id ⇒ Integer
Used for generating unique operation ids to link events together.
Instance Attribute Summary
- #monitoring? ⇒ Boolean readonly Internal use only Internal use only
- #options readonly Internal use only Internal use only
Instance Method Summary
-
#failed(topic, event)
Publish a failed event.
- #publish_heartbeat(server, awaited: false) Internal use only Internal use only
-
#published(topic, event)
Publish an event.
-
#started(topic, event)
Publish a started event.
-
#succeeded(topic, event)
Publish a succeeded event.
- #initialize_copy(original) private
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_id ⇒ Integer
Used for generating unique operation ids to link events together.
# File 'lib/mongo/monitoring.rb', line 79
def self.next_operation_id self.next_id end
Instance Attribute Details
#monitoring? ⇒ Boolean
(readonly)
# File 'lib/mongo/monitoring.rb', line 245
def monitoring? [:monitoring] != false end
#options (readonly)
# File 'lib/mongo/monitoring.rb', line 242
attr_reader :
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.
# File 'lib/mongo/monitoring.rb', line 306
def failed(topic, event) subscribers_for(topic).each{ |subscriber| subscriber.failed(event) } end
#initialize_copy(original) (private)
# 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)
# 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-logging-and-monitoring.md # 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.
# 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.
# 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.
# File 'lib/mongo/monitoring.rb', line 290
def succeeded(topic, event) subscribers_for(topic).each{ |subscriber| subscriber.succeeded(event) } end