lib/mongo/monitoring/publishable.rb in mongo-2.1.0.beta vs lib/mongo/monitoring/publishable.rb in mongo-2.1.0.rc0
- old
+ new
@@ -35,18 +35,24 @@
# @return [ Object ] The result of the yield.
#
# @since 2.1.0
def publish_command(messages, operation_id = Monitoring.next_operation_id)
start = Time.now
- payload = messages.first.payload
+ 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)
- command_completed(result, address, operation_id, payload, start)
+ total_duration = duration(receive_start) + send_duration
+ command_completed(result, address, operation_id, payload, total_duration)
result
rescue Exception => e
- command_failed(address, operation_id, payload, e.message, start)
+ total_duration = duration(receive_start) + send_duration
+ command_failed(address, operation_id, payload, e.message, total_duration)
raise e
end
end
private
@@ -56,41 +62,45 @@
Monitoring::COMMAND,
Event::CommandStarted.generate(address, operation_id, payload)
)
end
- def command_completed(result, address, operation_id, payload, start)
+ def command_completed(result, address, operation_id, payload, duration)
document = result ? (result.documents || []).first : nil
- parser = Error::Parser.new(document)
- if parser.message.empty?
- command_succeeded(result, address, operation_id, payload, start)
+ if error?(document)
+ parser = Error::Parser.new(document)
+ command_failed(address, operation_id, payload, parser.message, duration)
else
- command_failed(address, operation_id, payload, parser.message, start)
+ command_succeeded(result, address, operation_id, payload, duration)
end
end
- def command_succeeded(result, address, operation_id, payload, start)
+ def command_succeeded(result, address, operation_id, payload, duration)
monitoring.succeeded(
Monitoring::COMMAND,
Event::CommandSucceeded.generate(
address,
operation_id,
payload,
result ? result.payload : nil,
- duration(start)
+ duration
)
)
end
- def command_failed(address, operation_id, payload, message, start)
+ def command_failed(address, operation_id, payload, message, duration)
monitoring.failed(
Monitoring::COMMAND,
- Event::CommandFailed.generate(address, operation_id, payload, message, duration(start))
+ Event::CommandFailed.generate(address, operation_id, payload, message, duration)
)
end
def duration(start)
Time.now - start
+ end
+
+ def error?(document)
+ document && (document['ok'] == 0 || document.key?('$err'))
end
end
end
end