Module: Mongo::Monitoring::Publishable

Overview

Defines behaviour for an object that can publish monitoring events.

Since:

  • 2.1.0

Constant Summary

Constants included from Loggable

Loggable::PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger

Instance Attribute Details

#monitoringMonitoring (readonly)

Returns monitoring The monitoring.

Returns:

Since:

  • 2.1.0



25
26
27
# File 'lib/mongo/monitoring/publishable.rb', line 25

def monitoring
  @monitoring
end

Instance Method Details

#publish_command(messages, operation_id = Monitoring.next_operation_id) ⇒ Object

Publish a command event to the global monitoring.

Examples:

Publish a command event.

publish_command do |messages|
  # ...
end

Parameters:

  • messages (Array<Message>)

    The messages.

Returns:

  • (Object)

    The result of the yield.

Since:

  • 2.1.0



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mongo/monitoring/publishable.rb', line 39

def publish_command(messages, operation_id = Monitoring.next_operation_id)
  start = Time.now
  message = messages.first
  message.set_request_id
  payload = message.payload
  send_duration = duration(start)
  command_started(address, operation_id, payload)
  receive_start = Time.now
  begin
    result = yield(messages)
    total_duration = duration(receive_start) + send_duration
    command_completed(result, address, operation_id, payload, total_duration)
    result
  rescue Exception => e
    total_duration = duration(receive_start) + send_duration
    command_failed(nil, address, operation_id, payload, e.message, total_duration)
    raise e
  end
end

#publish_event(topic, event) ⇒ Object

Since:

  • 2.1.0



59
60
61
# File 'lib/mongo/monitoring/publishable.rb', line 59

def publish_event(topic, event)
  monitoring.succeeded(topic, event)
end

#publish_sdam_event(topic, event) ⇒ Object

Since:

  • 2.1.0



63
64
65
66
67
68
# File 'lib/mongo/monitoring/publishable.rb', line 63

def publish_sdam_event(topic, event)
  return unless monitoring?

  log_debug("EVENT: #{event.inspect}")
  monitoring.succeeded(topic, event)
end