Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Minor refactoring of Oink::Middleware internal methods

  • Loading branch information...
commit 9a9140aa40643357f1ce1eaf826a42b6c7ac8c59 1 parent 0ebe10e
Eirik Dentz Sinclair authored
Showing with 34 additions and 25 deletions.
  1. +34 −25 lib/oink/middleware.rb
View
59 lib/oink/middleware.rb
@@ -8,51 +8,60 @@ class Middleware
DEFAULT_LOG_PATH = "log/oink.log"
def initialize(app, options = {})
- @options = options
- @options[:log_path] ||= DEFAULT_LOG_PATH
- @options[:instruments] ||= [:memory, :activerecord]
- ActiveRecord::Base.send(:include, Oink::Instrumentation::ActiveRecord) if instrumented?(:activerecord)
- @logger = Hodel3000CompliantLogger.new(@options[:log_path])
- @app = app
+ @app = app
+ @log_path = options[:log_path] || DEFAULT_LOG_PATH
+ @instruments = 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_information(env)
- @logger.info("Completed in")
- log_memory_snapshot if instrumented?(:memory)
- log_objects_instantiated if instrumented?(:activerecord)
+
+ log_routing(env)
+ log_completed
+ log_memory
+ log_activerecord
+
reset_objects_instantiated
[status, headers, body]
end
- def log_routing_information(env)
+ def log_completed
+ logger.info("Completed in")
+ 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 "Processing #{controller}##{action}"
+ action = env['action_dispatch.request.parameters']['action']
+ logger.info("Processing #{controller}##{action}")
end
end
- def log_memory_snapshot
- memory = Oink::Instrumentation::MemorySnapshot.memory
- @logger.info("Memory usage: #{memory} | PID: #{$$}")
+ def log_memory
+ if @instruments.include?(:memory)
+ memory = Oink::Instrumentation::MemorySnapshot.memory
+ logger.info("Memory usage: #{memory} | PID: #{$$}")
+ end
end
- def log_objects_instantiated
- 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(' | ')}")
+ 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(' | ')}")
+ end
end
+ private
+
def reset_objects_instantiated
ActiveRecord::Base.reset_instance_type_count
end
- private
-
- def instrumented?(instrument)
- @options[:instruments].include?(instrument)
+ def logger
+ @logger ||= Hodel3000CompliantLogger.new(@log_path)
end
end
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.