Merge pull request #85 from shingara/log_duration
Add a log duration option.
This commit is contained in:
commit
3613a05461
@ -87,6 +87,7 @@ module Mongo
|
|||||||
@db, @name = db, name
|
@db, @name = db, name
|
||||||
@connection = @db.connection
|
@connection = @db.connection
|
||||||
@logger = @connection.logger
|
@logger = @connection.logger
|
||||||
|
@log_duration = @connection.log_duration
|
||||||
@cache_time = @db.cache_time
|
@cache_time = @db.cache_time
|
||||||
@cache = Hash.new(0)
|
@cache = Hash.new(0)
|
||||||
unless pk_factory
|
unless pk_factory
|
||||||
|
@ -36,7 +36,7 @@ module Mongo
|
|||||||
|
|
||||||
mongo_thread_local_accessor :connections
|
mongo_thread_local_accessor :connections
|
||||||
|
|
||||||
attr_reader :logger, :size, :auths, :primary, :safe, :host_to_try,
|
attr_reader :logger, :log_duration, :size, :auths, :primary, :safe, :host_to_try,
|
||||||
:pool_size, :connect_timeout, :pool_timeout,
|
:pool_size, :connect_timeout, :pool_timeout,
|
||||||
:primary_pool, :socket_class, :op_timeout
|
:primary_pool, :socket_class, :op_timeout
|
||||||
|
|
||||||
@ -63,6 +63,8 @@ module Mongo
|
|||||||
# to a single, slave node.
|
# to a single, slave node.
|
||||||
# @option opts [Logger, #debug] :logger (nil) A Logger instance for debugging driver ops. Note that
|
# @option opts [Logger, #debug] :logger (nil) A Logger instance for debugging driver ops. Note that
|
||||||
# logging negatively impacts performance; therefore, it should not be used for high-performance apps.
|
# logging negatively impacts performance; therefore, it should not be used for high-performance apps.
|
||||||
|
# @option opts [Boolean, #debug] :log_duration (nil) A boolean to say if you want see information about log_duration on logger
|
||||||
|
# like logging, log_duration negatively impacts performance; therefore, it should not be used for high-performance apps.
|
||||||
# @option opts [Integer] :pool_size (1) The maximum number of socket self.connections allowed per
|
# @option opts [Integer] :pool_size (1) The maximum number of socket self.connections allowed per
|
||||||
# connection pool. Note: this setting is relevant only for multi-threaded applications.
|
# connection pool. Note: this setting is relevant only for multi-threaded applications.
|
||||||
# @option opts [Float] :pool_timeout (5.0) When all of the self.connections a pool are checked out,
|
# @option opts [Float] :pool_timeout (5.0) When all of the self.connections a pool are checked out,
|
||||||
@ -568,6 +570,7 @@ module Mongo
|
|||||||
|
|
||||||
if @logger
|
if @logger
|
||||||
write_logging_startup_message
|
write_logging_startup_message
|
||||||
|
@log_duration = opts[:log_duration] || false
|
||||||
end
|
end
|
||||||
|
|
||||||
should_connect = opts.fetch(:connect, true)
|
should_connect = opts.fetch(:connect, true)
|
||||||
|
@ -43,6 +43,7 @@ module Mongo
|
|||||||
@collection = collection
|
@collection = collection
|
||||||
@connection = @db.connection
|
@connection = @db.connection
|
||||||
@logger = @connection.logger
|
@logger = @connection.logger
|
||||||
|
@log_duration = @connection.log_duration
|
||||||
|
|
||||||
# Query selector
|
# Query selector
|
||||||
@selector = opts[:selector] || {}
|
@selector = opts[:selector] || {}
|
||||||
|
@ -25,14 +25,15 @@ module Mongo
|
|||||||
|
|
||||||
# Execute the block and log the operation described by name and payload.
|
# Execute the block and log the operation described by name and payload.
|
||||||
def instrument(name, payload = {}, &blk)
|
def instrument(name, payload = {}, &blk)
|
||||||
|
time_elapse = @log_duration ? Time.now : nil
|
||||||
res = yield
|
res = yield
|
||||||
log_operation(name, payload)
|
log_operation(name, payload, time_elapse)
|
||||||
res
|
res
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def log_operation(name, payload)
|
def log_operation(name, payload, time_elapse=nil)
|
||||||
@logger ||= nil
|
@logger ||= nil
|
||||||
return unless @logger
|
return unless @logger
|
||||||
msg = "#{payload[:database]}['#{payload[:collection]}'].#{name}("
|
msg = "#{payload[:database]}['#{payload[:collection]}'].#{name}("
|
||||||
@ -40,8 +41,12 @@ module Mongo
|
|||||||
msg += ".skip(#{payload[:skip]})" if payload[:skip]
|
msg += ".skip(#{payload[:skip]})" if payload[:skip]
|
||||||
msg += ".limit(#{payload[:limit]})" if payload[:limit]
|
msg += ".limit(#{payload[:limit]})" if payload[:limit]
|
||||||
msg += ".sort(#{payload[:order]})" if payload[:order]
|
msg += ".sort(#{payload[:order]})" if payload[:order]
|
||||||
|
if time_elapse
|
||||||
|
@logger.debug "MONGODB (#{Time.now - time_elapse}s) #{msg}"
|
||||||
|
else
|
||||||
@logger.debug "MONGODB #{msg}"
|
@logger.debug "MONGODB #{msg}"
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -137,6 +137,15 @@ class TestConnection < Test::Unit::TestCase
|
|||||||
assert output.string.include?("admin['$cmd'].find")
|
assert output.string.include?("admin['$cmd'].find")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_logging_duration
|
||||||
|
output = StringIO.new
|
||||||
|
logger = Logger.new(output)
|
||||||
|
logger.level = Logger::DEBUG
|
||||||
|
connection = standard_connection(:logger => logger, :log_duration => true).db(MONGO_TEST_DB)
|
||||||
|
assert output.string.index(/\(0.\d+s\)/)
|
||||||
|
assert output.string.include?("admin['$cmd'].find")
|
||||||
|
end
|
||||||
|
|
||||||
def test_connection_logger
|
def test_connection_logger
|
||||||
output = StringIO.new
|
output = StringIO.new
|
||||||
logger = Logger.new(output)
|
logger = Logger.new(output)
|
||||||
@ -148,6 +157,21 @@ class TestConnection < Test::Unit::TestCase
|
|||||||
assert output.string.include?('testing')
|
assert output.string.include?('testing')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_connection_log_duration_with_logger
|
||||||
|
output = StringIO.new
|
||||||
|
logger = Logger.new(output)
|
||||||
|
logger.level = Logger::DEBUG
|
||||||
|
connection = standard_connection(:logger => logger, :log_duration => true)
|
||||||
|
assert logger, connection.logger
|
||||||
|
assert connection.log_duration
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_connection_log_duration_without_logger
|
||||||
|
connection = standard_connection(:log_duration => true)
|
||||||
|
assert_nil connection.logger
|
||||||
|
assert !connection.log_duration
|
||||||
|
end
|
||||||
|
|
||||||
def test_drop_database
|
def test_drop_database
|
||||||
db = @conn.db('ruby-mongo-will-be-deleted')
|
db = @conn.db('ruby-mongo-will-be-deleted')
|
||||||
coll = db.collection('temp')
|
coll = db.collection('temp')
|
||||||
|
Loading…
Reference in New Issue
Block a user