Class: Rack::Monitor::MonitorApp

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/monitor/monitor_app.rb

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of MonitorApp.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rack/monitor/monitor_app.rb', line 12

def initialize(app, options={})
  @app = app
  @options = {
    :url => '/rack_status',
    :ignore_addr => ['127.0.0.1']
  }.merge(options)
  sensor_class_names = Rack::Monitor.constants.reject { |s| %q(Sensor MonitorApp).include?(s) }
  @sensors = sensor_class_names.collect { |s| Rack::Monitor.const_get(s).new }
  @watches = {}
  if @options.has_key?(:watch)
    @options[:watch].each do |path|
      @watches[path] = [Request.new]
    end
  end
end

Instance Method Details

#call(env, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rack/monitor/monitor_app.rb', line 28

def call(env, options={})
  if env["PATH_INFO"] == @options[:url]
    [200, {'Content-Type' => 'text/plain'}, [monitor_output]]
  else
    unless @options[:ignore_addr].include?(env['REMOTE_ADDR'])
      @sensors.each { |sensor| sensor.before(env) }
      if @watches.has_key?(env["PATH_INFO"])
        @watches[env["PATH_INFO"]].each { |sensor| sensor.before(env) }
      end
    end

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

    unless @options[:ignore_addr].include?(env['REMOTE_ADDR'])
      if @watches.has_key?(env["PATH_INFO"])
        @watches[env["PATH_INFO"]].each { |sensor| sensor.after(env, status, headers, body) }
      end
      @sensors.each { |sensor| sensor.after(env, status, headers, body) }
    end
    [status, headers, body]
  end
end