Class: Oink::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/oink/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



8
9
10
11
12
13
14
# File 'lib/oink/middleware.rb', line 8

def initialize(app, options = {})
  @app         = app
  @logger      = options[:logger] || Hodel3000CompliantLogger.new("log/oink.log")
  @instruments = options[:instruments] ? Array(options[:instruments]) : [:memory, :activerecord]

  Oink.extend_active_record! if @instruments.include?(:activerecord)
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/oink/middleware.rb', line 16

def call(env)
  status, headers, body = @app.call(env)

  log_routing(env)
  log_memory
  log_activerecord
  log_completed
  [status, headers, body]
end

#log_activerecordObject



46
47
48
49
50
51
52
53
# File 'lib/oink/middleware.rb', line 46

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

#log_completedObject



26
27
28
# File 'lib/oink/middleware.rb', line 26

def log_completed
  @logger.info("Oink Log Entry Complete")
end

#log_memoryObject



39
40
41
42
43
44
# File 'lib/oink/middleware.rb', line 39

def log_memory
  if @instruments.include?(:memory)
    memory = Oink::Instrumentation::MemorySnapshot.memory
    @logger.info("Memory usage: #{memory} | PID: #{$$}")
  end
end

#log_routing(env) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/oink/middleware.rb', line 30

def log_routing(env)
  routing_info = rails3_routing_info(env) || rails2_routing_info(env)
  if routing_info
    controller = routing_info['controller']
    action     = routing_info['action']
    @logger.info("Oink Action: #{controller}##{action}")
  end
end