Refactored logging of DB operations to use Connection#instrument.
This allows for easy overriding, e.g. to ActiveSupport notifications.
This commit is contained in:
parent
7c4740c47c
commit
8a7296599b
@ -79,7 +79,6 @@ module Mongo
|
|||||||
|
|
||||||
@db, @name = db, name
|
@db, @name = db, name
|
||||||
@connection = @db.connection
|
@connection = @db.connection
|
||||||
@logger = @connection.logger
|
|
||||||
@cache_time = @db.cache_time
|
@cache_time = @db.cache_time
|
||||||
@cache = Hash.new(0)
|
@cache = Hash.new(0)
|
||||||
unless pk_factory
|
unless pk_factory
|
||||||
@ -322,12 +321,13 @@ module Mongo
|
|||||||
message.put_int(0)
|
message.put_int(0)
|
||||||
message.put_binary(BSON::BSON_CODER.serialize(selector, false, true).to_s)
|
message.put_binary(BSON::BSON_CODER.serialize(selector, false, true).to_s)
|
||||||
|
|
||||||
@logger.debug("MONGODB #{@db.name}['#{@name}'].remove(#{selector.inspect})") if @logger
|
@connection.instrument( :remove, :database => @db.name, :collection => @name, :selector => selector ) do
|
||||||
if safe
|
if safe
|
||||||
@connection.send_message_with_safe_check(Mongo::Constants::OP_DELETE, message, @db.name, nil, safe)
|
@connection.send_message_with_safe_check(Mongo::Constants::OP_DELETE, message, @db.name, nil, safe)
|
||||||
else
|
else
|
||||||
@connection.send_message(Mongo::Constants::OP_DELETE, message)
|
@connection.send_message(Mongo::Constants::OP_DELETE, message)
|
||||||
true
|
true
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -367,11 +367,13 @@ module Mongo
|
|||||||
message.put_int(update_options)
|
message.put_int(update_options)
|
||||||
message.put_binary(BSON::BSON_CODER.serialize(selector, false, true).to_s)
|
message.put_binary(BSON::BSON_CODER.serialize(selector, false, true).to_s)
|
||||||
message.put_binary(BSON::BSON_CODER.serialize(document, false, true).to_s)
|
message.put_binary(BSON::BSON_CODER.serialize(document, false, true).to_s)
|
||||||
@logger.debug("MONGODB #{@db.name}['#{@name}'].update(#{selector.inspect}, #{document.inspect})") if @logger
|
|
||||||
if safe
|
@connection.instrument( :update, :database => @db.name, :collection => @name, :selector => selector, :document => document ) do
|
||||||
@connection.send_message_with_safe_check(Mongo::Constants::OP_UPDATE, message, @db.name, nil, safe)
|
if safe
|
||||||
else
|
@connection.send_message_with_safe_check(Mongo::Constants::OP_UPDATE, message, @db.name, nil, safe)
|
||||||
@connection.send_message(Mongo::Constants::OP_UPDATE, message, nil)
|
else
|
||||||
|
@connection.send_message(Mongo::Constants::OP_UPDATE, message, nil)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -838,11 +840,12 @@ module Mongo
|
|||||||
end
|
end
|
||||||
raise InvalidOperation, "Exceded maximum insert size of 16,000,000 bytes" if message.size > 16_000_000
|
raise InvalidOperation, "Exceded maximum insert size of 16,000,000 bytes" if message.size > 16_000_000
|
||||||
|
|
||||||
@logger.debug("MONGODB #{@db.name}['#{collection_name}'].insert(#{documents.inspect})") if @logger
|
@connection.instrument( :insert, :database => @db.name, :collection => collection_name, :documents => documents ) do
|
||||||
if safe
|
if safe
|
||||||
@connection.send_message_with_safe_check(Mongo::Constants::OP_INSERT, message, @db.name, nil, safe)
|
@connection.send_message_with_safe_check(Mongo::Constants::OP_INSERT, message, @db.name, nil, safe)
|
||||||
else
|
else
|
||||||
@connection.send_message(Mongo::Constants::OP_INSERT, message, nil)
|
@connection.send_message(Mongo::Constants::OP_INSERT, message, nil)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
documents.collect { |o| o[:_id] || o['_id'] }
|
documents.collect { |o| o[:_id] || o['_id'] }
|
||||||
end
|
end
|
||||||
|
@ -534,6 +534,13 @@ module Mongo
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# execute the block and log the operation as described by name/payload
|
||||||
|
def instrument( name, payload = {}, &blk )
|
||||||
|
res = yield
|
||||||
|
log_operation(name, payload)
|
||||||
|
res
|
||||||
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
# Generic initialization code.
|
# Generic initialization code.
|
||||||
@ -587,6 +594,18 @@ module Mongo
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
## Logging methods
|
||||||
|
|
||||||
|
def log_operation( name, payload )
|
||||||
|
return unless @logger
|
||||||
|
msg = "#{payload[:database]}['#{payload[:collection]}'].#{name}("
|
||||||
|
msg += payload.values_at(:selector, :document, :documents, :fields ).compact.map(&:inspect).join(', ') + ")"
|
||||||
|
msg += ".skip(#{payload[:skip]})" if payload[:skip]
|
||||||
|
msg += ".limit(#{payload[:limit]})" if payload[:limit]
|
||||||
|
msg += ".sort(#{payload[:sort]})" if payload[:sort]
|
||||||
|
@logger.debug "MONGODB #{msg}"
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
## Methods for establishing a connection:
|
## Methods for establishing a connection:
|
||||||
|
@ -378,13 +378,14 @@ module Mongo
|
|||||||
false
|
false
|
||||||
else
|
else
|
||||||
message = construct_query_message
|
message = construct_query_message
|
||||||
@logger.debug query_log_message if @logger
|
@connection.instrument( :find, instrument_payload ) do
|
||||||
results, @n_received, @cursor_id = @connection.receive_message(
|
results, @n_received, @cursor_id = @connection.receive_message(
|
||||||
Mongo::Constants::OP_QUERY, message, nil, @socket, @command)
|
Mongo::Constants::OP_QUERY, message, nil, @socket, @command)
|
||||||
@returned += @n_received
|
@returned += @n_received
|
||||||
@cache += results
|
@cache += results
|
||||||
@query_run = true
|
@query_run = true
|
||||||
close_cursor_if_query_complete
|
close_cursor_if_query_complete
|
||||||
|
end
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -401,10 +402,13 @@ module Mongo
|
|||||||
message
|
message
|
||||||
end
|
end
|
||||||
|
|
||||||
def query_log_message
|
def instrument_payload
|
||||||
"#{@db.name}['#{@collection.name}'].find(#{@selector.inspect}, #{@fields ? @fields.inspect : '{}'})" +
|
log = { :database => @db.name, :collection => @collection.name, :selector => selector }
|
||||||
"#{@skip != 0 ? ('.skip(' + @skip.to_s + ')') : ''}#{@limit != 0 ? ('.limit(' + @limit.to_s + ')') : ''}" +
|
log[:fields] = @fields if @fields
|
||||||
"#{@order ? ('.sort(' + @order.inspect + ')') : ''}"
|
log[:skip] = @skip if @skip && (@skip > 0)
|
||||||
|
log[:limit] = @limit if @limit && (@limit > 0)
|
||||||
|
log[:order] = @order if @order
|
||||||
|
log
|
||||||
end
|
end
|
||||||
|
|
||||||
def construct_query_spec
|
def construct_query_spec
|
||||||
|
@ -14,7 +14,7 @@ class CollectionTest < Test::Unit::TestCase
|
|||||||
@conn.expects(:send_message).with do |op, msg, log|
|
@conn.expects(:send_message).with do |op, msg, log|
|
||||||
op == 2001
|
op == 2001
|
||||||
end
|
end
|
||||||
@logger.stubs(:debug)
|
@conn.stubs(:log_operation)
|
||||||
@coll.update({}, {:title => 'Moby Dick'})
|
@coll.update({}, {:title => 'Moby Dick'})
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -25,8 +25,8 @@ class CollectionTest < Test::Unit::TestCase
|
|||||||
@conn.expects(:send_message).with do |op, msg, log|
|
@conn.expects(:send_message).with do |op, msg, log|
|
||||||
op == 2002
|
op == 2002
|
||||||
end
|
end
|
||||||
@logger.expects(:debug).with do |msg|
|
@conn.expects(:log_operation).with do |name, payload|
|
||||||
msg.include?("Moby")
|
(name == :insert) && payload[:documents][0][:title].include?('Moby')
|
||||||
end
|
end
|
||||||
@coll.insert({:title => 'Moby Dick'})
|
@coll.insert({:title => 'Moby Dick'})
|
||||||
end
|
end
|
||||||
@ -38,8 +38,8 @@ class CollectionTest < Test::Unit::TestCase
|
|||||||
@conn.expects(:receive_message).with do |op, msg, log, sock|
|
@conn.expects(:receive_message).with do |op, msg, log, sock|
|
||||||
op == 2004
|
op == 2004
|
||||||
end.returns([[], 0, 0])
|
end.returns([[], 0, 0])
|
||||||
@logger.expects(:debug).with do |msg|
|
@conn.expects(:log_operation).with do |name, payload|
|
||||||
msg.include?('Moby')
|
(name == :find) && payload[:selector][:title].include?('Moby')
|
||||||
end
|
end
|
||||||
@coll.find({:title => 'Moby Dick'}).sort([['title', 1], ['author', 1]]).next_document
|
@coll.find({:title => 'Moby Dick'}).sort([['title', 1], ['author', 1]]).next_document
|
||||||
end
|
end
|
||||||
@ -52,8 +52,8 @@ class CollectionTest < Test::Unit::TestCase
|
|||||||
@conn.expects(:send_message).with do |op, msg, log|
|
@conn.expects(:send_message).with do |op, msg, log|
|
||||||
op == 2002
|
op == 2002
|
||||||
end
|
end
|
||||||
@logger.expects(:debug).with do |msg|
|
@conn.expects(:log_operation).with do |name, payload|
|
||||||
msg.include?("Binary")
|
(name == :insert) && payload[:documents][0][:data].inspect.include?('Binary')
|
||||||
end
|
end
|
||||||
@coll.insert({:data => data})
|
@coll.insert({:data => data})
|
||||||
end
|
end
|
||||||
@ -65,8 +65,8 @@ class CollectionTest < Test::Unit::TestCase
|
|||||||
@conn.expects(:send_message_with_safe_check).with do |op, msg, db_name, log|
|
@conn.expects(:send_message_with_safe_check).with do |op, msg, db_name, log|
|
||||||
op == 2001
|
op == 2001
|
||||||
end
|
end
|
||||||
@logger.expects(:debug).with do |msg|
|
@conn.expects(:log_operation).with do |name, payload|
|
||||||
msg.include?("testing['books'].update")
|
(name == :update) && payload[:document][:title].include?('Moby')
|
||||||
end
|
end
|
||||||
@coll.update({}, {:title => 'Moby Dick'}, :safe => true)
|
@coll.update({}, {:title => 'Moby Dick'}, :safe => true)
|
||||||
end
|
end
|
||||||
@ -78,7 +78,7 @@ class CollectionTest < Test::Unit::TestCase
|
|||||||
@conn.expects(:send_message_with_safe_check).with do |op, msg, db_name, log|
|
@conn.expects(:send_message_with_safe_check).with do |op, msg, db_name, log|
|
||||||
op == 2001
|
op == 2001
|
||||||
end
|
end
|
||||||
@logger.stubs(:debug)
|
@conn.stubs(:log_operation)
|
||||||
@coll.update({}, {:title => 'Moby Dick'}, :safe => true)
|
@coll.update({}, {:title => 'Moby Dick'}, :safe => true)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user