Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
62 lines (49 sloc) 1.74 KB
require 'hodel_3000_compliant_logger'
require 'oink/utils/hash_utils'
require 'oink/instrumentation'
module Oink
class Middleware
def initialize(app, options = {})
@app = app
@logger = options[:logger] || Hodel3000CompliantLogger.new("log/oink.log")
@instruments = options[:instruments] ? Array(options[:instruments]) : [:memory, :activerecord]
ActiveRecord::Base.send(:include, Oink::Instrumentation::ActiveRecord) if @instruments.include?(:activerecord)
end
def call(env)
status, headers, body = @app.call(env)
log_routing(env)
log_memory
log_activerecord
log_completed
[status, headers, body]
end
def log_completed
@logger.info("Oink Log Entry Complete")
end
def log_routing(env)
if env.has_key?('action_dispatch.request.parameters')
controller = env['action_dispatch.request.parameters']['controller']
action = env['action_dispatch.request.parameters']['action']
@logger.info("Oink Action: #{controller}##{action}")
end
end
def log_memory
if @instruments.include?(:memory)
memory = Oink::Instrumentation::MemorySnapshot.memory
@logger.info("Memory usage: #{memory} | PID: #{$$}")
end
end
def log_activerecord
if @instruments.include?(:activerecord)
sorted_list = Oink::HashUtils.to_sorted_array(ActiveRecord::Base.instantiated_hash)
sorted_list.unshift("Total: #{ActiveRecord::Base.total_objects_instantiated}")
@logger.info("Instantiation Breakdown: #{sorted_list.join(' | ')}")
reset_objects_instantiated
end
end
private
def reset_objects_instantiated
ActiveRecord::Base.reset_instance_type_count
end
end
end
Something went wrong with that request. Please try again.