class MongoActiveInstrumentation::LogSubscriber

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method
# File lib/mongo_active_instrumentation/log_subscriber.rb, line 16
def initialize(options = {})
  super()
  @events = {}
end
reset_runtime() click to toggle source
# File lib/mongo_active_instrumentation/log_subscriber.rb, line 11
def self.reset_runtime
  rt, self.runtime = runtime, 0
  rt
end
runtime() click to toggle source
# File lib/mongo_active_instrumentation/log_subscriber.rb, line 7
def self.runtime
  Thread.current["mongo_runtime"] ||= 0
end
runtime=(value) click to toggle source
# File lib/mongo_active_instrumentation/log_subscriber.rb, line 3
def self.runtime=(value)
  Thread.current["mongo_runtime"] = value
end

Public Instance Methods

failed(event) click to toggle source
# File lib/mongo_active_instrumentation/log_subscriber.rb, line 31
def failed(event)
  return unless logger.debug?
  completed event, RED
end
started(event) click to toggle source
# File lib/mongo_active_instrumentation/log_subscriber.rb, line 21
def started(event)
  return unless logger.debug?
  @events[event.request_id] = event
end
succeeded(event) click to toggle source
# File lib/mongo_active_instrumentation/log_subscriber.rb, line 26
def succeeded(event)
  return unless logger.debug?
  completed event, CYAN
end

Private Instance Methods

command_color(command_name) click to toggle source
# File lib/mongo_active_instrumentation/log_subscriber.rb, line 52
def command_color(command_name)
  case command_name
  when 'find', 'count', 'aggregate'
    BLUE
  when 'insert'
    GREEN
  when 'update'
    YELLOW
  when 'delete'
    RED
  else
    MAGENTA
  end
end
completed(event, name_color) click to toggle source
# File lib/mongo_active_instrumentation/log_subscriber.rb, line 38
def completed(event, name_color)
  started_event = @events.delete(event.request_id)
  if started_event
    duration = event.duration * 1000
    self.class.runtime += duration
    command_name = started_event.command_name.to_s
    name  = "Mongo (#{(duration).round(1)}ms)"
    name  = color(name, name_color, true)
    command = event.respond_to?(:message) ? event.message : started_event.command.inspect
    command = color(command, command_color(command_name), true)
    debug "  #{name}  #{command}"
  end
end